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
    OR
  5. use a query to fetch an entity which has a LocalTime attribute set by LocalTime.now() betweeen 12AM and 1AM
1 Like

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.

I’m having the same exact issue as described by @vaqxai even after I updated to hibernate core 6.4.4 Final (Spring boot 3.2.3).
Is there a way to handle this bug until it gets fixed?

Since the Jira issue (if it was created at all) was not linked here, it’s hard to understand or track down the progress. Maybe @vaqxai can post the link to the Jira issue?

1 Like