Jpa组合键可为空的列 [英] Jpa composite key nullable columns
问题描述
我使用Hibernate的JPA impl来模拟一些表格。我无法映射表格:
我试图破解它并将索引定义为复合标识,但是有些列可以为空,但这不能正常工作。这可能与JPA / Hibernate吗?
谢谢
work arround is ...你应该实现你自己的UserType实现,并将null值视为返回一个代表性的Object。
看看我的例子。该字段为空数字,因此我的实现是:
在HBM文件中看起来像:
< key-property name =fieldNametype =mypackage.forUserTypes.DefaultLongType>
< column name =FIELD_NAMEprecision =10scale =0/>
< / key-property>
...在Java中:
public class DefaultLongType implements UserType {
private static final long serialVersionUID = 1L;
$ b / *(非Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,java.lang.Object)
* /
public Object assemble(Serializable cached,Object owner)
throws HibernateException {
return null;
$ b $ *(非Javadoc)
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
* /
public Object deepCopy(Object value)抛出HibernateException {
return null;
$ b $ *(非Javadoc)
* @see org.hibernate.usertype.UserType#反汇编(java.lang.Object)
* /
public Serializable反汇编(Object value)抛出HibernateException {
return null;
$ b $ *(非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);
$ b $ *(非Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
* /
public int hashCode(Object x)抛出HibernateException {
return x == null? 0:x.hashCode();
$ b / *(非Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
* /
public boolean isMutable(){
return false;
$ b $ *(非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)
抛出HibernateException,SQLException {
final long value = rs .getLong(名称[0]);
if(rs.wasNull()){
return new Long(Long.MIN_VALUE);
}
return new Long(value);
$ b $ *(非Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,java.lang.Object,int )
* /
public void nullSafeSet(PreparedStatement st,Object value,int index)
抛出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());
$ b $ *(非Javadoc)
* @ 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;
$ b / *(非Javadoc)
* @see org.hibernate.usertype.UserType#returnedClass()
* /
public Class returnedClass(){
return Long.class;
$ b $ *(非Javadoc)
* @see org.hibernate.usertype.UserType#sqlTypes()
* /
public int [] sqlTypes(){
final int [] args = {Types.NUMERIC};
返回参数;
}
}
I'm using Hibernate's JPA impl to model some tables. I'm having trouble mapping a table that:
- Has no primary key
- Has a unique index on 4 columns, 3 of which can be nullable
I tried to hack it and define the index as a composite Id, but since some columns are nullable this is not working properly. Is this possible with JPA/Hibernate?
Thanks
A work arround is... You should implements your own UserType implementation and treats the null value to return a representative Object for this.
Look my example. The field is a nullable numeric, so my implementation is:
Looks like in HBM file:
<key-property name="fieldName" type="mypackage.forUserTypes.DefaultLongType">
<column name="FIELD_NAME" precision="10" scale="0" />
</key-property>
...In 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;
}
}
这篇关于Jpa组合键可为空的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!