Hibernate 5: How to Persist LocalDateTime and Co With Hibernate
Learn to persist LocalDateTime and Co with Hibernate 5.
Join the DZone community and get the full member experience.
Join For FreeDo 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 two 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 the video below.
Java 8 Support in Hibernate 5
One of the features added to Hibernate 5 is the support of Java 8 classes like the date and time API. The Java 8 support is shipped in a separate jar file called hibernate-java8.jar, which you need to add to the classpath of your application.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
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 provide 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 typeLocalDate
,LocalDateTime
, and Duration
in the following code snippet.
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column
private LocalDate date;
@Column
private LocalDateTime dateTime;
@Column
private Duration duration;
...
}
You can then use these attributes in the same way as any other attributes in your Java code.
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
MyEntity e = new MyEntity();
e.setDate(LocalDate.now());
e.setDateTime(LocalDateTime.now());
e.setDuration(Duration.ofDays(2));
em.persist(e);
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.
Published at DZone with permission of Thorben Janssen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments