10

Decided to update to Hibernate 5 to remove the existing Date to LocalDateTime conversion. I installed hibernate-java8 artifact from Maven. Then I replaced my hibernate entity date time to

@Column (name = "mis_a_jour_au", nullable = false)
@Temporal (TemporalType.TIMESTAMP)
private LocalDateTime misAJourAu;

@Column (name = "envoi_au", nullable = false)
@Temporal (TemporalType.TIMESTAMP)
private LocalDateTime envoiAu;

This exception was thrown

org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property

If I remove the @Temporal then the exception becomes

ClassCastException: java.util.Date cannot be cast to java.time.LocalDateTime

I thought Java 8 + Hibernate 5 supports LocalDateTime? Please advise.

Bacteria
  • 8,406
  • 10
  • 50
  • 67
Ryan Yuen
  • 111
  • 1
  • 7
  • Last time I checked, it only supports Date and Calendar. https://docs.oracle.com/javaee/6/api/javax/persistence/Temporal.html – bmarkham Nov 23 '15 at 02:27
  • 2
    Hello, I am using Java 8 and also I read this: http://stackoverflow.com/a/32680455 , which says LocalDateTime is supported in the hibernate-java8 artifact. I don't quite understand how hibernate handles the types apart from converter so I need some help – Ryan Yuen Nov 23 '15 at 06:40
  • Oops, sorry, didn't notice that article was an old article – bmarkham Nov 23 '15 at 06:58
  • Unfortunately, I don't have and answer for you. But it seems that JPA doesn't support LocalDateTime. Here's an (recent) article about it and how to work around it. I hope this helps http://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/ – bmarkham Nov 23 '15 at 07:14
  • I am currently using this workaround but I wanted to remove it because I saw the LocalDateTime support in Hibernate 5. Thanks anyway! – Ryan Yuen Nov 23 '15 at 23:43

2 Answers2

4

Just remove the line: @Temporal (TemporalType.TIMESTAMP) in each case you define it.

Hibernate 5 reads LocalDateTime as the type and correctly inserts the data into the database as a timestamp. There isn't much information at this time due to the fact that they released the product and documentation will follow.

Ryan
  • 55
  • 3
2
@Column(name = "updated", columnDefinition="DATETIME")
private LocalDateTime updated;

@Column(name = "created", columnDefinition="TIMESTAMP")
private LocalDateTime created;
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Vazgen Torosyan
  • 1,255
  • 1
  • 12
  • 26