Java 8 + Hibernate 5 MySQL TIMESTAMP/DATETIME to LocalDateTime Mapping
Asked Answered
A

2

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.

Aposematic answered 23/11, 2015 at 1:26 Comment(5)
Last time I checked, it only supports Date and Calendar. docs.oracle.com/javaee/6/api/javax/persistence/Temporal.htmlDemoiselle
Hello, I am using Java 8 and also I read this: 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 helpAposematic
Oops, sorry, didn't notice that article was an old articleDemoiselle
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 thoughts-on-java.org/persist-localdate-localdatetime-jpaDemoiselle
I am currently using this workaround but I wanted to remove it because I saw the LocalDateTime support in Hibernate 5. Thanks anyway!Aposematic
M
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.

Meta answered 8/12, 2015 at 22:52 Comment(0)
M
2
@Column(name = "updated", columnDefinition="DATETIME")
private LocalDateTime updated;

@Column(name = "created", columnDefinition="TIMESTAMP")
private LocalDateTime created;
Maimonides answered 27/11, 2016 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.