DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Hibernate 5: How to Persist LocalDateTime and Co With Hibernate

Hibernate 5: How to Persist LocalDateTime and Co With Hibernate

Learn to persist LocalDateTime and Co with Hibernate 5.

Thorben Janssen user avatar by
Thorben Janssen
·
Nov. 20, 18 · Tutorial
Like (1)
Save
Tweet
Share
18.49K Views

Join the DZone community and get the full member experience.

Join For Free

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

Hibernate

Published at DZone with permission of Thorben Janssen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Quest for REST
  • How To Create and Edit Excel XLSX Documents in Java
  • A Brief Overview of the Spring Cloud Framework
  • ChatGPT: The Unexpected API Test Automation Help

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: