尝试使用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)
);
转化课程 - 基于documentation.
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&GT; 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&GT; TEST_DATETIME = createField(“TEST_DATETIME”, org.jooq.impl.SQLDataType.TIMESTAMP,this);
答案 0 :(得分:1)
这可能是由于XML中的拼写错误。 (由于历史原因)该元素应该被称为<expressions/>
而不是<expression/>
:
<complexType name="ForcedType">
<all>
<!-- The name of the type to be forced upon various artefacts -->
<element name="name" type="string" minOccurs="1" maxOccurs="1" />
<!--
A Java regular expression matching columns, parameters, attributes,
etc to be forced to have this type
-->
<element name="expressions" type="string" minOccurs="1" maxOccurs="1" />
</all>
</complexType>
See also the XSD for details。我认为值得为jOOQ 3.3中的两个元素名称创建同义词。现在注册为#2837
<forcedtypes/>
元素还有另一个拼写错误,应该是<forcedTypes/>
!