As the title says, basically what I want to do is to change a child's parent. But when I try to do so, I get the exception "ObjectDeletedException: deleted object would be re-saved by cascade(remove deleted object from associations)".
I've been googling for hours now but none of the solutions I found worked for me!!
These are my classes involved:
public class Parent: Entity
{
public virtual IList<Child> Children { get; set; }
}
public class Child: Entity
{
public virtual DateTime? CancellationDate { get; set; }
}
The class Entity is the one that has the Id property.
This is the parent's mapping (Parents.hbm.xml):
<bag name="Children" cascade="all-delete-orphan" table="Schema.Child" where="CancellationDate is null">
<key column="ParentID"/>
<one-to-many class="Namespace.Child"/>
</bag>
And this is the piece of code where I try to reasign the child to another parent:
foreach(Child c in Parent1.Children)
{
Parent2.Children.Add(c);
}
But then, the session.Flush()
throws the exception above.
I guess that the problem is that since the Child changed his parent, because of the cascade NHibernate would have to eliminate the Child, but then it has been reasigned to anothet parent so, again because of the cascade, it would have to re-save the Child.
I already tried changing the mapping, and removing the Child from the former parent's collection (before or after I assign it to the other parent) but none of those worked...
Any help would be very appreciated!!
Thanks!!