如何映射MySQL DATE' 0000-00-00' &安培; TIME' 00:00'与Hibernate

时间:2015-02-18 23:20:15

标签: java mysql hibernate usertype

首先,您需要将“zeroDateTimeBehavior = convertToNull”添加到数据库连接URL的末尾 - 在读取记录时将零日期转换为空。

其次,您需要将字段映射到自定义UserType,如下所示:

@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;
}

请注意,该字段应映射为可选字段。

最后,两个班级,第一个是DATE,第二个是TIME。测试了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;
    }
}

此代码基于Preston的TIMESTAMP类:Hibernate custom UserType isn't working

0 个答案:

没有答案