Hey all, I have spent lots of time trying to figure this out with no luck, any help would be appreciated.
I am getting a ClassCastException when trying to run this query yet, I have checked the getters and setters and they seem fine to me.. maybe I just need a new pair of eyes. QuestionId.? doesn't make sense to me..
Hibernate version: 3.0
Mapping documents:
Code:
<hibernate-mapping>
<class name="Question" table="st.tblquestion">
<composite-id name="id" class="QuestionId">
<key-property name="job" type="integer" column="JOB" />
<key-property name="questionNumber" type="string" column="QUENUMBER" />
</composite-id>
<properties name="tlkpKey" unique="true" insert="false" update="false">
<property name="tlkJob" unique="true" type="integer" insert="false" update="false" column="JOB"/>
<property name="sclName" unique="true" type="string" insert="false" update="false" column="SCLNAME"/>
</properties>
<property name="sclName" type="string" length="15" column="SCLNAME" />
<set name="tlkpScale" inverse="true" fetch="join" table="webquest.tlkpscale" lazy="false" cascade="all,delete-orphan">
<key property-ref="tlkpKey">
<column name="JOB" />
<column name="SCLNAME" />
</key>
<one-to-many class="TlkpScale" />
</set>
</class>
</hibernate-mapping>
TlkpScale mapping just incase:
Code:
<hibernate-mapping>
<class name="TlkpScale" table="st.TLKPSCALE" >
<composite-id name="id" class="TlkpScaleId">
<key-property name="job" type="integer" column="JOB"/>
<key-property name="sclName" type="string" column="SCLNAME"/>
<key-property name="sclPunch" type="integer" column="SCLPUNCH"/>
</composite-id>
<many-to-one name="question" class="Question">
<formula>JOB</formula>
<formula>QUENUMBER</formula>
</many-to-one>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Criteria crit = session.createCriteria(Question.class);
crit.add(Restrictions.eq("id.questionNumber", itmKey));
crit.add(Restrictions.eq("id.job", job));
Question result = (Question) crit.uniqueResult();
The Question class:
Code:
public class Question implements Serializable{
private static final long serialVersionUID = -3282425477491286221L;
private QuestionId id;
private Integer tlkJob;
private String sclName;
private Set<TlkpScale> tlkpScale;
public Set getTlkpScale() {
return tlkpScale;
}
public void setTlkpScale(Set<TlkpScale> tlkpScale) {
this.tlkpScale = tlkpScale;
}
public QuestionId getId() {
return id;
}
public void setId(QuestionId questionId) {
this.id = questionId;
}
public int getJob() {
return this.getId().getJob();
}
public void setJob(int job) {
this.getId().setJob(job);
}
public Integer getTlkJob() {
return this.tlkJob;
}
public void setTlkJob(Integer tJob) {
this.tlkJob = tJob;
}
public String getSclName() {
return sclName;
}
public void setSclName(String sclName) {
this.sclName = sclName;
}
}
The QuestionId class:
Code:
public class QuestionId implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1298983779171835820L;
private int job;
private String questionNumber;
public int getJob() {
return job;
}
public void setJob(int job) {
this.job = job;
}
public String getQuestionNumber() {
return questionNumber;
}
public void setQuestionNumber(String questionNumber) {
this.questionNumber = questionNumber;
}
public boolean equals(Object object) {
if(this == object)
return true;
if(object == null || (object.getClass() != this.getClass()))
return false;
QuestionId myObj = (QuestionId) object;
return ((job == myObj.job)
&& (questionNumber == null ? myObj.questionNumber == null : questionNumber.equals(myObj.questionNumber)));
}
public int hashCode() {
int hash = 7;
int varCode = 0;
varCode = (int) job;
hash = 31 * hash + varCode;
varCode = (null == questionNumber ? 0 : questionNumber.hashCode());
hash = 31 * hash + varCode;
return hash;
}
public String toString() {
return "QuestionId: job [" + job + "], questionNumber ["
+ questionNumber + "]";
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
}
The TlkpScale class:
Code:
public class TlkpScale {
private TlkpScaleId id;
private Question question;
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
protected TlkpScaleId getId() {
return id;
}
protected void setId(TlkpScaleId id) {
this.id = id;
}
}
TlkpScaleId class:
Code:
public class TlkpScaleId implements Serializable {
private static final long serialVersionUID = -5408106562584876769L;
private int job;
private String sclName;
private int sclPunch;
public int getJob() {
return job;
}
public void setJob(int job) {
this.job = job;
}
public String getSclName() {
return sclName;
}
public void setSclName(String sclName) {
this.sclName = sclName;
}
public int getSclPunch() {
return sclPunch;
}
public void setSclPunch(int sclPunch) {
this.sclPunch = sclPunch;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || !(object instanceof TlkpScaleId)) {
return false;
}
TlkpScaleId myObj = (TlkpScaleId) object;
return ((sclName.equals(myObj.sclName)) && (sclPunch == myObj.sclPunch) && (job == myObj.job));
}
public int hashCode() {
int hash = 7;
int varCode = 0;
varCode = (null == sclName ? 0 : sclName.hashCode());
hash = 31 * hash + varCode;
varCode = (int) sclPunch;
hash = 31 * hash + varCode;
varCode = (int) job;
hash = 31 * hash + varCode;
return hash;
}
}
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 QuestionId.?
at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:89)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:307)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:158)
at org.hibernate.engine.EntityKey.generateHashCode(EntityKey.java:103)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:47)
at org.hibernate.engine.StatefulPersistenceContext.getCollectionOwner(StatefulPersistenceContext.java:644)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:980)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:635)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:755)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:229)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)
at dao.HibernateReportPushDAO.getQuestion(HibernateReportPushDAO.java:422)
at SampleValuesTest.testQuestion(SampleValuesTest.java:18)
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 org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:552)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:407)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:778)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105)
at org.testng.TestRunner.privateRun(TestRunner.java:682)
at org.testng.TestRunner.run(TestRunner.java:566)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:220)
at org.testng.SuiteRunner.run(SuiteRunner.java:146)
at org.testng.eclipse.runner.RemoteTestNG.run(RemoteTestNG.java:98)
at org.testng.eclipse.runner.RemoteTestNG.main(RemoteTestNG.java:138)
Caused by: java.lang.ClassCastException: Question
at QuestionId$$BulkBeanByCGLIB$$fbd46587.getPropertyValues(<generated>)
at net.sf.cglib.beans.BulkBean.getPropertyValues(BulkBean.java:48)
at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:86)
... 41 more
The generated SQL (show_sql=true):
[code]
Hibernate:
select
this_.JOB as JOB11_0_,
this_.QUENUMBER as QUENUMBER11_0_,
this_.SCLNAME as SCLNAME11_0_,
from
st.tblquestion this_
where
(
this_.JOB=?
and this_.QUENUMBER=?
)
Hibernate:
select
tlkpscale0_.JOB as JOB1_,
tlkpscale0_.SCLNAME as SCLNAME1_,
tlkpscale0_.SCLPUNCH as SCLPUNCH1_,
tlkpscale0_.JOB as JOB23_0_,
tlkpscale0_.SCLNAME as SCLNAME23_0_,
tlkpscale0_.SCLPUNCH as SCLPUNCH23_0_,
from
st.TLKPSCALE tlkpscale0_
where
tlkpscale0_.JOB=?
and tlkpscale0_.SCLNAME=?
[/code]