小编典典

休眠自定义用户类型不起作用

hibernate

我已经创建了一个UserType(请参见下文)来处理mySQL数据库中的一种情况,在该情况下我们将空日期保存为0000-00-00 00:00:00。

当我尝试使用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;
}
}

阅读 207

收藏
2020-06-20

共1个答案

小编典典

您的问题不在于您的UserType,而是您已将属性声明为非空(使用@Basic
optional =“ false”),而您却将其设置为null。

就是说,我在返回deepCopy / assemble / disassemble方法中的原始值时要小心。java.util.Date
可变的,您可能正在那里麻烦。

2020-06-20