I'm trying to setup a relationship where a composite class can have one of two parents. I have inadvertently created a situation where "one instance variable which is used by to two different properties, one on the superclass, one on the subclass" (thought I not sure which instance variable this is).
This causes the child to parent reference get set to null when an existing graph is retrieved from the DB and then saveOrUpdate is called with no changes made to the graph.
I can work around the issue if I disable the setParent method if the parent provided is null. But I'm not sure of the consequences of doing this neither in my code or in hibernate. Does work around effect performance.
Is it possible to get a bit of an explanation why this doesn't work and any suggestions on how I should refractor this would be greatly appreciated
Regards
Gary
I have the following:
Code:
Class hierarchy
==========
Model
|
---------
| |
Root NodeElem
|
-----------
| |
Node Leaf
Object Graph
========
root -> nodes
node -> nodeelems
All these relationships are bidirectional
Therefore a node's parent can either be a Root or a NodeElem.
Mapping file
========
<hibernate-mapping>
<class name="net.sf.hibernate.examples.Root" table="eg_root" dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="java.lang.String" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<version name="version" type="int" column="version"/>
<bag name="nodes" lazy="false" inverse="true" cascade="save-update">
<key column="root_id"/>
<one-to-many class="net.sf.hibernate.examples.Node"/>
</bag>
</class>
<class name="net.sf.hibernate.examples.NodeElem" table="eg_nodeelem" dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="java.lang.String" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<version name="version" type="int" column="version"/>
<many-to-one name="parentNodeElem" column="node_id" not-null="false" cascade="save-update"/>
<joined-subclass name="net.sf.hibernate.examples.Node" table="eg_node" dynamic-update="false" dynamic-insert="false">
<key column="id"/>
<many-to-one name="parentRoot" column="root_id" not-null="false" cascade="save-update"/>
<bag name="nodeElem" table="NodeElem" lazy="false" inverse="true" cascade="save-update">
<key column="node_id"/>
<one-to-many class="net.sf.hibernate.examples.NodeElem"/>
</bag>
</joined-subclass>
<joined-subclass name="net.sf.hibernate.examples.Leaf" table="eg_leaf" dynamic-update="false" dynamic-insert="false">
<key column="id"/>
<property name="name" type="java.lang.String" update="true" insert="true" column="name"/>
</joined-subclass>
</class>
</hibernate-mapping>
[/code]