composite-id and normal id property conflicted in Criteria Query
I used composite-id as the unique identifier of POJO,and Role class
has another field named as 'id'.When I create a Criteria query,and set 'id' to query,hibernate will generated error sql statement which use composite-id as parameter.
Hibernate version:3.03
Java code:
Code:
package test.enkisoft.db;
import java.io.Serializable;
public class OID implements Serializable, Comparable
{
long m_uid;
String m_qName;
public long getUid() {
return m_uid;
}
public void setUid(long uid) {
m_uid = uid;
}
public String getQName() {
return m_qName;
}
public void setQName(String name) {
m_qName = name;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return (int)(m_uid ^ m_uid >> 32);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(final Object other) {
if (this == other) {
return true;
}
if (!(other instanceof OID)) {
return false;
}
OID castOther = (OID)other;
return m_uid == castOther.m_uid;
}
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(final Object other) {
OID castOther = (OID)other;
return (int)(m_uid - castOther.m_uid);
}
}
Code:
package test.enkisoft.db;
public class Role
{
private OID oid;
private java.lang.String id;
private java.lang.String name;
public OID getOID() {
return oid;
}
public void setOID(OID oid) {
this.oid = oid;
}
public String getId() {
return id;
}
public void setId(String value) {
id = value;
}
public java.lang.String getName() {
return name;
}
public void setName(String value) {
name = value;
}
}
Mapping documents: Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.enkisoft.db.Role" table="TB_Role" lazy="false" >
<composite-id name="OID">
<key-property name="uid">
<column name="UID_" index="id_IDX" not-null="true" unique="true" />
</key-property>
<key-property name="QName">
<column name="REC_TYPE_" not-null="true" />
</key-property>
</composite-id>
<property name="id">
<column name="id" unique-key="pk_IDX" not-null="true" />
</property>
<property name="name" >
<column name="name" not-null="true" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
// create role
OID oid = new OID();
oid.setUid(100001);
oid.setQName("users\role");
Role role = new Role();
role.setOID(oid);
role.setId("Administrator");
role.setName("Administrator");
Session sess = openSession();
Transaction tx = tx = sess.beginTransaction();
sess.save(role);
tx.commit();
//query
Criteria query = sess.createCriteria(Role.class);
query.add(Restrictions.eq("id", "Administrator"));
query.list();//throw exception here
sess.close();
Full stack trace of any exception that occurs:Code:
org.hibernate.PropertyAccessException: exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of test.enkisoft.db.OID.?
at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:42)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:257)
at org.hibernate.type.ComponentType.nullSafeGetValues(ComponentType.java:230)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:188)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1042)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1104)
at org.hibernate.loader.Loader.doQuery(Loader.java:365)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
at org.hibernate.loader.Loader.doList(Loader.java:1515)
at org.hibernate.loader.Loader.list(Loader.java:1498)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1253)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
at test.enkisoft.db.HibernateTest.testFindRole(HibernateTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.ClassCastException: java.lang.String
at test.enkisoft.db.OID$$BulkBeanByCGLIB$$78aaf107_2.getPropertyValues(<generated>)
at net.sf.cglib.beans.BulkBean.getPropertyValues(BulkBean.java:48)
at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:39)
... 28 more
Name and version of the database you are using:HSQL DB 1.73
The generated SQL (show_sql=true)::Code:
select this_.UID_ as UID1_0_, this_.REC_TYPE_ as REC2_0_, this_.id as id0_0_, this_.name as name0_0_ from TB_Role this_ where (this_.UID_=? and this_.REC_TYPE_=?)
Debug level Hibernate log excerpt:Code:
Code: