Does Jooq support mapping Record into Pojo class with the same column names?

121 views
Skip to first unread message

extj...@gmail.com

unread,
Aug 23, 2019, 11:07:07 AM8/23/19
to jOOQ User Group
Hi, everyone

I have two tables: TABLE_A and TABLE_B, they have some columns with the same names.

TABLE_A (ID, NAME, ADDRESS)
TABLE_B (ID, NAME)

I want to retrieve all columns from both tables by join:

List<MyDto> results = query.select()
                         .from(TABLE_A)
                         .join(TABLE_B)
                         .on(TABLE_A.ID.equal(TABLE_B.ID))
                         .fetchInto(MyDto.class);


And I use the @Column annotation to specify which column comes from which table, like this:

import javax.persistence.Column;

public class MyDto {

    @Column(table = "TABLE_A", name = "ID")
    private String idA;

    @Column(table = "TABLE_A", name = "NAME")
    private String nameA;

    @Column(table = "TABLE_A", name = "ADDRESS")
    private String addressA;

    @Column(table = "TABLE_B", name = "ID")
    private String idB;

    @Column(table = "TABLE_B", name = "NAME")
    private String nameB;

}


or this:

<code>
import javax.persistence.Column;

public class MyDto {

    @Column(name = "TABLE_A.ID")
    private String idA;

    @Column(name = "TABLE_A.NAME")
    private String nameA;

    @Column(name = "TABLE_A.ADDRESS")
    private String addressA;

    @Column(name = "TABLE_B.ID")
    private String idB;

    @Column(name = "TABLE_B.NAME")
    private String nameB;

}
</code>

But both cannot work as expected, does Jooq support this feature?
If not, how to implement this?

Thanks,
Extjs

Lukas Eder

unread,
Aug 26, 2019, 3:43:56 AM8/26/19
to jOOQ User Group
Thank you very much for your message. Unfortunately, this is not yet supported in jOOQ. We have a feature request to improve this:

As a workaround, you could write your own RecordMapper (possibly extending upon DefaultRecordMapper) and hook it into your Configuration using a RecordMapperProvider:

Alternatively, instead of using JPA annotations (I'm not sure if that is actually idiomatic JPA usage), you could alias your projected columns to TABLE_A.NAME.as("name_a") or TABLE_A.NAME.as("nameA") to get the wanted behaviour.

I hope this helps
Lukas 

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/e8877340-f107-4a95-b28e-040591e46078%40googlegroups.com.

Lukas Eder

unread,
Aug 26, 2019, 5:14:52 AM8/26/19
to jOOQ User Group
For the record, this question has also been asked here:
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.

extj...@gmail.com

unread,
Aug 26, 2019, 6:22:01 AM8/26/19
to jOOQ User Group
Thanks Lukas,

I have read the contents of issues/5961, but it seems has a lower priority to support.
But I think it would be better to implements this, since it may be a common solution
to resolve duplicate name problems in Jooq.

I cannot use column alias to implement this, since I need a dynamic query not a static one.
I will try to write a custom RecordMapper to implement this.

Thank you.
Extjs
To unsubscribe from this group and stop receiving emails from it, send an email to jooq...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages