【发布时间】:2016-01-15 09:43:22
【问题描述】:
Jooq currently does not support JSR 310 types 和 support will not come until v3.8。
使用简单的转换器通常可以工作,但某些类型除外,例如 postgres 的TIMESTAMP WITH TIME ZONE,它需要自定义绑定。所以我尝试编写一个,但生成的XxxRecord 类仍然使用Timestamp 数据类型作为我数据库中的TIMESTAMP WITH TIME ZONE 字段。
我需要在下面的代码中进行哪些更改才能在 jooq 生成的类中将 postgres 的 TIMESTAMP WITH TIME ZONE 视为 Instant?
转换器
public class TimestampConverter implements Converter<Timestamp, Instant> {
@Override public Instant from(Timestamp ts) {
return ts == null ? null : ts.toInstant();
}
@Override public Timestamp to(Instant instant) {
return instant == null ? null : Timestamp.from(instant);
}
@Override public Class<Timestamp> fromType() { return Timestamp.class; }
@Override public Class<Instant> toType() { return Instant.class; }
}
自定义绑定
public class TimestampBinding implements Binding<Timestamp, Instant> {
private static final Converter<Timestamp, Instant> converter = new TimestampConverter();
private final DefaultBinding<Timestamp, Instant> delegate =
new DefaultBinding<> (converter());
@Override public Converter<Timestamp, Instant> converter() { return converter; }
@Override public void sql(BindingSQLContext<Instant> ctx) throws SQLException {
delegate.sql(ctx);
}
//etc. same for all other overriden methods.
}
pom.xml(摘录)
<customType>
<name>java.time.Instant</name>
<type>java.time.Instant</type>
<binding>xxx.TimestampBinding</binding>
</customType>
...
<forcedType>
<name>java.time.Instant</name>
<types>timestamp with time zone</types>
</forcedType>
【问题讨论】:
标签: java postgresql java-8 jooq java-time