-->
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.  [ 2 posts ] 
Author Message
 Post subject: Composite keys, one-to-many mappings and ClassCastExceptions
PostPosted: Wed Mar 15, 2006 3:12 pm 
Newbie

Joined: Wed Mar 15, 2006 2:37 pm
Posts: 6
Hi,

I have been having problems with a one-to-many relationship with a composite id and was unable to get it to work until I introduced an id which was a concatentation of the columns in the composite key. Ideally I'd like the composite-id to work and was hoping some could explain what is gonig on.

Here are the mapping files that fail:
Code:
     <!-- Parent -->
    <class name="com.ingenuity.model.CannonicalPathway" table="MAGB_PATHWAY">

        <id name="id" column="PATHWAY_DURABLE_ID" type="java.lang.String">
            <generator class="assigned"/>
        </id>

        <set name="references" lazy="false" cascade="none" sort="unsorted">
            <key column="PATHWAY_DURABLE_ID"/>
            <one-to-many class="com.ingenuity.model.CannonicalPathwayReference"/>
        </set>
    </class>

    <!-- Child -->
    <class name="com.ingenuity.model.CannonicalPathwayReference" table="MAGB_PATHWAY_REFERENCE">

        <composite-id>
            <key-property name="pathwayId" type="java.lang.String" column="PATHWAY_DURABLE_ID"/>
            <key-property name="pubmedId" type="java.lang.String" column="PUBMED_ID"/>
            <key-property name="reference" type="java.lang.String" column="REFERENCE"/>
        </composite-id>

        <property name="publicationDate" type="java.lang.String" column="PUBLICATION_DATE"/>

    </class>



And here the mapping files that work:
Code:
    <!-- Same parent as above -->
    <class name="com.ingenuity.model.CannonicalPathway" table="MAGB_PATHWAY">

        <id name="id" column="PATHWAY_DURABLE_ID" type="java.lang.String">
            <generator class="assigned"/>
        </id>

        <set name="references" lazy="false" cascade="none" sort="unsorted">
            <key column="PATHWAY_DURABLE_ID"/>
            <one-to-many class="com.ingenuity.model.CannonicalPathwayReference"/>
        </set>
    </class>

    <!-- Child is modified to use ugly composite key -->
    <class name="com.ingenuity.model.CannonicalPathwayReference" table="MAGB_PATHWAY_REFERENCE">
       
        <id name="key" column="PATHWAY_DURABLE_ID||PUBMED_ID||REFERENCE">
            <generator class="assigned"/>
        </id>

        <property name="pathwayId" type="java.lang.String" column="PATHWAY_DURABLE_ID" />
        <property name="reference" type="java.lang.String" column="REFERENCE" />
        <property name="pubmedId" type="java.lang.String" column="PUBMED_ID" />
        <property name="publicationDate" type="java.lang.String" column="PUBLICATION_DATE" />

    </class>


Using the first set of mapping files results in the following class cast exception:
Code:
java.lang.ClassCastException: com.ingenuity.model.CannonicalPathwayReference
   at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:759)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:292)
   at org.hibernate.loader.Loader.doQuery(Loader.java:412)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
   at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
   at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:280)
   at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:796)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
   at org.hibernate.loader.Loader.doList(Loader.java:1593)
   at org.hibernate.loader.Loader.list(Loader.java:1577)
   at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1322)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
   at org.springframework.orm.hibernate3.HibernateTemplate$5.doInHibernate(HibernateTemplate.java:452)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:311)
   at org.springframework.orm.hibernate3.HibernateTemplate.loadAll(HibernateTemplate.java:448)


Any ideas why this should occur? (Hibernate 3.0.5, Oracle 9.2.0.4)


Last edited by kiml on Thu Mar 16, 2006 11:52 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 10:50 pm 
Newbie

Joined: Wed Mar 15, 2006 3:15 pm
Posts: 1
I'm having a problem very similar to this where composite keys give me a ClassCastException.

Anyone have any ideas?


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