-->
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.  [ 2 posts ] 
Author Message
 Post subject: Removing objects that are contained in dfferents collections
PostPosted: Thu Jan 25, 2007 12:34 pm 
Newbie

Joined: Mon Aug 07, 2006 7:14 pm
Posts: 16
[b]Hibernate version:3.1.3[/b]

Hello all.
I've persistent objects that are contained in different collections, also this objects can be retrieved independently with its DAO.
I saw in the Hibernate session that the same java object is in the differents collections that contain it. But when I delete this object with the DAO, It isn't automatically remove from the collections!!!, Why Hibernate behave in this way? is there a good reason for this?
In the other hand, if I remove the object from one collection, the same object still exists in other collections and independently as a entity in the hibernate session.
I think that Hibernate must remove all the references to a persistent objects when a remove method is called, from a DAO or from a persistent collection.
Is the only solution remove the persistent object from all collections and using the remove dao method for removing it as an entity?
isn`t there another better approach in Hibernate?

Anybody could help me with this issue?

thanks to all!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 4:55 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you map a colleciton with inverse="false" cascade="delete", then removing an object from a collection will also delete it. After you refresh() all other objects that have collections referring to the deleted object, your in-memory state will be the same as the DB state. This will achieve what you describe. In the case where you use the DAO to delete the object, in-memory collections referring to the object don't know that it has been deleted, so you still have to refresh() them to sync them up with the DB.

The reason that this is not done by default is that most of the time, if an object can exist on its own or in multiple collections, then removing it from one collection should not remove it from the database or from any other collections. I know that I don't want that behaviour. If a student is in multiple classes, why would he leave school (removed from all classes and deleted from school database) just because he drops out of one class? Not only do I find it counter-intuitive, hibernate already lets me achieve this by delete()ing the object and refresh()ing any objects that refer to it.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.