Over a million developers have joined DZone.

Properly Persisting LocalDateTime to Oracle DATE Column With Hibernate 5

DZone's Guide to

Properly Persisting LocalDateTime to Oracle DATE Column With Hibernate 5

Let's take a look at a tutorial that explores properly persisting LocalDateTime to Oracle DATE column with Hibernate 5.

· Database Zone ·
Free Resource

Read the 2019 State of Database DevOps Report for the very latest insights


In my current Java project, I'm using hibernate to persist a Java LocalDateTime to an Oracle DATE type column.

Out of the box, everything seems to work well, but when the amount of data in the queried table increases, the execution time drastically increases as well.

The technical context is: Java 8, Hibernate 5, and Oracle 11g.


After an analysis of the explain plan by a DBA, it seemed that the query was not using the proper index created on the DATE column.

I did some research and found the useful post below:


The problem was located at the binding level when Hibernate prepared the query.

java.time.LocalDateTime is converted to java.sql.Timestamp and finally bound to oracle.sql.TIMESTAMP.

Our column has oracle.sql.DATE type so there is an implicit conversion in Oracle to perform the query causing the index not to be used.


Change Type

The first solution is the simpler one: change the column type to TIMESTAMP.

Unfortunately, this is not always possible.

Custom Hibernate Type

The second solution is to create a Hibernate custom type in order to control the binding of the field.

Annotate the field of your entity with @Type

And org.mycompany.util.LocalDateTimeUserType is a custom Hibernate UserType:

import oracle.sql.DATE;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.usertype.EnhancedUserType;

import java.io.Serializable;
import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class LocalDateTimeUserType implements EnhancedUserType, Serializable {

    private static final int[] SQL_TYPES = new int[] {

    public String objectToSQLString(Object o) {
        throw new UnsupportedOperationException();

    public String toXMLString(Object o) {
        return o.toString();

    public Object fromXMLString(String s) {
        return LocalDateTime.parse(s);

    public int[] sqlTypes() {
        return SQL_TYPES;

    public Class returnedClass() {
        return LocalDateTime.class;

    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y) {
            return true;
        if (x == null || y == null) {
            return false;
        LocalDateTime dtx = (LocalDateTime) x;
        LocalDateTime dty = (LocalDateTime) y;
        return dtx.equals(dty);

    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();

    public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
        Object timestamp = StandardBasicTypes.TIMESTAMP.nullSafeGet(resultSet, names, session, owner);
        if (timestamp == null) {
            return null;
        Date ts = (Date) timestamp;
        Instant instant = Instant.ofEpochMilli(ts.getTime());
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

    public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
        if (value == null) {
            StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, null, index, session);
        } else {
            LocalDateTime ldt = ((LocalDateTime) value);
            preparedStatement.setObject(index, new DATE(Timestamp.valueOf(ldt)));

    public Object deepCopy(Object o) throws HibernateException {
        return o;

    public boolean isMutable() {
        return false;

    public Serializable disassemble(Object o) throws HibernateException {
        return (Serializable) o;

    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return serializable;

    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return o;


When using Oracle, the choice of a column type could have important consequences on queries performance, especially with DATE type.

If you need to store Date and Time, prefer the TIMESTAMP type and use Java LocalDateTime.

If Date only is enough, Oracle DATE type is ok.

Read the 2019 State of Database DevOps Report for latest insights into DevOps adoption among SQL Server professionals, and the benefits and challenges of including the database in DevOps initiatives

database ,tutorial ,localdatetime ,javaproject ,hibernate 5 ,oracle tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}