简体   繁体   English

在jooQ中使用左联接的自定义POJO

[英]Custom POJO with Left Join in jooQ

I'm doing a left join of Table A and Table B and trying to fetch the results into a custom POJO which has fields from both Table A and Table B as follows: 我正在对表A和表B进行左连接,并尝试将结果提取到自定义POJO中,该自定义POJO具有表A和表B的字段,如下所示:

List<MyCustomPojo> res = create.select()
                         .from(TABLE_A)
                         .leftJoin(TABLE_B)
                         .on(TABLE_A.MY_CODE.eq(TABLE_B.MY_CODE))
                         .fetchInto(MyCustomPojo.class);

It works fine for all the fields except for the field myCode the one on which these two tables are joined. 它对所有字段都适用,除了myCode字段(将这两个表连接到该字段)之外。 For me the values for myCode were picked up from the right table, Table B, which is NULL for all of those records in Table A that do not have a corresponding entry in Table B. I would like to know how jooQ decides which field to map to POJO and if this behavior is documented anywhere. 对我来说, myCode的值是从正确的表B中选取的,对于表A中所有在表B中没有对应条目的记录,该值均为NULL。我想知道jooQ如何决定要选择哪个字段映射到POJO,并且如果在任何地方都记录了此行为。

My goal is to fetch all the fields from Table A and Table B into the custom POJO such that myCode is picked up from the left table. 我的目标是将表A和表B中的所有字段提取到自定义POJO中, 以便从左侧表中获取myCode I would appreciate your advice on the right way to achieve it. 感谢您提出正确的建议。

The default behaviour of ResultQuery.fetchInto(Class) (and most other into(Class) methods) is specified in DefaultRecordMapper . DefaultRecordMapper指定ResultQuery.fetchInto(Class) (和大多数其他into(Class)方法)的默认行为。 It can be overridden globally by providing a custom RecordMapperProvider in your Configuration . 通过在Configuration 提供一个自定义的RecordMapperProvider ,可以全局覆盖它。

In your particular case, DefaultRecordMapper will map all values from your records in field order. 在您的特定情况下, DefaultRecordMapper将按字段顺序映射记录中的所有值。 If there's a column that appears twice, it will be mapped twice, meaning that the second value will persist in your resulting object. 如果有一列出现两次,它将被映射两次,这意味着第二个值将保留在结果对象中。 There are two easy workarounds: 有两种简单的解决方法:

Don't select the "wrong" myCode . 不要选择“错误的” myCode This is really the most robust solution 这确实是最可靠的解决方案

List<MyCustomPojo> res = create
    .select(TABLE_A.fields())
    .select(/* all fields in TABLE_B except the ones you don't want */)
    .from(TABLE_A)
    .leftJoin(TABLE_B)
    .on(TABLE_A.MY_CODE.eq(TABLE_B.MY_CODE))
    .fetchInto(MyCustomPojo.class);

Use RIGHT JOIN instead: 使用RIGHT JOIN代替:

Perhaps a bit of a hack, but this will quickly reverse the table order in your SELECT statement. 也许有点骇人听闻,但这会迅速逆转SELECT语句中的表顺序。

List<MyCustomPojo> res = create
    .select()
    .from(TABLE_B)
    .rightJoin(TABLE_A)
    .on(TABLE_A.MY_CODE.eq(TABLE_B.MY_CODE))
    .fetchInto(MyCustomPojo.class);

Finally a use-case for RIGHT JOIN :) 最后是RIGHT JOIN的用例:)

Note, this is the only solution that will also prevent wrong values for other columns that "accidentally" share the same name. 注意,这是唯一可以防止“偶然”共享相同名称的其他列的错误值的解决方案。

Add the "correct" myCode field once more. 再次添加“正确的” myCode字段。

Another hack, but it will work around the issue you're experiencing: 另一个hack,但是它可以解决您遇到的问题:

List<MyCustomPojo> res = create
    .select(TABLE_A.fields())
    .select(TABLE_B.fields())
    .select(TABLE_A.MY_CODE)
    .from(TABLE_A)
    .leftJoin(TABLE_B)
    .on(TABLE_A.MY_CODE.eq(TABLE_B.MY_CODE))
    .fetchInto(MyCustomPojo.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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