Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOOQ record mapper maps the wrong id on join

Tags:

java

sql

mysql

jooq

I have two tables, asdf and qwer, both tables have a primary key named "id". When I join these two tables, the result will have two columns named id, and JOOQ is unable to map the records correctly to POJOs.

sql.select(ASDF.fields())
.select(QWER.fields())
.from(ASDF)
.leftOuterJoin(QWER).onKey(QWER.ASDF_ID)
.where(ASDF.SOMETHING.eq(something))
.fetch(r -> tuple(r.into(Asdf.class), r.into(Qwer.class)))

Now every Asdf instance has the same id as it's corresponding Qwer instance in the tuple.

Is there a clever aliasing trick that can solve this problem, or did I miss something in the JOOQ documentation, or is this a bug in JOOQ?

like image 709
kazmer Avatar asked Oct 02 '15 14:10

kazmer


1 Answers

DefaultRecordMapper maps Record columns to POJO attributes by column name. Even if the two ID columns may have distinct table references associated to them in the Record (ASDF.ID vs. QWER.ID), there is no such table information available in the POJO.

One workaround would be to map r to the respective TableRecord first, using Record.into(Table):

r -> tuple(
    r.into(ASDF).into(Asdf.class), 
    r.into(QWER).into(Qwer.class)
)
like image 142
Lukas Eder Avatar answered Oct 17 '22 00:10

Lukas Eder