简体   繁体   中英

Hibernate - Using java.time.Instant with PostgreSQL

I get a SchemaManagementException because of a type mismatch. The Hibernate version is 5.2.8.Final. The hibernate.ddl-auto is set to 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 .

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

For java.time.Instant , the Hibernate type is InstantType which maps to a TIMESTAMP JDBC type. So I understand why the error says that it's expecting a Types#TIMESTAMP . What I don't understand is that the error says that it found a Types#DATE .

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

The workaround is not the solution. For java.time.Instant , the PostgreSQL column should be of type TIMESTAMP . It was a mistake in the DB creation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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