-->
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: How do i re-assign a child properly?
PostPosted: Mon Jul 11, 2005 6:54 pm 
Newbie

Joined: Sun Jul 10, 2005 6:30 pm
Posts: 10
Greetings.

I have a group of parents (Foo) that have children (bars). What is the proper way to move a child from one parent to another? I currently have code that looks like this:

bar.parent = newParent;

when i try to persist this i get an error telling me that bar already exists in the tranaction someplace else. This is probably true since the old parent is indeed in the transaction by way of another one of it's children. So I am begining to think that bar.parent = newParent doesn't remove the child from the oldParent's child collection?

whats the best way to do this then? -- or am i totally wrong with thats happening?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 7:10 pm 
Newbie

Joined: Sun Jul 10, 2005 6:30 pm
Posts: 10
i think my problem is related to having the same record from the database being represented as 2 distinct instances of the parent --- and they are in the same transaction...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 12:06 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I would need to see more detail about your transaction, however, bar.Parent does not affect parent.Bars. Indeed you have to manage the referential integrety yourself. You can either write business methods or do the work yourself in code. so, if you change bar.Parent, you would have to mange the changed to newParent.Bars and oldParent.Bars yourself.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 10:51 am 
Newbie

Joined: Sun Jul 10, 2005 6:30 pm
Posts: 10
interesting.... cause after i figured out the "multiple references of same row" problem - it worked.

The front end has a screen with all the parents and children and lets you mix them up however you want (re-assign children etc...)

I was doing something like this:
Code:
foreach child on screen
  is parent new?
    no? do nothing
    yes? child.parent = manager.GetParent(id);
loop

as you can see the problem with this is that GetParent returns new instances every time -- so when the second child of the same parent comes along he gets the same parent but in a different instance... now put all these children in a collection and call the manager.SaveKids(childCollection) - which does everything in one transaction and you've got problems....

ALL i did to get this to work was this:
Code:
parents = getAllParents();
foreach child on screen
  is parent new?
    no? do nothing
    yes? child.parent = parents.find(id);
loop

walla - this works fine. But I never explicitly remove the child from the former parent collection - and it still works... maybe because i am persisting the children in the end with SaveKids (as opposed to SaveParents) ...

hrmmmmm


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.