我在使用强制类型生成代码时遇到了一些问题(JOOQ 3.3,Postgres 9.3)。
尝试将sql时间戳转换为joda DateTime,导致编译错误。
我的表:
CREATE TABLE book
(
// [...]
date_published timestamp without time zone,
// [...]
);
和我的.xml配置:
// [...]
<customTypes>
<customType>
<name>java.sql.Timestamp</name>
<converter>com.plannow.jooq.converters.DateTimeConverter</converter>
</customType>
</customTypes>
<forcedTypes>
<forcedType>
<name>java.sql.Timestamp</name>
<expression>.*\.date_.*</expression>
<types>.*</types>
</forcedType>
</forcedTypes>
// [...]
DateTimeConverter类:
public class DateTimeConverter implements Converter<Timestamp, DateTime>
{
@Override
public DateTime from(Timestamp databaseObject)
{
return new DateTime(databaseObject);
}
@Override
public Timestamp to(DateTime userObject)
{
return new Timestamp(userObject.getMillis());
}
@Override
public Class<Timestamp> fromType()
{
return Timestamp.class;
}
@Override
public Class<DateTime> toType()
{
return DateTime.class;
}
}
所以,BOOK.DATE_PUBLISHED就是这样生成的:
public final org.jooq.TableField<com.plannow.jooq.db.tables.records.BookRecord, java.sql.Timestamp> DATE_PUBLISHED = createField("date_published", org.jooq.impl.SQLDataType.TIMESTAMP, this, "", new com.plannow.jooq.converters.DateTimeConverter());
导致编译错误:
Type mismatch: cannot convert from TableField<BookRecord,DateTime> to TableField<BookRecord,Timestamp>.
我知道我可以将DATE_PUBLISHED
的类型更改为TableField<BookRecord,DateTime>
并重构代码,但我不想手动修补生成的类。
任何想法我做错了什么?
答案 0 :(得分:3)
<name/>
的{{1}}元素应引用Converter<T, U>
的<customType/>
类型(用户类型),而不是<U>
类型(数据库)类型)。所以,如果你这样写:
<T>
然后你真的只是注册<customTypes>
<customType>
<name>java.sql.Timestamp</name>
<converter>com.plannow.jooq.converters.DateTimeConverter</converter>
</customType>
</customTypes>
。试试这个:
Converter<Timestamp, Timestamp>
请注意,您的转换器也应正确处理<customTypes>
<customType>
<name>org.joda.time.DateTime</name>
<converter>com.plannow.jooq.converters.DateTimeConverter</converter>
</customType>
</customTypes>
值:
null