Cómo asignar MySQL DATE '0000-00-00' & TIME '00: 00: 00 'con Hibernate
Primero, debe agregar "zeroDateTimeBehavior = convertToNull" al final de la URL de conexión db, que convierte las fechas cero en nulas mientras lee los registros.
En segundo lugar, debe asignar su campo a UserType personalizado como este:
@Column(name = "start_date")
@Type(type = "com.my.dao.support.hibernate.MySqlDateUserType")
public Date getStartDate() {
return startDate;
}
@Column(name = "start_time")
@Type(type = "com.my.dao.support.hibernate.MySqlTimeUserType")
public Time getStartTime() {
return startTime;
}
Tenga en cuenta que el campo debe asignarse como opcional.
Y por fin, dos clases, primero para DATE, segundo para TIME. Probado con Hibernate 3.6, MySQL 5.1, 5.5.
MySqlDateUserType
package com.th.dao.support.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.*;
/**
* Allows save MySql DATE '0000-00-00'
*/
public class MySqlDateUserType implements UserType {
private static final int[] SQL_TYPES = {Types.DATE};
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return Date.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
public int hashCode(Object arg0) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
// if the date is 0000-00-00 return null, else return the Date
Date result = null;
String strResult = resultSet.getString(names[0]);
if (strResult != null && !strResult.equals("0000-00-00"))
result = resultSet.getDate(names[0]);
return result;
}
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
// if the date is null set the value to "0000-00-00" else save the date
if (value == null)
statement.setString(index, "0000-00-00");
else
statement.setDate(index, (Date) value);
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
}
MySqlTimeUserType
package com.th.dao.support.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.*;
/**
* Allows save MySql TIME '00:00:00'
*/
public class MySqlTimeUserType implements UserType {
private static final int[] SQL_TYPES = {Types.TIME};
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return Time.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
public int hashCode(Object arg0) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
// if the time is 00:00:00 return null, else return the Time
Time result = null;
String strResult = resultSet.getString(names[0]);
if (strResult != null && !strResult.equals("00:00:00"))
result = resultSet.getTime(names[0]);
return result;
}
public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
// if the time is null set the value to "00:00:00" else save the time
if (value == null)
statement.setString(index, "00:00:00");
else
statement.setTime(index, (Time) value);
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
}
Este código basado en la clase de Preston para TIMESTAMP:El tipo de usuario personalizado de Hibernate no funciona