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)