Hibernate 5: How to persist LocalDateTime & Co with Hibernate

By Thorben Janssen

Date and Time, Mapping

Do you use Java 8’s Date and Time API in your projects? Let’s be honest, working with java.util.Date is a pain, and I would like to replace it with the new API in all of my projects.

The only problem is that JPA does not support it.

Java 8 was released after JPA 2.1, and the persistence standard does not support the new APIs. You can, of course, use LocalDate or other classes of the Date and Time API as entity attributes. But you can’t annotate them with @Temporal and Hibernate stores them as blobs in the database.

You have 2 options if you want to use the right JDBC types when you persist classes of the Date and Time API:

  • You can implement a JPA AttributeConverter and convert the Java 8 class into one that is supported by Hibernate. I described this in detail in How to persist LocalDate and LocalDateTime with JPA. This approach does not use any Hibernate-specific APIs and is portable to other JPA implementations, but it is a little complicated.
  • Or you can use the Hibernate-specific Java 8 support which was introduced with Hibernate 5. This approach is not portable to other JPA implementations but much easier to use as I will show you in this post.

Java 8 Support in Hibernate 5

One of the features added with Hibernate 5 is the support of Java 8 classes like the Date and Time API.

The Java 8 support was initially shipped in a separate jar file called hibernate-java8.jar, which you needed to add to the classpath of your application. Since Hibernate 5.2, it’s part of the core distribution and the additional jar file is no longer required.

If you are using Hibernate as part of Wildfly 10, you don’t have to do anything because the Hibernate module already contains the required jar file.

JDBC Mappings

Hibernate maps the classes of the Date and Time API to the corresponding JDBC types. The following table shows an overview of the supported classes and their JDBC mapping.

Java type JDBC type
java.time.Duration BIGINT
java.time.Instant TIMESTAMP
java.time.LocalDateTime TIMESTAMP
java.time.LocalDate DATE
java.time.LocalTime TIME
java.time.OffsetDateTime TIMESTAMP
java.time.OffsetTime TIME
java.time.ZonedDateTime TIMESTAMP

Date and Time API Classes As Entity Attributes

Hibernate supports the classes of the Date and Time API as BasicTypes. This provides the main advantage that you don’t have to add any additional annotations. Not even the @Temporal annotation which you currently add to each java.util.Date attribute. Hibernate gets all required information from the type of the attribute. You can see an example of an entity with attributes of type LocalDate, LocalDateTime, and Duration in the following code snippet.

You can then use these attributes in the same way as any other attributes in your Java code.

And as you can see in the following screenshot, Hibernate persists them with the right JDBC data type instead of the blob it uses without the hibernate-java8.jar.


We need to wait for JPA 2.2 to get standardized support for the Date and Time API. Until then you have to handle the type conversion yourself, or you can use the proprietary Java 8 support added in Hibernate 5.

If you’re using Hibernate 5.0.x or Hibernate 5.1.x, you need to add an additional jar file to your classpath.

Hibernate handles the classes of the Date and Time API as BasicTypes. This makes them even easier to use than the old java.util.Date because you don’t have to add any additional annotations.


Date and Time, Mapping

About the author

Thorben is an independent consultant, international speaker, and trainer specialized in solving Java persistence problems with JPA and Hibernate.
He is also the author of Amazon’s bestselling book Hibernate Tips - More than 70 solutions to common Hibernate problems.


Books and Courses

Coaching and Consulting

Leave a Reply

Your email address will not be published. Required fields are marked

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. could you please confirm below query’s
    1.hibernate 5.x support from which version of spring ?
    2.left joins, right join Query’s is it work with out relations?

    1. Hi,
      I don’t know exactly when Spring Data JPA started to support Hibernate 5. Please take a look at the documentation.
      JPQL supports only left joins and inner joins.

  2. Hello Thorben,
    In my case, the LocalDateTime and LocalDate persisted even without using the hibernate-java8 dependency. I am using the Spring-data-jpa and hibernate as it’s implementation. Is it handled by Spring?

  3. Hi Thorben,

    good post!

    I’m curious about what happens behind the scene. A LocalDateTime for instance has no time zone information. Will Hibernate use system default for the Timstamp? And if using the ZonedDateTime, will Hibernate use the associated Instant object?

    Kind regards,

  4. AttributeConverter cannot be used when the field is a primary key (nor part of a compound PK)

    if the LocalDate field is in fact a primary key, when running in wildlfy 10, will using this hibernate specific feature still work?

    1. Yes, it will work because Hibernate now supports LocalDate as native attribute. So, no AttributeConverter is needed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}