On 30 November 2011 17:12, costa wrote:
> I my test case, in one session, I create the children (new records),
> then persist the data, then I try to remove the children. The test is
> trying to simulate different operations which normally get executed
> one at the time in one request. Removing the children fails with the
> dreaded 'deleted object would be re-saved by cascade' error.

Your problem sounds somewhat like problems I've seen, and maybe the
solutions are the same.
(But it is very possible that our problems are only superficially
similar, and so I should have kept quiet rather than suggest a
non-sense solution, sorry if that turns out to be the case.)

1) In one session, child objects were added to a Lazy=true,
Inverse=true collection, and then removed (without being persisted,
AFAIK), and TransientObjectException was thrown when the session was
flushed.

A workaround was to initialise the collection before the child objects
were added, using either :

NHibernate.NHibernateUtil.Initialize(collection);
... or...
collection.FirstOrDefault();

2) Deleting child objects caused a 'deleted object would be re-saved
by cascade' or related exception.
The bug was that the cascade on the child object many-to-one
association was set incorrectly.
It was set to Cascade.All, which caused deletion of the child to
cascade into a delete of the parent.  The parent was still referenced
by other non-deleted objects, causing an exception.

The fix was to use the correct Cascade.SaveUpdate options (rather than
All) on the child object's many-to-one association.

I hope these help, otherwise sorry for the mailing group noise!

Mark

-- 
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.

Reply via email to