Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 982
Ryan Yuen Avatar asked Nov 23 '15 01:11

Ryan Yuen


2 Answers

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.

like image 97
Ryan Avatar answered Nov 10 '22 01:11

Ryan


@Column(name = "updated", columnDefinition="DATETIME")
private LocalDateTime updated;

@Column(name = "created", columnDefinition="TIMESTAMP")
private LocalDateTime created;
like image 2
Vazgen Torosyan Avatar answered Nov 10 '22 02:11

Vazgen Torosyan