Jooq查询结果如何自动映射到pojo?

时间:2017-08-11 19:07:13

标签: jooq

这是pojo用户:

public class User implements Serializable {
    private UInteger      id;
    private String        openId;
...
}

这是pojo UserDTO:

public class UserDTO extends User implements Serializable{

    @JsonProperty("sk")
    private String sk;

    @JsonProperty("cars")
    private List<Car> cars; //There are user id in table car
...
}

jooq查询如下:

    UserDTO userDTO  = dslContext.select().from(User.USER).
leftJoin(Car.CAR).on(User.USER.ID.eq(Car.CAR.USER_ID))
    .where(User.USER.OPEN_ID.eq(openId)).fetchOneInto(UserDTO.class);

我看到UserDTO的汽车是空的。 我想使用jooq查询结果自动填入UserDTO汽车而不是手动遍历查询结果然后分配给汽车。我该怎么办?

我尝试使用第三方映射器,问题是查询的结果有些字段为空,这会导致映射器转换异常,例外情况如下:

    "Exception in thread" main "java.lang.NumberFormatException: null
    At java.lang.Integer.parseInt (Integer.java:542)
    At java.lang.Short.parseShort (Short.java:118)
    At java.lang.Short.parseShort (Short.java:144)
    At org.jooq.types.UByte.valueOf (UByte.java:101)
    At org.simpleflatmapper.reflect.generated.org.jooq.types.AsmInstantiatorUByteFromStringIntovalue_I1.newInstance (Unknown Source) "


    String openId = "sdjakfh13145sdkadkdfc";
    JdbcMapper<Tuple2<UserRecord, List<CarRecord>>> mapper = JdbcMapperFactory.newInstance().addKeys("id")
            .newMapper(new TypeReference<Tuple2<UserRecord, List<CarRecord>>>() {
            });

ResultSet rs = dslContext.select().from(User.USER).leftJoin(Car.CAR).on(User.USER.ID.eq(Car.CAR.USER_ID)).where(User.USER.OPEN_ID.eq(openId)).fetchResultSet();
    try {Stream<Tuple2<UserRecord, List<CarRecord>>> stream = mapper.stream(rs);
        stream.forEach(System.out::println);
    } catch (MappingException | SQLException e) {
        e.printStackTrace();
    }

0 个答案:

没有答案