-->
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: Bi-directional mapping against a subclass
PostPosted: Thu Aug 17, 2006 3:45 pm 
Newbie

Joined: Thu Aug 17, 2006 3:19 pm
Posts: 1
Hibernate version:3.0

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.tibco.pvm.pve.impl.PveObjectGraphImpl" table="OBJECT_GRAPH">

<id name="perId"
column="object_graph_id"
length="32">
<generator class="uuid.hex"/>
</id>

<discriminator column="graphType"/>

<property name="isPrototype"/>
<many-to-one name="processRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveProcessRef"
cascade="save-update"/>

<list name="attributeRefs" cascade="all">
<key>
<column name="OBJECT_ATTR_REF"/>
</key>
<list-index column="OBJECT_ATTR_REF_INDEX"/>
<one-to-many class="com.tibco.pvm.pve.dao.objectref.impl.PveAttributeRef"/>
</list>

<list name="blobRefs" cascade="all">
<key>
<column name="OBJECT_BLOB_REF"/>
</key>
<list-index column="OBJECT_BLOB_REF_INDEX"/>
<one-to-many class="com.tibco.pvm.pve.dao.objectref.impl.PveBlobRef"/>
</list>


<subclass name="com.tibco.pvm.pve.impl.PveProcessGraphImpl" discriminator-value="processGraph">
<many-to-one name="objectId" class="com.tibco.pvm.pve.impl.PveProcessId"
cascade="save-update"/>

<many-to-one name="processData" class="com.tibco.pvm.pve.impl.PveProcessDataImpl"
not-null="false" cascade="all"/>

<many-to-one name="rootTaskRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"
cascade="save-update"/>

</subclass>

<subclass name="com.tibco.pvm.pve.impl.PveTaskGraphImpl" discriminator-value="taskGraph">
<many-to-one name="objectId" class="com.tibco.pvm.pve.impl.PveTaskId"
cascade="save-update"/>

<many-to-one name="taskData" class="com.tibco.pvm.pve.impl.PveTaskDataImpl"
not-null="false" cascade="all"/>

<many-to-one name="parentRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"
column="tParentRef" cascade="save-update"/>

<many-to-one name="successorRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"
cascade="save-update"/>

<list name="taskRefs" cascade="all">
<key>
<column name="CHILD_TASK_REF"/>
</key>
<list-index column="CHILD_TASK_REF_INDEX"/>
<one-to-many class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"/>
</list>

<list name="linkRefs" cascade="all">
<key>
<column name="CHILD_LINK_REF"/>
</key>
<list-index column="CHILD_LINK_REF_INDEX"/>
<one-to-many class="com.tibco.pvm.pve.dao.objectref.impl.PveLinkRef"/>
</list>
</subclass>

<subclass name="com.tibco.pvm.pve.impl.PveLinkGraphImpl" discriminator-value="linkGraph">
<many-to-one name="objectId" class="com.tibco.pvm.pve.impl.PveLinkId"
cascade="save-update"/>

<many-to-one name="linkData" class="com.tibco.pvm.pve.impl.PveLinkDataImpl"
not-null="false" cascade="all"/>

<many-to-one name="parentRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"
column="lParentRef" cascade="save-update"/>

<many-to-one name="fromTaskRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"
cascade="save-update"/>

<many-to-one name="toTaskRef" class="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef"
cascade="save-update"/>
</subclass>

<subclass name="com.tibco.pvm.pve.impl.PveAttributeGraphImpl" discriminator-value="attributeGraph">
<many-to-one name="objectId" class="com.tibco.pvm.pve.impl.PveAttributeId"
cascade="save-update"/>

<many-to-one name="attributeData" class="com.tibco.pvm.pve.impl.PveAttributeDataImpl"
not-null="false" cascade="all"/>
</subclass>

<subclass name="com.tibco.pvm.pve.impl.PveBlobGraphImpl" discriminator-value="blobGraph">
<many-to-one name="objectId" class="com.tibco.pvm.pve.impl.PveId"
cascade="save-update"/>

<many-to-one name="blobData" class="com.tibco.pvm.pve.impl.PveBlobDataImpl"
not-null="false" cascade="all"/>
</subclass>


</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.tibco.pvm.pve.dao.objectref.impl.PveObjectRefAbstract" table="OBJECT_REF">
<id name="perId"
column="object_ref_id"
length="32">
<generator class="uuid.hex"/>
</id>

<discriminator column="refType"/>

<!--<set name="tasks" inverse="true" cascade="save-update">-->
<!--<key column="per_id"/>-->
<!--<one-to-many class="com.tibco.pvm.pve.impl.PveTaskGraphImpl"/>-->
<!--</set>-->

<!--<set name="processes" inverse="true" cascade="save-update">-->
<!--<key column="per_id"/>-->
<!--<one-to-many class="com.tibco.pvm.pve.impl.PveProcessGraphImpl"/>-->
<!--</set>-->

<!--<set name="links" inverse="true" cascade="save-update">-->
<!--<key column="per_id"/>-->
<!--<one-to-many class="com.tibco.pvm.pve.impl.PveLinkGraphImpl"/>-->
<!--</set>-->

<subclass name="com.tibco.pvm.pve.dao.objectref.impl.PveBlobRef" discriminator-value="blobref">
<many-to-one name="blobId" class="com.tibco.pvm.pve.impl.PveBlobId" column="BLOB_ID"
cascade="save-update"/>
</subclass>

