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.
Join the DZone community and get the full member experience.
Join For FreeContext
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.
Analysis
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.
Solutions
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[] {
Types.TIMESTAMP
};
@Override
public String objectToSQLString(Object o) {
throw new UnsupportedOperationException();
}
@Override
public String toXMLString(Object o) {
return o.toString();
}
@Override
public Object fromXMLString(String s) {
return LocalDateTime.parse(s);
}
@Override
public int[] sqlTypes() {
return SQL_TYPES;
}
@Override
public Class returnedClass() {
return LocalDateTime.class;
}
@Override
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);
}
@Override
public int hashCode(Object o) throws HibernateException {
return o.hashCode();
}
@Override
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());
}
@Override
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)));
}
}
@Override
public Object deepCopy(Object o) throws HibernateException {
return o;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object o) throws HibernateException {
return (Serializable) o;
}
@Override
public Object assemble(Serializable serializable, Object o) throws HibernateException {
return serializable;
}
@Override
public Object replace(Object o, Object o1, Object o2) throws HibernateException {
return o;
}
}
Conclusion
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.
Opinions expressed by DZone contributors are their own.
Comments