How to use the Java 8 LocalDateTime with JPA and Hibernate

gstackoverflow picture gstackoverflow · Jan 3, 2015 · Viewed 32.4k times · Source

I have the following class description snippet:

... 
@Column(name = "invalidate_token_date")
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime invalidateTokenDate;
....

This code doesn't work on Hibernate 4 because @Temporal doesn't support LocalDateTime.

I saw the suggestion on how to use LocalDateTime from Joda-Time but I use Java 8.

Answer

Paul Samsotha picture Paul Samsotha · Sep 20, 2015

For any Hibernate 5.x users, there is

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.0.Final</version>
</dependency>

You don't need to do anything else. Just add the dependency, and the Java 8 time types should work like any other basic types, no annotations required.

private LocalDateTime invalidateTokenDate;

Note: this won't save to timestamp type though. Testing with MySQL, it saves to datetime type.