I'm having trouble getting updates working in nHibernate for a Parent/Child relationship (where the Child's lifecycle is dependent on the Parent). Save, Delete, and retrieve-by-id work but Update results in: "Illegal attempt to associate a collection with two open sessions". I 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 b/c FK_ParentID in tblChild has a NOT NULL constraint) and then tried different cascade options ("all", "all-delete-orphan", "save-update") and received the same error. I then removed the cascade option and did the update manually and I get the same exception.
Here's the code I used when I tried the different cascade option:
// Get Parent with 2 Child objects
Parent parent = session.Get(typeof(Parent), iID);
... // update some fields
session.SaveOrUpdate(parent); // update is done b/c IDs are filled in
session.Flush();
Here's the code I used when I removed the cascade option and did things manually:
ITransaction transaction = session.BeginTransaction();
// Get Parent with 2 Child objects
Parent parent = session.Get(typeof(Parent), iID);
session.SaveOrUpdate(parent);
// Update Child objects
if (propertyTemplate.Filters != null)
{
foreach (Child child in parent.Children)
{
session.SaveOrUpdate(child);
}
}
transaction.Commit();
session.Flush();
Here are the simplified class mappings - not showing the cascade option:
<class name="Parent, Domain" table="tblParent">
<bag name="Children" outer-join="true" inverse="true">
<key column="FK_ParentID"/>
<one-to-many class="Child, Domain"/>
</bag>
</class>
<class name="Child, Domain" table="tblChild">
<many-to-one
name="PParent"
column="FK_ParentD"
class="Parent, Domain"
not-null="true"
/>
</class>
Thanks,
Bill
_________________ metazone
|