Hibernate version:
3.2.1
I have two tables:
PARENT
--------------------------
PARENT_ID (PK)
.
. other fields
.
CHILD
-------------------------
PARENT_ID (FK)(PK)
INDEX (PK)
So child has a composite ID, a FK to its parent and and an integer for ordering and uniquely identifying the children.
Mapping documents:
Now the mapping for those two tables:
Code:
<class name="Parent" table="PARENT" lazy="true">
<id column="PARENT_ID" name="parentId" type="long">
<generator class="native" />
</id>
.
. Other properties....
.
<set name="children" inverse="true" cascade="merge,delete-orphan">
<key column="PARENT_ID" />
<one-to-many class="Child"/>
</set>
</class>
<class name="Child" table="CHILD" lazy="true">
<composite-id>
<key-many-to-one name="parent">
<column name="PARENT_ID" not-null="true" />
</key-many-to-one>
<key-property name="index" type="long">
<column name="INDEX" />
</key-property>
</composite-id>
<many-to-one name="contact" cascade="merge" insert="false" update="false">
<column name="PARENT_ID" not-null="true" />
</many-to-one>
<property name="index" type="long" column="INDEX" insert="false" update="false"/>
</class>
The Problem
We have a object graph of a parent + children.
We are using merge (on the parent, which cascades to the children) to associate the objects in the graph with the session.
The trouble is this: If the parent and children are both transient (new), then the parent's ID
has not yet been generated. When we attempt to merge the parent and its children into the
session, Hibernate generates a null pointer exception when merging the children because their
parent's ID is null and hasn't yet been generated by hibernate. We can work around this by
using explicitly assigning the parent's ID but we expect that this case should work with hibernate
generating the ID for the parent.
We have opened a bug with an example of the problem
http://opensource.atlassian.com/project ... e/HHH-2326
Anybody have a legitimate workaround that does not require the explicit assignment of the parent ID before merge?
Thanks.
Tyler Van Gorder
tkv@landacorp.com
Landacorp.