5

尝试使用 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);

4

1 回答 1

1

这可能是由于您的 XML 中的拼写错误。(出于历史原因)该元素应称为<expressions/>not <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>

有关详细信息,另请参阅 XSD。我认为值得为 jOOQ 3.3 中的两个元素名称创建同义词。现在注册为#2837

另一个错字

该元素还有另一个错字<forcedtypes/>,应该是<forcedTypes/>

于 2013-11-11T06:46:16.220 回答