How do I change a child's parent in NHibernate when cascade is delete-all-orphan?
Posted
by Daniel T.
on Stack Overflow
See other posts from Stack Overflow
or by Daniel T.
Published on 2010-05-04T08:59:25Z
Indexed on
2010/05/04
9:08 UTC
Read the original article
Hit count: 317
I have two entities in a bi-directional one-to-many relationship:
public class Storage
{
public IList<Box> Boxes { get; set; }
}
public class Box
{
public Storage CurrentStorage { get; set; }
}
And the mapping:
<class name="Storage">
<bag name="Boxes" cascade="all-delete-orphan" inverse="true">
<key column="Storage_Id" />
<one-to-many class="Box" />
</bag>
</class>
<class name="Box">
<many-to-one name="CurrentStorage" column="Storage_Id" />
</class>
A Storage
can have many Boxes
, but a Box
can only belong to one Storage
.
I have them mapped so that the one-to-many has a cascade of all-delete-orphan
.
My problem arises when I try to change a Box's Storage
. Assuming I already ran this code:
var storage1 = new Storage();
var storage2 = new Storage();
storage1.Boxes.Add(new Box());
Session.Create(storage1);
Session.Create(storage2);
The following code will give me an exception:
// get the first and only box in the DB
var existingBox = Database.GetBox().First();
// remove the box from storage1
existingBox.CurrentStorage.Boxes.Remove(existingBox);
// add the box to storage2 after it's been removed from storage1
var storage2 = Database.GetStorage().Second();
storage2.Boxes.Add(existingBox);
Session.Flush(); // commit changes to DB
I get the following exception:
NHibernate.ObjectDeletedException : deleted object would be re-saved by cascade (remove deleted object from associations)
This exception occurs because I have the cascade set to all-delete-orphan
. The first Storage
detected that I removed the Box
from its collection and marks it for deletion. However, when I added it to the second Storage
(in the same session), it attempts to save the box again and the ObjectDeletedException
is thrown.
My question is, how do I get the Box
to change its parent Storage
without encountering this exception? I know one possible solution is to change the cascade to just all
, but then I lose the ability to have NHibernate automatically delete a Box
by simply removing it from a Storage
and not re-associating it with another one. Or is this the only way to do it and I have to manually call Session.Delete
on the box in order to remove it?
© Stack Overflow or respective owner