-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate: Moving Entities Between Collections
PostPosted: Thu Nov 15, 2007 3:41 pm 
Newbie

Joined: Thu Nov 15, 2007 1:36 pm
Posts: 12
Location: Ahsburn, VA
Is it possible to move an entity from one collection to another?

I have two top level entities A and B which both have one-to-many references to another entity C, which happens to have its own network of stuff below it.

Cascade deletes are working fine - if I delete C, it and everything below it goes away. Also, if I delete A or B, then it and its children also go away. So far so good, and I want to keep this cascading behavior.

Now the interesting part.

I want to take C, remove it from A's one-to-many collection and add it to B's one-to-many collection, preserving the object network under C.

a.getChildren().remove( c );
c.setParent(null);
b.getChildren().add(c);
c.setParent( b );

I get errors like "Found two representations of same collection" the moment I do the add().

Image


I'd think this would be a trivial reassignment of some foreign keys, but there seems to be more to the story that I'm missing.

Does Hibernate assume remove() is a delete? The object c is not being orphaned.

Thanks,
-Walt Stoneburner | Hibernate 3.2.5 / Hibernate Annotations 3.3.0.GA


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 15, 2007 4:15 pm 
Beginner
Beginner

Joined: Mon Feb 19, 2007 4:22 am
Posts: 22
Location: Poland
Do you use 'delete-orphan' in cascade attribute of these collections? If so, Hibernate will issue 'delete' to object once it has been removed from this collection. No matter if you assign another parent to it.

There has already been a topic about it on this forum, I will give link if I find it. I quote from memory: "delete-orphan is a feature that sometimes may help. If it doesn't work with your problem - don't use it".


Top
 Profile  
 
 Post subject: delete-orphan
PostPosted: Thu Nov 15, 2007 4:26 pm 
Newbie

Joined: Thu Nov 15, 2007 1:36 pm
Posts: 12
Location: Ahsburn, VA
My hope was that I could get away with the delete-orphan in the even the parent object was deleted.

Sounds like, since I need object C to hang around after removal, that I need to manage both ends of the delete. Not a problem, just needed to know that was the case.

Slightly related, do you know of the XML is "all, delete-orphan" as shown by the documentation, or if it's "all-delete-orphan" as, unfortunately, also shown by the documentation?

Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 15, 2007 4:30 pm 
Beginner
Beginner

Joined: Mon Feb 19, 2007 4:22 am
Posts: 22
Location: Poland
I think there is no difference, look at this post:
http://forum.hibernate.org/viewtopic.php?t=938758


Top
 Profile  
 
 Post subject: Re: Hibernate: Moving Entities Between Collections
PostPosted: Tue Dec 29, 2009 4:49 am 
Newbie

Joined: Tue Dec 29, 2009 4:44 am
Posts: 1
I do exactly have the same issue [moving entities between collection] explained using diagram by Newbie. Can someone give the solution/advise to resolve this issue


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