Hibernate incorrectly converting SQL Time to LocalTime between 12am and 1am (only if using LocalTime.now())

If you use LocalTime.now() and save something to a PostgreSQL database with that timestamp, the ORM will crash trying to read/save the value (particularly while converting java time to sql time). It’s something in LocalTimeJavaType.java at lines 123-139 of Hibernate ORM core version 6.2.5 Final. The error is as follows:

Exception in thread “main” java.time.DateTimeException: Invalid value for NanoOfSecond (valid values 0 - 999999999): -791000000

the values are random if writing, but are constant when reading.

also in that code “millis” are negative for some reason.

This does not happen if you use LocalTime.of() and provide the values yourself,
however it does happen if you use LocalTime.of(LocalTime.now().getHour(), LocalTime.now().getMinutes(), LocalTime.now().getSeconds()), even though the NanoOfSecond part is zero.

Steps to reproduce:

  1. Make sure it is between 12AM and 1AM.
  2. Create a persistent Entity with a LocalTime attribute defined by LocalTime.now()
  3. use the Session.persist function on it
  4. Try to commit the data
  5. use a query to fetch an entity which has a LocalTime attribute set by LocalTime.now() betweeen 12AM and 1AM

Try to reproduce this with our test case template (hibernate-test-case-templates/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub), and if you manage to do so, please create an issue in the issue tracker (https://hibernate.atlassian.net) and attach the reproducer to it.