尝试使用 Joda DateTimes 生成 JooQ POJO,但遇到了一些问题。POJO 来自生成器,带有默认的 java.sql.TimeStamp 值,而不是 DateTimes。
代码如下。
创建表——此处时间戳字段的名称已更改——希望确保我没有在构建系统中的某个地方访问缓存。此名称仍应与以下正则表达式一起使用以进行匹配。
CREATE TABLE nonsense (
name VARCHAR(50) NOT NULL,
DATETIME_TEST TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
id UUID NOT NULL,
PRIMARY KEY(id)
);
转换类——基于文档。
public class LocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {
@Override
public LocalDateTime from(Timestamp databaseObject) {
return new LocalDateTime(databaseObject.getTime());
}
@Override
public Timestamp to(LocalDateTime dt) {
return new Timestamp(dt.toDateTime().getMillis());
}
@Override
public Class<Timestamp> fromType() {
return Timestamp.class;
}
@Override
public Class<LocalDateTime> toType() {
return LocalDateTime.class;
}
}
Jooq 配置 XML。基于文档(感谢您发现我的表达/表达错误卢克)!
<configuration>
<generator>
<database>
<customTypes>
<customType>
<name>org.joda.time.LocalDateTime</name>
<converter>n.b.jooqJodaTime.LocalDateTimeConverter</converter>
</customType>
</customTypes>
<forcedtypes>
<forcedType>
<name>org.joda.time.LocalDateTime</name>
<expressions>.*DATETIME.*</expressions>
</forcedType>
</forcedtypes>
</database>
</generator>
</configuration>
创建 POJO:
@javax.persistence.Column(name = "DATETIME_TEST", precision = 23, scale = 10)
public java.sql.Timestamp getDatetimeTest() {
return this.datetimeTest;
}
对于历史背景,这是最初的问题。
创建表:
CREATE TABLE nonsense ( name VARCHAR(50) NOT NULL, TEST_DATETIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP, id UUID NOT NULL, PRIMARY KEY(id) );
转换器:
package n.b.jooqJodaTime; import org.joda.time.DateTime; import org.jooq.Converter; import java.sql.Timestamp; public class DateTimeConverter implements Converter<Timestamp, DateTime> { @Override public DateTime from(Timestamp databaseObject) { return new DateTime(databaseObject.getTime()); } @Override public Timestamp to(DateTime dt) { return new Timestamp(dt.getMillis()); } @Override public Class<Timestamp> fromType() { return Timestamp.class; } @Override public Class<DateTime> toType() { return DateTime.class; } }
Jooq 配置:
<configuration> <jdbc> <url>jdbc:h2:file:build/database</url> <driver>org.h2.Driver</driver> <user>sa</user> </jdbc> <generator> <database> <name>org.jooq.util.h2.H2Database</name> <inputSchema>PUBLIC</inputSchema> <includes>.*</includes> <customTypes> <customType> <name>org.joda.time.DateTime</name> <converter>n.b.jooqJodaTime.DateTimeConverter</converter> </customType> </customTypes> <forcedtypes> <forcedType> <name>org.joda.time.DateTime</name> <expression>.*DATETIME.*</expression> </forcedType> </forcedtypes> </database> <generate> <pojos>true</pojos> <immutablePojos>true</immutablePojos> <jpaAnnotations>true</jpaAnnotations> <validationAnnotations>true</validationAnnotations> <deprecated>false</deprecated> </generate> <target> <packageName>n.b.c.generated.jooq</packageName> <directory>src/main/java/</directory> </target> </generator> </configuration>
结果如下:
/** * The column <code>PUBLIC.NONSENSE.TEST_DATETIME</code>. */ public final org.jooq.TableField<n.b.c.generated.jooq.tables.records.NonsenseRecord,
java.sql.Timestamp> TEST_DATETIME = createField("TEST_DATETIME", org.jooq.impl.SQLDataType.TIMESTAMP, this);
我希望它看起来像这样:
/** * The column <code>PUBLIC.NONSENSE.TEST_DATETIME</code>. */ public final org.jooq.TableField<n.b.c.generated.jooq.tables.records.NonsenseRecord,
org.joda.time.DateTime> TEST_DATETIME = createField("TEST_DATETIME", org.jooq.impl.SQLDataType.TIMESTAMP, this);