使用Hibernate的联合主键composite-id
如下:
有一个类A,在类B里面,所有的字段组成联合主键, 同时在里面定义了查询语句
<class name="com.test.A" table="T_BASE_SETTINGS">
<composite-id name="id" class="com.test.B">
<key-property name="name" type="com.text.util.DefaultStringType">
<column name="NAME"/>
</key-property>
<key-property name="value" type="com.text.util.DefaultStringType">
<column name="VALUE"/>
</key-property>
</composite-id>
</class>
<query name="GET_SURCHARGE_VALUE">
FROM com.test.A a
WHERE a.id.name =? and a.id.value=?
</query>
遇到的问题
1: 查询QUERY报错,发现是需要用name替换NAME, value替换VALUE,就是不要用COLUMN
2: 查出来的结果,虽然打log发现是有值,如:list.size() 为1 但是设置不到对象里面去, 就是因为有些字段在数据库里面是空的, 联合主键的就要保证每一个字段都不能为空,解决的办法就是自己实现string, 定义了一个类com.text.util.DefaultStringType 如下:
public class DefaultStringType implements UserType {
private static final String blankChart = "";
private static final int[] TYPES = new int[]{Types.VARCHAR};
/* (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 {
String copy = new String(value.toString());
return copy;
}
/* (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 new HashCodeBuilder().append(x).toHashCode();
}
/* (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 {
String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
return value == null ? "" : 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 {
if (value.equals(blankChart)) {
Hibernate.STRING.nullSafeSet(st, null, index);
} else {
Hibernate.STRING.nullSafeSet(st, value, index);
}
}
/* (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 String.class;
}
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return TYPES;
}
}