Java Hibernate自定义用户类型为';行不通
我创建了一个UserType(见下文)来处理mySQL数据库中的一种情况,其中我们将空日期保存为0000-00-00:00:00 当我尝试为dispDT持久化我的实体时(见下文),它会生成以下异常:“javax.persistence.PersistenceException:org.hibernate.PropertyValueException:notnull属性引用空值或临时值:myEntity.dispDT” 通过在MySQLTimeStampUserType中的每个方法中设置断点,我可以看到它调用了deepCopy方法,而从不调用nullSafeSet方法。我认为nuyllSafeSet方法的全部要点是允许我在持久化之前操作该值。我做错了什么 实体注释Java Hibernate自定义用户类型为';行不通,java,hibernate,usertype,Java,Hibernate,Usertype,我创建了一个UserType(见下文)来处理mySQL数据库中的一种情况,其中我们将空日期保存为0000-00-00:00:00 当我尝试为dispDT持久化我的实体时(见下文),它会生成以下异常:“javax.persistence.PersistenceException:org.hibernate.PropertyValueException:notnull属性引用空值或临时值:myEntity.dispDT” 通过在MySQLTimeStampUserType中的每个方法中设置断点,我可
@Basic(optional = false)
@Column(name = "disp_dt")
@Type(type = "mypackage.MySQLTimeStampUserType")
// @Temporal(TemporalType.TIMESTAMP)
private Date dispDt;
用户类型类
public class MySQLTimeStampUserType implements UserType {
private static final int[] SQL_TYPES = {Types.TIMESTAMP};
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 00:00:00 return null, else return the timestamp
Date result = null;
if (!resultSet.wasNull()) {
if (!resultSet.getString(names[0]).equals("0000-00-00 00: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 00:00:00" else save the timestamp
if (value == null) {
statement.setString(index, "0000-00-00 00:00:00");
} else {
statement.setTimestamp(index,(Timestamp) 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;
}
}
您的问题不在于您的用户类型,而在于您已将属性声明为NOTNULL(使用optional=“false”),但您却将其设置为null
也就是说,在deepCopy/assembly/disassembly方法中,我会小心返回原始值。java.util.Date是可变的,您可能会在那里自找麻烦。这里对这些细微差别进行了充分讨论:
日期和时间的准备就绪和有效解决方案:
感谢Preston的代码和ChssPly76的有用注释。数据库不允许此列为空值。相反,他们用“0000-00-00:00:00”来填充它。我认为UserType的目的是允许我将null转换为“0000-00-00:00:00”。我不是说数据库。您正在应用程序中将该属性设置为NULL;Hibernate对此犹豫不决,因为它被映射为optional=false。删除该声明,您就可以开始了—数据库在该列中不会为NULL,因为您的用户类型将用零替换它。