jooq fetch自定义对象已经ZonedDateTime字段遇到DataTypeException

时间:2017-12-13 03:13:19

标签: timestamp jooq

java-1.8 jooq-3.10.0 SpringBoot-1.5.7

myCondition = create .select(... TABLE_AL.START_DATE.as("table_al_start_date"), TABLE_BR.START_DATE.as("table_br_start_date"))) .from(MAIN).innerJoin(TABLE_AL).onkey() .innerJoin(TABLE_BR).onkey() .where(DSL.trueCondition()); List<MyObject> myObjList = myCondition.fetch().into(MyObject.class)

class MyObject(){ ZonedDateTime tableAlStartDate; ZonedDateTime tableBrStartDate; ... }

我的对象有几个表中的字段, table_al_start_datejava.time.ZonedDateTimetimestamp without time zone,nullable中的PostgreSQL

获取MyObject时会抛出DataType异常 Caused by: org.jooq.exception.DataTypeException: Cannot convert from 1511971200000 (class java.lang.Long) to class java.time.ZonedDateTime at org.jooq.tools.Convert$ConvertAll.fail(Convert.java:1169) at org.jooq.tools.Convert$ConvertAll.toDate(Convert.java:1121) at org.jooq.tools.Convert$ConvertAll.from(Convert.java:823)

如果使用 .select(TABLE_A.START_DATE.as("table_a_start_date").cast(ZonedDateTime.class) 会见 org.jooq.exception.SQLDialectNotSupportedException: Type class java.time.ZonedDateTime is not supported in dialect null

还试过添加一个setter setTableAlStartDate(Long long),无效。

我在custom-bindings找到了Converter,在创建自定义转换器之后应该怎么做,应该在哪里 DataType<LocalDate> type = SQLDataType.DATE.asConvertedDataType(new LocalDateConverter());这是?

ZonedDateTime字段应该怎么做? 感谢。

1 个答案:

答案 0 :(得分:0)

不确定这是一个好的做法,对我有用:

myCondition = create
    .select(,,,
        DSL.field("table_al.start_date", SQLDataType.TIMESTAMP
            .asConvertedDataType(ts2zonedConverter))
            .as("table_al_start_date"),,,, 
    .from(,,,);

myConverter

public class TsZonedDateTimeConverter implements Converter<Timestamp, ZonedDateTime> {
@Override public ZonedDateTime from(Timestamp t) {
        return t == null ? null : t.toInstant().atZone(ZoneId.of("UTC"));
    }
@Override public Timestamp to(ZonedDateTime u) {
        return u == null ? null : Timestamp.from(u.toInstant());
    }
}