-->
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: moving one-to-many collection betwen parents
PostPosted: Thu Apr 14, 2005 7:38 pm 
Newbie

Joined: Fri Apr 08, 2005 2:10 am
Posts: 6
if I have a realtion of a cat and kittens (1 - *)
I want to remove all kittens from one cat to the another cat.
Can I do that this way:

cat2.getKittens().addAll(cat1.getKittens());
cat1.getKittens().clear();

If not then what is the best way of doing that, and in general,
when moving a child from one parent to another?

Also is there a need to associate a child to its parent once the parent
was accociated with the child?
Hence if I do cat3.getKittens().add(kitten10);
is there any need to do: kitten10.setParent(cat3) or is it done
by hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 1:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you need to setup all the associations since we cannot intercept this stuff without voodoo magic.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: RE: moving one-to-many collection betwen parents
PostPosted: Fri Apr 15, 2005 10:50 am 
Newbie

Joined: Fri Apr 08, 2005 2:10 am
Posts: 6
1. So what will this code do (if anything)?
cat2.getKittens().addAll(cat1.getKittens());
cat1.getKittens().clear();

2. Should it be done this way?
Collection kittens = cat1.getKittens();
for (Iterator i = kittens.iterator(); i.hasNext(); )
{
Kitten kitten = (Kitten) i.next();
cat2.getKittens().add(kitten);
kitten.setParent(cat2);
i.remove();
}

3. Is there other way which is more recommended to do that?

4. Doesn't EJB CMP do that for you?

5. Do I need to associate a kitten with its parent once I called
cat2.getKittens().add(kitten); ?
After all the collection is implemented by Hibernate which could
apply the reverse association there.

Sorry for all this related questions, I just want to make this subject clear to me.
Thanks a lot.
Arie.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 2:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
>1. So what will this code do (if anything)?
>cat2.getKittens().addAll(cat1.getKittens());
>cat1.getKittens().clear();

Depending on your mapping.
If the collection is the only assocation you have then cat2 will have all cat1 kittens and cat1 will have none.

>2. Should it be done this way?
>Collection kittens = cat1.getKittens();
>for (Iterator i = kittens.iterator(); i.hasNext(); )
>{
>Kitten kitten = (Kitten) i.next();
>cat2.getKittens().add(kitten);
>kitten.setParent(cat2);
>i.remove();
>}

basically Yes

>3. Is there other way which is more recommended to do that?

add methods that encapsulate this.

4. Doesn't EJB CMP do that for you?

EJB 2.1 CMR - container managed relationships does this
but this is not CMR/CMP.

CMR is not usable outside a j2ee container and it
is not how java normally works (there you need to mange your
collections)

>5. Do I need to associate a kitten with its parent once I called
>cat2.getKittens().add(kitten); ?

yes - as in plain old java.

>After all the collection is implemented by Hibernate which could
>apply the reverse association there.

It shouldn't because of the reason mentioned in #4

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: RE: moving one-to-many collection betwen parents
PostPosted: Fri Apr 15, 2005 3:14 pm 
Newbie

Joined: Fri Apr 08, 2005 2:10 am
Posts: 6
Hi Max,

Thanks, that was very helpful!
The only thing that makes me wonder is that in this
relation cat --->* Kitten the DB relation is done by having the cat PK
set as a foreign key in the Kitten table.

When I do: cat2.getKittens().add(kitten);
I assume that either an update to the FK in Kitten table will be done (when flushed)
or that the old record (with the previous parent) will be removed and a new record (with the current parent) will be added.
Which one of the above (if any) Hibernate will actually do?

Also, what will kitten.setParent(cat2) do to the DB in that case?
After all the above action was sufficient to maintain the relation
from the DB perspective.
If the latter action is not going to trigger any further DB updates can
I assume that if I load the same Kitten in a separate Session that the kitten
will have its relation to its parent set correctly (without having to
explicitly set it in the previous session)?

I think those questions cover that topic for me and I hope
you are OK with me asking so much...

Thank you very much,
Arie.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 3:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it will do what you could expect - in this case it would be an update.

Regarding the last question on what is required and not then please go read about the inverse attribute and even better check out Hibernate In Action .....

_________________
Max
Don't forget to rate


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.