[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Hibernate Experts Required

Posted on 2005-04-14
8
Medium Priority
?
227 Views
Last Modified: 2013-11-24
Hello all,

          I have a one-to-many relation ship defined using XDoclet tags between two tables say cat->kittens.  I have a requirement here, suppose that a cat dies, all the kittens associated with this cat should be now assigned to another cat (say step-cat), how do i achieve it ?? Does Hibernate do that automatically, if we specify a default value for cat_id in the kittens table, or is there anything we should write to explicity command the Hibernate to do it this way.

Thanks.
0
Comment
Question by:letsbedecent
  • 5
  • 3
8 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13783638
You need to specify to which cat you want to pass the kittens.
You will need to get the Cat kittens.
Collection kittens = cat1.getKittens();
then call cat2.getKittens().addAll(kittens);
0
 

Author Comment

by:letsbedecent
ID: 13784067
well that will add to the list, but will that changet the cat_id column of all these kittens in the kittens table ?? is my question.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13784473
It should once you commited the changes. did you try it? Are you using Hibernate 3
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:letsbedecent
ID: 13784980
I am using Hibernate 2.1.7,  and moreover i am using spring to handle the transactions for me, so i am not sure how to commit the transactions !!
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13785075
I think it will be commited automatically once spring transaction is completed [as long as it was not rollbacked] (but not sure about it).
Did you check your DB to see if the above code applies the required changes?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13787076
Did you try it?
Also, if you are not going to delete cat1 (as you specified in your question) then I think you will need to call cat1.getKittnes().clear();
0
 

Author Comment

by:letsbedecent
ID: 13787083
Hey thanks for you help man,

i was tied up with other task today, i will sure try it tommorrow morning and let you know, i seriuosly appreciate your help
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1500 total points
ID: 13796335
BTW, if your Cat definition has inverse="true" in its kitten set one-to-many relation
then chaning the collection will not have an effect on the DB. (unless Kitten does not have a relation back to Cat).
If Kitten have a relation many-to-one to Cat and Cat has as inverse="true" in its relation then
the DB relation will be effected only by modifying Kitten relation to its parent Cat.
In that case the following example should work:
 
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();
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question