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.
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|
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.