If I've read anything about cascading, people ALWAYS stated that if you use cascading you should also make the connection in your domain modell bi-directional. And if you do make your Domain Modell bi- directional this problem shouldn't occur.
So if you do: parent.Add(child) You should also do: child.Parent = parent; and if you remove: parent.Remove(child); child.Parent = null; Now it is obvious what happens if you move a child: parent.Remove(child); child.Parent = null; otherParent.Add(child); child.Parent = otherParent; On Sep 11, 3:11 pm, Markus Zywitza <[email protected]> wrote: > If you had a transient object model with a bidirectional > parent-child-relationship, could you reparent your child by only setting > child.Parent = newParent? > > Probably not. You had to ensure within the setter, that the old and new > parent's references get updated, too. If you do this, you can simply use > cascade=all-delete-orphan and all goes as expected. > Don't try to think relational, when you want persistence ignorance. > > -Markus > 2009/9/10 Paladin_za <[email protected]> > > > > > > > I think NHibernate is a great ORM, but I can't help but feel that in > > some cases the product forces me to manage persistence issues it could > > have dealt with itself. > > > My biggest gripe at the moment is the case of re-parenting. > > > For example, I have a parent object with a collection of child objects > > and I want to move one of the existing child objects to a different > > parent object. > > > From a SQL perspective this could be done with the simple update > > statement UPDATE ChildTable SET ParentId = DifferentParentId where > > ChildId = TheIDOfTheChildToMove. > > > But Nhibernate refuses to make this easy. From what I can read on the > > web, there are 2 solutions. > > > First solution. Change the HasMany mapping on the parent object to use > > CascadeAll. This lets re-parenting work fine, but breaks deletes. How > > do we typically delete an object in our models, we set the > > ChildObject.Parent reference to null and remove the ChildObject from > > the Parent's ChildObjects collection. Now NHibernate de-references the > > objects, but the data still remains in the database. Outcome = > > Developer has to do database programming by calling session.Delete. > > > Second solution. Change the HasMany mapping on the parent object to > > use CascadeAllDeleteOrphan. Delete works great now. As per the > > documentation. amazing. Except, re-parenting breaks. And we get the > > stupid object would be resaved exception. To "fix" this the developer > > must first "delete" the object from the session and then re-attach it > > to the new parent object. > > > The above solutions invariably require my domain model to have some > > knowledge of the underlying data access implementation. This flies in > > the face of persistence ignorance which Nhibernate champions in every > > other area. > > > Can someone please explain why this is even considered acceptable? > > > I can only hope that someone wiser than me has a solution to this.- Hide > > quoted text - > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
