-->
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.  [ 6 posts ] 
Author Message
 Post subject: delete orphans and re-attached instances
PostPosted: Fri May 20, 2005 7:11 pm 
Newbie

Joined: Thu Apr 28, 2005 8:16 pm
Posts: 9
Any advice related to this topic would be appreciated.

An instance of class A is associated with a Set of class B instances (A:B is 1:n). In the following sequence, class B orphans are not deleted.

1. open Hibernate session
2. fetch instance of A
3. fetch associated B instances (e.g., a.getBs())
4. close hibernate session
5. replace original Set of B instances with new Set (e.g., a.setBs(new Set())
6. open another Hibernate session
7. re-attach A to Hibernate session
8. commit transaction

Orphaned B instances are not deleted. I think (?) this is because the original Set of B instances was dereferenced. Is there any way to dereference the original Set and still leverage the delete orphans facility?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 7:41 am 
Newbie

Joined: Mon Nov 14, 2005 7:07 am
Posts: 9
Did you got this problem solved? I have probably the same issue since i converts my entities to transfer objects for the gui and then back to entities.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 7:59 am 
Beginner
Beginner

Joined: Mon Oct 24, 2005 9:46 am
Posts: 22
Location: Germany
as far as my understanding of hibernate goes, hibernate replaces the Set with its own implementation to track some additional information. So i dont think, its possible to dereference the original Set. One possible workaround could be to just oldSet.clear and oldSet.addAll(newSet) :-/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 9:40 am 
Newbie

Joined: Mon Nov 14, 2005 7:07 am
Posts: 9
My problem has to do with the removal of children from a parent object.

From the parent i get a Set with children, this set is converted to an array of child entities.
This array of child entities is converted to a new object, a transfer object. So the children now is placed in an array with ChildTO. This object is presented in the gui and if all children where to be removed the method parentTO.setChildren(null) would be executed.
The ParentTO is converted to a entity and the childrenTO array to a set of chilren entities (which would be an empty Set).
Now this empty set wont get hibrenate to know that the children should be deleted from the database.
The next fins of the same parent would have the same children as when the cycle started.

The reason that the deletion breaks is probably because hibernate loses state of the children somewhere.
The question is how to preserve this state for hibrenate.
Of course, I could do a find() before the update and sync the Set in the database and the Set received from the gui but isnt there another way?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 11:06 am 
Beginner
Beginner

Joined: Mon Oct 24, 2005 9:46 am
Posts: 22
Location: Germany
thats somewhat difficult to point at something specific in your process, because there are so many potential places where the bug could hide ;-) providing some pseudo-code could also be helpfull.

however: to be more specific, it seems that hibernate looses the state of the parent-children-relationship, so the problem seems to reside in your synchronization of the original Set of your Parent-Object.

please check, if you somewhere dereference the original set. For example, just setting the parents set to your new children set or the like.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 11:24 am 
Newbie

Joined: Mon Nov 14, 2005 7:07 am
Posts: 9
One solution, somewhat ugly, might be to reload the parent/child from DB, do a parent.getChildren().clear() and pasernt.getChildren().addAll( theNewSetOfChildren)


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