<subclass name="com.tibco.pvm.pve.dao.objectref.impl.PveAttributeRef" discriminator-value="attributeref">
<many-to-one name="attributeId" class="com.tibco.pvm.pve.impl.PveAttributeId"
column="ATTRIBUTE_ID" cascade="save-update"/>
</subclass>

<subclass name="com.tibco.pvm.pve.dao.objectref.impl.PveLinkRef" discriminator-value="linkref">
<many-to-one name="linkId" class="com.tibco.pvm.pve.impl.PveLinkId" column="LINK_ID"
cascade="save-update"/>
</subclass>

<subclass name="com.tibco.pvm.pve.dao.objectref.impl.PveProcessRef" discriminator-value="processref">
<many-to-one name="processId" class="com.tibco.pvm.pve.impl.PveProcessId" column="PROCESS_ID"
cascade="save-update"/>

<set name="taskProcessRefs" inverse="true" cascade="save-update">
<key column="processRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveTaskGraphImpl"/>
</set>

<set name="attrProcessRefs" inverse="true" cascade="save-update">
<key column="processRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveAttributeGraphImpl"/>
</set>

<set name="linkProcessRefs" inverse="true" cascade="save-update">
<key column="processRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveLinkGraphImpl"/>
</set>

<set name="blobProcessRefs" inverse="true" cascade="save-update">
<key column="processRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveBlobGraphImpl"/>
</set>

</subclass>

<subclass name="com.tibco.pvm.pve.dao.objectref.impl.PveTaskRef" discriminator-value="taskref">
<many-to-one name="taskId" class="com.tibco.pvm.pve.impl.PveTaskId" column="TASK_ID"
cascade="none"/>

<set name="rootTaskRefs" inverse="true" cascade="save-update">
<key column="rootTaskRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveProcessGraphImpl"/>
</set>

<set name="toTaskRefs" inverse="true" cascade="save-update">
<key column="toTaskRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveLinkGraphImpl"/>
</set>

<set name="fromTaskRefs" inverse="true" cascade="save-update">
<key column="fromTaskRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveLinkGraphImpl"/>
</set>

<set name="successorTaskRefs" inverse="true" cascade="save-update">
<key column="successorRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveTaskGraphImpl"/>
</set>

<set name="tParentTaskRefs" inverse="true" cascade="save-update">
<key column="tParentRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveTaskGraphImpl"/>
</set>

<set name="lParentTaskRefs" inverse="true" cascade="save-update">
<key column="lParentRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveLinkGraphImpl"/>
</set>

</subclass>
</class>

</hibernate-mapping>



org.hibernate.WrongClassException: Object with id: 8ae1a45c0d1d4154010d1d416e8c0004 was not of the specified subclass: com.tibco.pvm.pve.impl.PveLinkGraphImpl (loaded object was of wrong class)
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1235)
at org.hibernate.loader.Loader.getRow(Loader.java:1186)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
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:71)
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:1695)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
at com.tibco.pvm.pve.dao.manager.DaoRepositoryPersistentImpl.deleteRef(DaoRepositoryPersistentImpl.java:240)
at com.tibco.pvm.pve.dao.manager.DaoRepositoryPersistentImpl.deleteGraph(DaoRepositoryPersistentImpl.java:208)
at com.tibco.pvm.pve.dao.manager.DaoRepositoryPersistentImpl.delete(DaoRepositoryPersistentImpl.java:149)
at com.tibco.pvm.pve.impl.PveProcessGraphImpl.delete(PveProcessGraphImpl.java:228)
at com.tibco.pvm.pve.impl.PveProcessAbstract.delete(PveProcessAbstract.java:128)
at test.tibco.pvm.pve.PveDelete.testDeleteProcess(PveDelete.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:32)




I'm trying to do inverse=true bi-directional mappings for processRef
which is defined for all subclasses. When I try to get the set related to

<set name="taskProcessRefs" inverse="true" cascade="save-update">
<key column="processRef"/>
<one-to-many class="com.tibco.pvm.pve.impl.PveTaskGraphImpl"/>
</set>

It returns all classes not just the one I'm interested. I took the SQL
I got from p6spy and added a "and graphType='linkGraph'" to the
query it works fine. How do I do a reverse mapping like this so that
it returns only the right subclass. I've looked all over and I find very
little documentation on subclasses in general, so if there is something
out there please point me in the right direction.

-Mike



Top
 Profile  
 
 Post subject: Re: Bi-directional mapping against a subclass
PostPosted: Fri Aug 18, 2006 1:41 am 
Newbie

Joined: Wed Aug 16, 2006 6:29 pm
Posts: 9
Suppose you have the following mappings based on database with tableA and tableB:
Code:
<class name="A" table="tableA">
  <set name="mySet">
    <key column="A.id"/>
    <one-to-many class="B"/>
  </set>
</class>

<class name="B" table="tableB">
  <discriminator column="type"/>
  <subclass name="B2" discriminator-value="B2"/>
  <subclass name="B3" discriminator-value="B3"/>
</class>


When you call a.getMySet() you get objects of the appropriate type - either B2 or B3.

Specifying B2 on the one-to-many doesn't change the nature of the join (that's controlled by the key). Instead it incorrectly asks for instances of B2 to be created where tableB can contain either B2 or B3. So I suggest keeping you one-to-many as class B.

If you want to load A with instances of only B2 or B3 try this:

Code:
String query =
  "from A a join fetch a.mySet b where a.id = :id and b.type = :type";

new HashSet(
  sess.createQuery(query).
  setInt("id", 1).
  setString("type", "B2").
  list());


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.