Hibernate Java 8 LocalDate, LocalDateTime and Duration Example

In this article, we will learn how to persist Java 8 LocalDate, LocalDateTime, and Duration with Hibernate 6.
Master in Hibernate ORM at Hibernate 5 ORM Tutorial

Java 8 Support in Hibernate 6

One of the features added to Hibernate 6 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.
You can find the latest maven dependency here.

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.

Hibernate Java 8 LocalDate, LocalDateTime, and Duration Complete Example

Let's demonstrate Hibernate support for classes of the date and time API as BasicTypes with a complete example.

Technologies and tools used

  • Hibernate 6.1.7.Final
  • IDE - Eclipse
  • Maven 3.5.3
  • Java 17
  • MySQL - 8.0.32

Development Steps

  1. Create a Simple Maven Project
  2. Project Directory Structure
  3. Add jar Dependencies to pom.xml
  4. Creating the JPA Entity Class(Persistent class)
  5. Create a Hibernate configuration file - Java Configuration
  6. Create the Main class and Run an Application

1. Create a Simple Maven Project

Use the How to Create a Simple Maven Project in Eclipse article to create a simple Maven project in Eclipse IDE.

2. Project Directory Structure

The project directory structure for your reference - 

3. Add jar Dependencies to pom.xml

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->

4. Creating the JPA Entity Class (Persistent class)

Create a SimpleEntity entity class under net.javaguides.hibernate.entity package as follows.
package net.javaguides.hibernate.entity;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

import jakarta.persistence.*;

public class SimpleEntity {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    private LocalDate date;

    private LocalDateTime dateTime;

    private Duration duration;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public LocalDate getDate() {
        return date;

    public void setDate(LocalDate date) {
        this.date = date;

    public LocalDateTime getDateTime() {
        return dateTime;

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;

    public Duration getDuration() {
        return duration;

    public void setDuration(Duration duration) {
        this.duration = duration;
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 the required information from the type of attribute. 
You can see in a SimpleEntity class, we have attributes of type LocalDateLocalDateTime, and Duration.

5. Create a Hibernate configuration file - Java Configuration

The HibernateUtil Java configuration file contains information about the database and mapping file.
Let's create a HibernateUtil file and write the following code in it.
package net.javaguides.hibernate.util;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;

import net.javaguides.hibernate.entity.Student;

public class HibernateUtil {
    private static SessionFactory sessionFactory;
    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                Configuration configuration = new Configuration();

                // Hibernate settings equivalent to hibernate.cfg.xml's properties
                Properties settings = new Properties();
                settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
                settings.put(Environment.URL, "jdbc:mysql://localhost:3306/hibernate_db?useSSL=false");
                settings.put(Environment.USER, "root");
                settings.put(Environment.PASS, "root");

                settings.put(Environment.SHOW_SQL, "true");

                settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");

                settings.put(Environment.HBM2DDL_AUTO, "create-drop");



                ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()

                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
        return sessionFactory;

6. Create the main App class and Run an Application

Let's test Hibernate application to connect to the MySQL database.
package net.javaguides.hibernate;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

import org.hibernate.Session;
import org.hibernate.Transaction;

import net.javaguides.hibernate.entity.SimpleEntity;
import net.javaguides.hibernate.util.HibernateUtil;

public class App {
    public static void main(String[] args) {

        SimpleEntity simpleEntity = new SimpleEntity();

        Transaction transaction = null;
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            // start a transaction
            transaction = session.beginTransaction();
            // save the student object
            // commit transaction
        } catch (Exception e) {
            if (transaction != null) {


GitHub Repository

The complete source code of this article is available on my GitHub Repository - https://github.com/RameshMF/Hibernate-ORM-Tutorials


In this article, we have seen how to persist Java 8 LocalDate, LocalDateTime, and Duration with Hibernate 5.
You can learn more about Hibernate ORM Framework at Hibernate Tutorial