我正在使用Hibernate的JPA来建模一些表。我在映射一张表时遇到了困难:
。
我试图破解它并将索引定义为一个复合Id,但是由于某些列是可空的,所以它不能正常工作。这在JPA/Hibernate中是可能的吗?
谢谢
发布于 2009-05-19 10:24:04
周围的工作是..。您应该实现自己的UserType实现,并将空值处理为返回具有代表性的对象。
看看我的例子。该字段是一个可空的数字,所以我的实现是:
在HBM文件中的样子
<key-property name="fieldName" type="mypackage.forUserTypes.DefaultLongType">
<column name="FIELD_NAME" precision="10" scale="0" />
</key-property>
...In
public class DefaultLongType implements UserType {
private static final long serialVersionUID = 1L;
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return null;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) throws HibernateException {
return null;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object value) throws HibernateException {
return null;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) return true;
if (x == null) return false;
return x.equals(y);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object x) throws HibernateException {
return x == null ? 0 : x.hashCode();
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
final long value = rs.getLong(names[0]);
if (rs.wasNull()) {
return new Long(Long.MIN_VALUE);
}
return new Long(value);
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
Long l = (Long) value;
if (l == null || l.longValue() == Long.MIN_VALUE) {
st.setNull(index, Types.NUMERIC);
}
else {
st.setLong(index, l.longValue());
}
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return Long.class;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
final int[] args = { Types.NUMERIC };
return args;
}}
发布于 2009-03-23 17:06:34
看来,不可空的列应该是您的主键。组合键的任何部分都不应为空。
您将需要接受可空属性,并将它们放置在主/复合键之外。
而且,这看起来像Hibernate mapping a composite key with null values的一个副本,当我在googled上搜索"null复合键“时,它作为#3出现了。
https://stackoverflow.com/questions/674104
复制相似问题