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.  [ 3 posts ] 
Author Message
 Post subject: joined-subclass gives ClassCastException Randomly
PostPosted: Wed Oct 05, 2005 8:41 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
I have a class called PartyAssociation which has a many-to-one relation with a polymorphic class Party whose sub-classes (Person and Organization) are defined using
Code:
<joined-subclass>
. Party, Person and Organization share the same primary key i.e. PARTY_ID.

When I retrieve a PartyAssociation object using a query on the session and then invoke the method partyAssociation.getParty().getPartyId(), the behaviour is random.

Most of the time it is successful, but suddenly it starts throwing "ClassCastException". Note that all entities in the polymorphic association have proxy interfaces defined (PartyIntf, PersonIntf and OrganizationIntf). Are there any key things that i should look for?

I have made sure that the PARTY_ID does not exist in both the PERON table and the ORGANIZATION table.

Hibernate version: 3.0

Mapping documents:

Party Mapping

Code:

<class proxy="PartyIntf"
      name="Party" table="PARTY">
    <id name="partyId" column="PARTY_ID">
      <generator class="sequence">
          <param name="sequence">PARTY_ID_SEQ</param>
      </generator>
   </id>
   <property name="partyType" type="string">
     <column name="PARTY_TYPE" sql-type="VARCHAR2"/>
   </property>

   <set name="addresses" table="PARTY_ADDRESS" cascade="all" >
      <key column="PARTY_ID"    not-null="true"/>
      <one-to-many class="PartyAddress" />
   </set>
     <set name="registeredIds" table="PARTY_REGISTERED_IDS" cascade="all">
      <key column="PARTY_ID" />
      <one-to-many class="RegistrationIdentity" />
    </set>
    <set name="partyAssociations" inverse="true" cascade="all">
      <key column="PARTY_ID"/>
      <one-to-many class="PartyAssociation" />
    </set>
   
   <joined-subclass proxy="PersonIntf"
             name="Person" table="PERSON" >
      <key column="PARTY_ID"/>
      <property name="dateOfBirth" type="date" >
         <column name="DATE_OF_BIRTH" sql-type="DATE" />
      </property>
      <property name="employer" type="string">
         <column name="EMPLOYER_NAME" sql-type="VARCHAR2"/>
      </property>
    </joined-subclass>
   <joined-subclass proxy="OrganizationIntf"
           name="Organization" table="ORGANIZATION">
      <key column="PARTY_ID"/>

      <component name="orgName" class="OrganizationName">
         <property name="businessName" type="string">
            <column name="BUSINESS_NAME" sql-type="VARCHAR2"/>

         </property>      
      </component>
   </joined-subclass>
</class>




Party Association Mapping

Code:
<class name="PartyAssociation" table="PARTY_INSTITUTION_ASSOCIATION" lazy="false">
      
   <id name="associationId" column="UNIQUE_ASSOCIATION_ID">
      <generator class="sequence" >
         <param name="sequence">PARTY_ASSOCIATION_SEQ</param>
      </generator>
   </id>
      
   <many-to-one name="party"
      class="Party" column="party_id" />
   <many-to-one name="institution"
      class="Institution" column="institution_id" />
      
   <property name="associateId" type="integer">
      <column name="INSTITUTION_ASSOCIATION_ID" sql-type="NUMBER"/>
   </property>
</class>



Code between sessionFactory.openSession() and session.close():

Code:
StringBuffer query = new StringBuffer("from PartyAssociation as association ");
           
         query.append("where association.institution.institutionId = ").append(theInstitutionId)
         .append(" and association.associateId= ").append(theAssociationId);
           
            System.out.println("*********************************");           
           
         PartyAssociation partyAssociation =
                    (PartyAssociation)theHibernateSessionObject.createQuery(query.toString()).uniqueResult();


            PartyIntf party = partyAssociation.getParty();
            System.out.println("######################> " + party.getClass().getName());
[b]            System.out.println("######################> " + party.getPartyId());[/b]
            System.out.println("######################> " + party.getPartyType());
            System.out.println("######################> " + party.getPartyAssociations().size());
            System.out.println("*********************************");


Full stack trace of any exception that occurs:

Code:
java.lang.ClassCastException: com.vsoftcorp.coresoft.model.party.hibernate.Organization
   at com.vsoftcorp.coresoft.model.party.PersonIntf$$FastClassByCGLIB$$e9f9a0d.invoke(<generated>)
   at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:183)
   at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:137)
   at com.vsoftcorp.coresoft.model.party.PersonIntf$$EnhancerByCGLIB$$19344702.getPartyId(<generated>)
   at com.vsoftcorp.coresoft.model.party.hibernate.Parties.findPartyAssociation(Parties.java:112)
   at com.vsoftcorp.coresoft.command.GetCommercialCustomerCommand.perform(GetCommercialCustomerCommand.java:90)
   at test.com.vsoftcorp.coresoft.command.GetCommercialCustomerCommandTest.testGetGetCommercialCustomerCommand(GetCommercialCustomerCommandTest.java:66)
   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:474)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)




Name and version of the database you are using:

Oracle 10g


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 9:14 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
All subclasses must use the same proxy interface or can not be lazy in polymorfic associations.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 8:33 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
I wanted to maintain my interfaces and hence changed by query to the following:

Code:

PartyAssociation partyAssociation =
                (PartyAssociation) theHibernateSessionObject.createCriteria(PartyAssociation.class)
                    .setFetchMode("party", FetchMode.JOIN)
                    .createAlias("institution", "institution")
                    .add(Restrictions.and(
                        Restrictions.eq("institution.institutionId", theInstitutionId),
                        Restrictions.eq("associateId", new Integer(theAssociationId)))).uniqueResult();


I haven't hit the issue so far.

Thanks for the help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.