-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: composite-id classcastexception
PostPosted: Wed Jul 12, 2006 5:56 pm 
Newbie

Joined: Tue Jul 11, 2006 12:09 pm
Posts: 7
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]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 6:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Have you tried it with the reflection optimizer turned off, as suggested by the exception?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 7:44 pm 
Newbie

Joined: Wed Nov 02, 2005 12:50 pm
Posts: 5
Shot in the dark here but....

Your QuestionId composite-id mapping.....

<key-property name="job" type="integer" column="JOB" />

Your QuestionId job property....

private int job;

Try changing the QuestionId job property from an int to an Integer?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 9:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's not it. "integer" is the hibernate typedef to the int primitive or Integer wrapper, so that mapping is totally fine.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:31 pm 
Newbie

Joined: Tue Jul 11, 2006 12:09 pm
Posts: 7
i believe that it is set to false in hibernate config file, changing true and false didn't seem to make any difference in the log.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That shouldn't be the case, it works usually. I guess you could override it at configuration (app startup) time, seeing as it's just for debugging. After you do Configuration.configure(), but before Configuration.buildSessionFactory(), use Configuration.setProperty("hibernate.cglib.use_reflection_optimizer", "false"). That should override everything else (becuase it's last).

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 9:21 am 
Newbie

Joined: Tue Jul 11, 2006 12:09 pm
Posts: 7
Yeah, I still got the same stack trace creating the session factory like this:

sf = new Configuration().configure().setProperty("hibernate.cglib.use_reflection_optimizer", "false").buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 10:14 am 
Newbie

Joined: Tue Jul 11, 2006 12:09 pm
Posts: 7
removing:
Code:
<set name="tlkpScale" inverse="true" table="st.tlkpscale" lazy="false" cascade="all,delete-orphan">
         <key property-ref="tlkpKey">
            <column name="JOB" />
            <column name="SCLNAME" />
         </key>
         <one-to-many class="TlkpScale" />
</set>


Will make it run fine, I imagine this is some kind of weird problem with property-ref, I know hibernate regards this as kind of an odd database mapping. If i get rid of property-ref and just use <key> it will run, of course I can't get any of the results I want that way though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 6:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Another shot-in-the-dark:

You could add more type information to the mappings. Your properties mapping says that job is integer and sclName is string, but neither the columns in the key (referring to the join table) nor the formulae in the many-to-one (referring to the TlkpScale entity) do. Maybe hibernate is guessing the wrong types for the columns?

Of course you could make the column pair into a CompositeUserType instead of distinct properties. That would likely help, too, but I couldn't swear to it.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 9:34 pm 
Newbie

Joined: Tue Jul 11, 2006 12:09 pm
Posts: 7
I changed the Question composite-id to a natural-id and now property-ref seems to be working fine.. Not quite sure why. I'm not quite sure the implications this will have with the overall functionality, as I'm not positive about what natural id does, but I'll continue testing.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 1:30 pm 
Newbie

Joined: Tue Jul 11, 2006 12:09 pm
Posts: 7
in case anyone was following, i actually solved the problem by removing class= in the composite-id tag, the new tag looks like:
Code:
<composite-id>
      <key-property name="job" type="integer" column="JOB"/>
      <key-property name="queNumber" type="string" column="QUENUMBER"/>
</composite-id>


Now everything is working as expected. Well there is a "Found shared references to a collection" error if i'm returning multiple questions but this is a seperate error i think i can solve, if need be i'll post a seperate topic.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.