我已經創建了一個UserType(見下文)來處理mySQL數據庫中的情況,我們已經將空日期保存爲0000-00-00 00:00:00。休眠自定義UserType不起作用
當我嘗試用dispDT的null(參見下文)持久化實體時,它會生成此異常:「javax.persistence.PersistenceException:org.hibernate.PropertyValueException:not-null屬性引用null或瞬態值:myEntity .dispDt「
通過在MySQLTimeStampUserType中的每個方法中設置一個斷點,我可以看到它調用deepCopy方法並從不調用nullSafeSet方法。我認爲nuyllSafeSet方法的全部重點是讓我在繼續使用之前操縱這個值。我究竟做錯了什麼?
實體批註
@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;
}
}
數據庫不允許此列爲空值。相反,他們一直使用「0000-00-00 00:00:00」來填充它。我認爲UserType的意義在於允許我將null轉換爲「0000-00-00 00:00:00」。 – Preston
我不是在談論數據庫。您在應用程序中將該屬性設置爲NULL;因爲它被映射爲可選= false,所以Hibernate在這方面很流行。刪除該聲明,你會很好 - 數據庫不會在該列中獲得NULL,因爲您的用戶類型將用零替換它。 – ChssPly76