我正在使用Hibernate的JPA impl来为一些表建模。我无法映射表格:
我试图破解它并将索引定义为复合ID,但由于某些列可以为空,因此无法正常工作。这可能是JPA / Hibernate吗?
由于
答案 0 :(得分:1)
似乎不可为空的列应该是您的主键。复合键的任何部分都不应该是可空的。
您需要获取可为空的属性并将它们放在主/复合键之外。
此外,这看起来像Hibernate mapping a composite key with null values的副本,当我用Google搜索“null composite key”时,它出现了#3。
答案 1 :(得分:1)
work arround是......你应该实现自己的UserType实现并处理null值以返回一个代表性的Object。
看看我的榜样。该字段是可以为空的数字,因此我的实现是:
在HBM文件中看起来像:
<key-property name="fieldName" type="mypackage.forUserTypes.DefaultLongType">
<column name="FIELD_NAME" precision="10" scale="0" />
</key-property>
...在Java中:
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;
}
}