I'm having problems getting the correct cascade option working for all CRUD (Create, Retrieve, Update, Delete) operations on a Parent/Child relationship. Save, Delete, and retrieve-by-id work but Update results in: "Illegal attempt to associate a collection with two open sessions". I've added inverse="true" to the Parent's Child collection and not-null="true" to the Child class mapping's Parent reference (to allow cascading deletes to work) and then tried different cascade options ("all", "all-delete-orphan", "save-update").
Parent has collection of Child; Child has reference to Parent. All Primary Keys are ID fields that are 'identity'. tblChild (db table) has FK_ParentID that is non-null. Following is the code (I showed each of the operations commented-out -- in testing, each one is performed by itself).
Parent parent = new Parent();
Child child = new Child();
child.ParentRef = parent;
parent.Children = child;
//session.SaveOrUpdate(parent); // save
//session.Get(Parent, parent); // retrieve-by-id
//session.Delete(parent); // delete
//session.SaveOrUpdate(parent); // update
session.Flush();
In the following, I changed cascade to "all", "all-delete-orphan", and "save-update" and in all cases save, retrieve-by-id, and delete work but update throws the exception:
<class name="Parent, Domain">
<bag name="Children" cascade="all" outer-join="true" inverse="true">
<key column="FK_ParentID"/>
<one-to-many class="Child, Domain"/>
</bag>
</class>
<class name="Child, Domain">
<many-to-one
name="ParentRef"
column="FK_ParentID"
class="Parent, Domain"
not-null="true"
/>
</class>
Thanks,
Bill
_________________ metazone
|