简体   繁体   中英

JOOQ checking for null fields in a Record object?

I have a query in JOOQ, which contains a LEFT OUTER JOIN with another table. This is intentional, as there is no guarantee a corresponding record will exist, in which case I still want to retrieve the value from the former table, and supply the missing fields with defaults.

However, since the getValue() function with a default parameter is getting deprecated, how can I check whether the field contains a NULL value?

It's easy enough with strings, but a boolean value simply returns as false , meaning I can't check whether it really was set to false, or it's just what JOOQ decided to return.

I've tried:

if (record.field(MY_TABLE.SOME_BOOLEAN) != null) {
    ...
}

but that doesn't work, as the return value of the field() function isn't null, even if the value of the field is. I've also tried

if (record.field(MY_TABLE.SOME_BOOLEAN).isNull()){
   ...
}

but that isn't a valid Java conditional.

Thanks in advance!

What you're trying won't work:

if (record.field(MY_TABLE.SOME_BOOLEAN) != null) {  }

Record.field() will return a Field reference. The idea is that your record may not have been created from MY_TABLE , but it might still contain a SOME_BOOLEAN column, Record.field() will return that SOME_BOOLEAN column from the record.

What you're looking for is simply:

if (record.get(MY_TABLE.SOME_BOOLEAN) != null) {  }

Do note that jOOQ has no way of distinguishing whether the SOME_BOOLEAN column was actually NULL in your database, or if LEFT JOIN did not return any row. This is just how SQL works. If you want to check whether your LEFT JOIN actually found a child row, you could check for the null value in a NOT NULL column, such as a primary key:

if (record.get(MY_TABLE.ID) != null) {  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM