github twitter linkedin
Hibernate, Java 8 and Dates
2015-11-01

Java 8 introduced a new date and time framework (thank god, the old one is a piece of crap). But unfortunately, Hibernate doesn’t support those types not out of the box. The Jadira usertypes project adds support for those to hibernate.

Here’s what you have to do when using Hibernate and the Java 8 date types:

Include the correct Jadira type

You have to include the extended Jadira version, not the core one. If you include the core one, a ClassNotFoundException will be thrown.

<dependency>
  <groupId>org.jadira.usertype</groupId>
  <artifactId>usertype.extended</artifactId>
  <version>4.0.0.GA</version>
</dependency>

Annotate your entity fields

Add the type annotation with the correct Persistent-* class from the Jadira project.

@Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime")
@Column(nullable = false)
private ZonedDateTime start;

Register the types with Hibernate

Set the property jadira.usertype.autoRegisterUserTypes to true in your hibernate config.

And … done!

Have fun with your date times!


Back to posts