-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Exception: "deleted object would be re-saved by cascade
PostPosted: Wed Nov 23, 2005 9:36 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
If I delete an entity and then flush the session, if the entity is referred to anywhere else in the session cache, I get

Code:
ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)


The code that's doing the delete doesn't know anything about the specific type of the entity being deleted, and there can be a lot of stuff in the session cache, so it would be major pain to somehow search through the session cache for other entities that refer to the one being deleted and clear the references (using Reflection?). Is it even possible to walk through the session cache like that?

I guess I could resort to requiring every entity to implement an interface defining a write-once "IsDeleted" property and a "Deleted" event. All entities with properties referencing other entities would have to subscribe/unsubscribe to the event when a referenced instance is associated/dissociated. An implementation of IInterceptor.OnDelete() could then set the IsDeleted property on the entity being deleted (if it implements the interface), and the deleted entity would then raise its event. Then the referring entities, listening to the event, could clear their references to the deleted object, freeing the deleted entity or any other code from that responsibility. Still sounds like a hassle though.

Does anyone else have this problem? Is it because my sessions live too long and have too much stuff in them (i.e. the "solution" is to always have sessions so short-lived and sparse that any code using them should know exactly what's in the cache?)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 9:26 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
IMO, whenever you have to delete an entity, you should find all associations/collections refering/containing the deleted entity and remove it
(this probably imply changing the way you use sessions...)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 2:00 pm 
Newbie

Joined: Thu Oct 27, 2005 11:22 am
Posts: 1
I have seen this occur when I do not remove an object from it's parent collection.

To give an example. Object OrderLine has a reference to Order, and Order has a collection of OrderLine's. If I want to delete an OrderLine, I have to remove it from the Order.OrderLine collection.

I have solved this with a design decission. I have a Save function on my DAL and I override this function for any objects that I need to perform this patern on. So in my case there is a generic Save(object obj) and a Save(OrderLine ord) that first removed the OrderLine from the Order.

Code:
function void Save(OrderLine line)
{
    line.Order.Remove(line);
    line.Order = null;           // Not sure this is needed, but better safe...

    session.SaveOrUpdate(line);
}


Top
 Profile  
 
 Post subject: How can you figure out where the offending association is?
PostPosted: Fri Jul 06, 2007 10:05 am 
Newbie

Joined: Tue Mar 08, 2005 12:24 pm
Posts: 9
Location: Boston
Like the original poster we have complex business logic on our server involving many objects. We encounter this issue periodically and it is sort of helpful; it indicates a real bug where we are saving inconsistent objects some of which have an item marked for deletion and other(s) which reference the deleted object.

The problem is that it is hard to tell which association is holding the non-deleted object, and which association caused the cascade delete.

Does anyone have tricks or tips for figuring this out?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.