-->
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: Child entity with 2 parents. Best practice?
PostPosted: Sun Jan 20, 2008 7:05 am 
Newbie

Joined: Sat Jan 19, 2008 8:21 am
Posts: 3
Hi.

I have a pojo child that has to parents mother and father. Lets say the child cant exist without a mother or father. If I delete one of the parents I want the child deleted also.

If I delete the mother I dont want the father deletede only the child.

Is there a best practice for doing this?

From what I can read I have to

If I map it with bidirectional bindings then the code would look like this.

Parent mother = getMotherFromDB....;
List children = mother.getChildren();
for(children)
{
//To remove the link from father to the child. Otherwise the child
// would be persisted again by father because I use cascade all
child.getFather().removeChild(child);
}
session.delete(mother);
session.flush();

This just seems more complex than it should have to be. Can hibernate in any way handle this by it self?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 20, 2008 10:37 am 
Newbie

Joined: Sun Dec 30, 2007 3:58 pm
Posts: 7
lookup "cascade = all-delete-orphan "


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 20, 2008 11:38 am 
Newbie

Joined: Sat Jan 19, 2008 8:21 am
Posts: 3
zahy wrote:
lookup "cascade = all-delete-orphan "


Hi Zahy and thx for your reply.

Well the thing is I know about delete-orphan and if it was a normal one to many mapping I would know how to set it up but since this child has 2 parents Im not sure how to do it. The problem is that the child will still have an orphan even though I remove the father or the mother but I want it deleted if I delete just one of them.


If I have

Class Mother
List children

Class father
List children

Class Child
Father
Mother

If the mother and father has the child in both their list that means they are the parents to that child.
What is the easiest way to delete a mother and all her children?
Or if I just want all a mothers children deleted how is the easiest way to do that? the only way I know of is to

get mother from db.
Loop her children.
remove link to the childrens father.
Remove link to the childrens mother.
and thats it but it just seems as much code when all I really just want is to


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 2:23 am 
Newbie

Joined: Mon Jun 11, 2007 1:34 pm
Posts: 11
if you use cascade = all-delete-orphan on both associations mother -> child and father -> child you only have to delete the mother or the father to have deleted the childs.
oh, and if you get a "deleted entity passed to persist" error tell me becouse i also got that error and find a workaround.

Good luck!


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.