简体   繁体   English

Hibernate-在PostgreSQL中使用java.time.Instant

[英]Hibernate - Using java.time.Instant with PostgreSQL

I get a SchemaManagementException because of a type mismatch. 由于类型不匹配,我得到了SchemaManagementException The Hibernate version is 5.2.8.Final. Hibernate版本是5.2.8.Final。 The hibernate.ddl-auto is set to validate hibernate.ddl-auto设置为validate

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [end_date] in table [certificate]; found [date (Types#DATE)], but expecting [timestamp (Types#TIMESTAMP)]

The column entity is of type java.time.Instant , and the PostgreSQL column is of type TIMESTAMPTZ . 列实体的类型为java.time.Instant ,而PostgreSQL列的类型为TIMESTAMPTZ

@Column(name = "end_date")
private Instant endDate;

For java.time.Instant , the Hibernate type is InstantType which maps to a TIMESTAMP JDBC type. 对于java.time.Instant ,Hibernate类型是InstantType ,它映射到TIMESTAMP JDBC类型。 So I understand why the error says that it's expecting a Types#TIMESTAMP . 因此,我理解为什么错误会提示您正在等待Types#TIMESTAMP What I don't understand is that the error says that it found a Types#DATE . 我不明白的是,该错误表明它找到了Types#DATE

Source: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html 资料来源: https : //docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

One workaround is to set the column definition as 一种解决方法是将列定义设置为

@Column(name = "end_date", columnDefinition="DATE")
private Instant endDate;

but I am not really convinced it's a solution. 但我真的不相信这是一个解决方案。 To me an Instant is a TIMESTAMP , not a DATE . 对我而言,即时是TIMESTAMP ,而不是DATE

The workaround is not the solution. 解决方法不是解决方案。 For java.time.Instant , the PostgreSQL column should be of type TIMESTAMP . 对于java.time.Instant ,PostgreSQL列应为TIMESTAMP类型。 It was a mistake in the DB creation. 这是数据库创建中的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM