Search code examples
javasqlmappingpojojooq

Jooq record.into(Pojo.class) with same fields name problem


I have code that retrieves one record of 2 objects(POJO generated by JooQ) from 2 tables.

 Record record = dsl.select()
            .from(ISSUE)
            .leftJoin(CLIENT).on(CLIENT.ID.eq(ISSUE.CLIENT_ID))
            .where(ISSUE.ID.eq(id))
            .fetchOne();
    JIssue jIssue = record.into(JIssue.class);
    JClientRecord jClient = record.into(JClientRecord.class);

Both table ISSUE and table CLIENT have PK field "ID".

The problem is when mapping into(POJO.class), table fields that have same names not mapped correctly to POJO. In example above jIssue takes id of jClient.

When using TableRecords instead of POJO's all mapping done correctly, but you cant use TableRecord in generated DAO, it takes generated POJO.

How can i solve this problem so jooq could map to generated pojo fields correctly?


Solution

  • The POJO classes do not have any reliable meta information to clearly disambiguate between two equally named columns from the source record. However, you can copy your joined record into a TableRecord for each table, and then copy that record again into your POJO:

    JIssue jIssue = record.into(ISSUE).into(JIssue.class);
    JClientRecord jClient = record.into(CLIENT).into(JClientRecord.class);