PostgreSQL: java.util.Instant - Hibernate creates column with typecode 93 and then expects typecode 3003

Description

What was observed?

Windows + PostgreSQL 10 or 14 Database / Linux + PostgreSQL 10 Database

Hibernate throws the following schema validation exception:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [mycolumn] in table [mytable]; found [timestamptz (Types#TIMESTAMP)], but expecting [timestamp(6) with time zone (Types#TIMESTAMP_UTC)]

The underlying Java datatype is java.util.Instant.

Additional Information

In both databases the information_schema describes the column as

data_type

datetime_precision

timestamp with time zone

6

The column in the windows database was created with an DDL statement containing timestamp(6) with time zone as datatype which was extracted from the linux database which had been created by hibernate with hibernate.hbm2ddl.auto=create-drop.

Recent tests showed that the bug also occurs in the linux installation when the application is redeployed. That means, hibernate is rejecting the datatype it created before.

What is the Bug?

After some analysis I could pin down the bug.

Hibernate creates the column as timestamp(6) with timezone which is later interpreted as a TIMESTAMP column (typeCode 93).
But since the Java type is a java.util.Instant typeCode 3003 is expected.

What is the impact?

PostgreSQL Schemas created by Hibernate that contain java.util.Instant can not be successfully validated by Hibernate under default settings. Therefore, in my opinion, the priority of this is Critical.

Workaround

Set hibernate.type.preferred_instant_jdbc_type=93

Activity

Show:

Mike Becker September 28, 2022 at 7:36 AM

Added final analysis of the bug and updated the priority from Major to Critical.

Mike Becker September 27, 2022 at 10:44 AM

Updated information about the circumstances under which the error occurs. We can exclude operating system and database version now.

Mike Becker September 26, 2022 at 10:31 AM

Updated the information on how the columns were created in each database to be more precise.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created September 26, 2022 at 10:09 AM
Updated October 5, 2022 at 10:02 AM
Resolved October 4, 2022 at 2:22 PM