HSQLDB 中带时区的时间戳

Timestamp with timezone in HSQLDB

我的项目正在使用 postgres,还有一些使用 hsqldb (2.4.1) 的内存测试

客户实体有这个字段:

@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false)
private Date creationDate;

其中日期是 java.util.Date

table 是使用 "timestamp with timezone"

创建的
CREATE TABLE client (
  id bigint NOT NULL,
  creationdate timestamp with time zone,
  ...)

当我 运行 测试时,dbunit 正在加载一个包含数据的文件,我得到这个错误:

2018-08-21 09:39:03,194 [warn] o.d.u.SQLHelper - CLIENT.CREATIONDATE data type (2014, 'TIMESTAMP WITH TIME ZONE') not recognized and will be ignored. See FAQ for more information.

所以我想我应该扩展 HSQLDialect 以便能够支持它。但是我不知道怎么做,我需要的是 registerColumnType() 还是 registerHibernateType()?或者也许可以改用演员表?

它是一个 dbUnit class(可能被日志框架混淆为 "o.d.u.SQLHelper")发出该警告消息。表示dbUnit不支持指定的数据类型。

TL;DR

请创建增强请求以支持此 SQL 标准数据类型: https://sourceforge.net/p/dbunit/feature-requests/

如果您通过测试实现它并创建合并请求或附加补丁,让 dbUnit 支持这种数据类型将会更快。

详情

消息显示 "See FAQ",FAQ 页面包含有关该问题的信息: http://dbunit.sourceforge.net/faq.html#typenotrecognized

单击 "replace the default data type factory" link 到此 FAQ 条目: http://dbunit.sourceforge.net/faq.html#typefactory

它显示使用 database-specific 数据类型工厂。
你的数据库是 HSQLDB,所以,正如它提到的,在 "org.dbunit.ext.hsqldb" 子包中找到它的 class:http://dbunit.sourceforge.net/xref/org/dbunit/ext/hsqldb/HsqldbDataTypeFactory.html.

按照示例配置 dbUnit 以使用它。 或者,根据您的设置(例如使用 Spring?哪个 TestCase?),这是一个有用的示例和另一种方法(此示例是我的典型设置): http://dbunit.sourceforge.net/testcases/PrepAndExpectedTestCase.html#Configuration_Example_Using_Spring

但是,查看 HsqldbDataTypeFactory 的源代码显示其中不支持的数据类型: http://dbunit.sourceforge.net/xref/org/dbunit/ext/hsqldb/HsqldbDataTypeFactory.html

或其parent: http://dbunit.sourceforge.net/xref/org/dbunit/dataset/datatype/DefaultDataTypeFactory.html

第 71 行的 parent、DefaultDataTypeFactory 委托给 DataType: http://dbunit.sourceforge.net/xref/org/dbunit/dataset/datatype/DataType.html

DataType 使用 Java API 类型 class 定义支持的数据类型: https://docs.oracle.com/javase/8/docs/api/java/sql/Types.html

在“类型”页面中搜索 "timestamp with timezone" 发现: https://docs.oracle.com/javase/8/docs/api/java/sql/Types.html#TIMESTAMP_WITH_TIMEZONE

我们看到它是在Java 8.

中添加的

搜索 Types.TIMESTAMP_WITH_TIMEZONE 的 DataType 未找到对 "timestamp with timezone" 的支持。这就是我们知道它丢失的方式。