如何使用Hibernate映射MySQL DATE’EMA’&TIME ’00:00:00′
首先,您需要将“zeroDateTimeBehavior = convertToNull”添加到db connection 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; }
请注意,该字段应映射为可选字段。
最后,两个class级,第一个是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自定义UserType不起作用
- 如何解决“org.hibernate.QueryException:并未设置所有命名参数”错误?
- 在列上添加@Convert会使执行时间增加10倍以上
- Hibernate 4.3,在构建SessionFactory时为什么要提供两次属性?
- 使用引用列进行Hibernate一对一映射(XML映射)
- 如何在辅助表中的非主键列上连接表?
- Hibernate,spring,JPS和隔离 – 不支持自定义隔离
- 一次性向所有MySQL Select Queries添加一列
- JPA / Hibernate使用共享主键进行单向一对一映射
- ClassNotFoundException:org.hibernate.service.jndi.JndiException