Hibernate 5 does not handle LocalDate correctly

We are porting our Hibernate (5.0.2) code to Java 8, which also includes conversion from java.util.Date to java.time.LocalDate (to solve the problems associated with date processing in Java 7). One of the problems that I have encountered is how Hibernate handles the special value, which we use as the β€œzero date”, which is 0001-01-01 .

A property is declared as follows:

 @NotNull @Column(name = "START_DATE", nullable = false) private LocalDate startDate; 

The value is stored in the database as 0001-01-01 , however when loading Hibernate it unexpectedly turns into 0000-12-29 . I suppose this happens because Hibernate uses the Gregorian calendar by default, and since this date is used before it is introduced, some conversion is used.

Is there a way to configure Hibernate to disable this behavior (besides implementing a special property author)?

+6
source share
1 answer

This is really a mistake. I played it in the next test . I will open an error report for him.

If I use LocalDate :

 @Entity(name = "LocalDateEvent") public class LocalDateEvent { @Id private Long id; @NotNull @Column(name = "START_DATE", nullable = false) private LocalDate startDate; } 

and run this test:

 doInJPA(entityManager -> { LocalDateEvent event = new LocalDateEvent(); event.id = 1L; event.startDate = LocalDate.of(1, 1, 1); entityManager.persist(event); }); doInJPA(entityManager -> { LocalDateEvent event = entityManager.find(LocalDateEvent.class, 1L); assertEquals(LocalDate.of(1, 1, 1), event.startDate); }); 

I get:

 java.lang.AssertionError: expected:<0001-01-01> but was:<0000-12-29> 

The problem with Jira is here .

This does not work for OffsetDateTime :

 @Entity(name = "OffsetDateTimeEvent") public static class OffsetDateTimeEvent { @Id private Long id; @NotNull @Column(name = "START_DATE", nullable = false) private OffsetDateTime startDate; } 

and run this test:

 doInJPA(entityManager -> { OffsetDateTimeEvent event = new OffsetDateTimeEvent(); event.id = 1L; event.startDate = OffsetDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); entityManager.persist(event); }); doInJPA(entityManager -> { OffsetDateTimeEvent event = entityManager.find(OffsetDateTimeEvent.class, 1L); assertEquals(OffsetDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC), event.startDate); }); 

Throws:

 java.lang.AssertionError: expected:<0001-01-01T00:00Z> but was:<0001-01-01T01:34:52+01:34:52> 

Jira's problem for this is here .

+3
source

All Articles