Como mapear MySQL DATE '0000-00-00' & TIME '00: 00: 00 'com Hibernate

Primeiro, você precisa adicionar "zeroDateTimeBehavior = convertToNull" ao final do URL da conexão db - que converte zero datas em nulos durante a leitura de registros.

Segundo, você precisa mapear seu campo para 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;
}

Observe que o campo deve ser mapeado como opcional.

E, finalmente, duas classes, primeiro para DATE, scond por TIME. Testado com o 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 baseado na classe de Preston para TIMESTAMP:O UserType personalizado de hibernação não está funcionando

questionAnswers(0)

yourAnswerToTheQuestion