Mapping timestamps to java.time classes broken for dates with negative years

Description

Given an entity with a java.time.* field mapped to a column with type timestamp, Hibernate does not read back the same value after writing a date with a negative year (any BCE date).

A use case for storing such a date is storing Instant.MIN to represent an arbitrary far-past date, or using PostgreSQL's -infinity timestamp literal.

This is a regression introduced with the fix for https://hibernate.atlassian.net/browse/HHH-13266. The root cause is that java.sql.Timestamp internally represents these dates with a positive year and stores the era (BCE vs. CE) separately. Timestamp#toLocalDateTime incorrectly returns a value using the positive year for BCE dates and ignores the era. Arguably this is a JDK bug, but it would be helpful to have this work in Hibernate.

PR with test cases:

Environment

None

Assignee

Unassigned

Reporter

Toby Hammett

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.4.1

Components

Affects versions

Priority

Major
Configure