简体   繁体   中英

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.

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.

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

@Column(name = "created", columnDefinition="TIMESTAMP")
private LocalDateTime created;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM