When using jooq with Nested Records (see: https://www.jooq.org/doc/latest/manual/sql-building/column-expressions/nested-records/) and Ad-Hoc Conversion (see: https://www.jooq.org/doc/latest/manual/coming-from-jpa/from-jpa-manytoone/), i could not find a good example for the case of processing a “Nullable 1:1 relationships”.
Following example:
- A product can have an “editor”, but the editor could also be NULL (no editor)
I have tried it out and got it to work, but it seems a little bit boilerplate.
...
row(
PRODUCT.editor().USERID,
PRODUCT.editor().FIRSTNAME,
PRODUCT.editor().LASTNAME
).convertFrom(r -> (r.value1() == null) ? null : mapping(UserDTO::new).apply(r)),
...
Note that we do not need to define the leftJoin
here explicitly because jooq would use a Left Join (left outer join for mariadb) per default for the Path-Expression (see “Default JOIN type”: https://www.jooq.org/doc/latest/manual/sql-building/sql-statements/select-statement/implicit-join/), because we have the “nullable parent” case.
If the USERID
column returns NULL in the joined result, we can be sure that there is no User-Row in the User-Table matching the Foreign-Key.
This can be accomplished with the (r.value1() == null) ? null : ...
clause that returns NULL when the first column (USERID
) returns NULL.
- Is there a jooq-function to automatically resolve NULL-Values on
row()
without the explicit conversion seen here (maybe with help of the Path-Expression that contains the FK-Information)? - Is my way of doing this the recommended way?