Sydney
Sydney

Reputation: 12212

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.

Upvotes: 1

Views: 12223

Answers (1)

Sydney
Sydney

Reputation: 12212

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.

Upvotes: 4

Related Questions