gpt4 book ai didi

java - Jooq CustomTypes 生成默认类型 POJO

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:42 25 4
gpt4 key购买 nike

尝试使用 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。基于文档(感谢捕获我的表达式/表达式错误 Luke)!

<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:

CREATE TABLE nonsense (
name VARCHAR(50) NOT NULL,
TEST_DATETIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
id UUID NOT NULL,
PRIMARY KEY(id)
);

Converter:

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:

<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>

And here are the results:

/**
* 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);

I'd like this to look like 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);

最佳答案

这可能是因为您的 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/> !

关于java - Jooq CustomTypes 生成默认类型 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19896772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com