-->
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.  [ 8 posts ] 
Author Message
 Post subject: Delete problem
PostPosted: Tue May 02, 2006 10:06 am 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
Basically, I have a one-to-many relationship between Parent and Children. The Parent has a map whose values are Child objects. The relationship is bidirectional, inverse=true, cascade=all-delete-orphan in Parent.hbm.xml and not-null=true in Child.hbm.xml.
I am using Spring's HibernateTemplate.

I am trying to re-assign all children from Parent p1 to Parent p2:

[... get all children from p1 ..]

child1.setParent(p2);
child2.setParent(p2);
[ .. etc. ..]
hibernateTemplate.saveOrUpdate(p1);

Also, I clear the map in p1 that contains child1, child2, etc.

Then, I need to delete p1 because it is empty now (no children):

hibernateTemplate.delete(p1);
hibernateTemplate.flush();

At this point, I get an exception:

org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: deleted object would be re-saved by cascade (remove deleted object from associations)

I don't understand why it's happening. I would think the child
objects would all be pointing to the new parent at that time.

Any help would be greatly appreciated.


Last edited by imchi on Tue May 02, 2006 2:10 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 12:06 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
do you also bind children to parent2 i.e.

Code:
p2.getChildren().add( child1 );
p2.getChildren().add( child2 );


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 12:30 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
bkmr_77 wrote:
do you also bind children to parent2 i.e.

Code:
p2.getChildren().add( child1 );
p2.getChildren().add( child2 );


Yes, I do.


Top
 Profile  
 
 Post subject: Re: Delete problem
PostPosted: Tue May 02, 2006 12:43 pm 
Newbie

Joined: Mon Aug 01, 2005 7:33 am
Posts: 18
Location: UK
imchi wrote:
I am trying to re-assign all children from Parent p1 to Parent p2:

[... get all children from p1 ..]

child1.setParent(p2);
child2.setParent(p2);
[ .. etc. ..]
hibernateTemplate.saveOrUpdate(p1);

Also, I clear the map in p1 that contains child1, child2, etc.

Then, I need to delete p2 because it is empty now (no children):

hibernateTemplate.delete(p1);
hibernateTemplate.flush();


I am confused here - you are moving them from p1 to p2, so wht would you want to delete p2? Secondly I would not saveOrUpdate p1 until you have removed the children from p1 which you don't appear to have done. The process would be as follows:
1) Get all children of p1
2) remove each child (or the collection) from p1
3) remove the parent reference in each child from p1 and change to p2
4) set each child to reference p2
5) save/update/delete (if you want to) p1
6) save/update p2

Or have I missed something here?

Good luck!

S[/list]


Top
 Profile  
 
 Post subject: Re: Delete problem
PostPosted: Tue May 02, 2006 2:09 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
shaunybee wrote:

I am confused here - you are moving them from p1 to p2, so wht would you want to delete p2? Secondly I would not saveOrUpdate p1 until you have removed the children from p1 which you don't appear to have done. The process would be as follows:
1) Get all children of p1
2) remove each child (or the collection) from p1
3) remove the parent reference in each child from p1 and change to p2
4) set each child to reference p2
5) save/update/delete (if you want to) p1
6) save/update p2

Or have I missed something here?

Good luck!

S[/list]


It's a typo, I meant to say I need to delete p1, not p2.
I'll try to double-check all steps. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 3:07 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
I think I got it to work now. Thanks everyone for help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 3:08 pm 
Newbie

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 10
Location: Buenos Aires
imchi wrote:
I think I got it to work now. Thanks everyone for help.


how did you resolve it? i have the same situation and the same problem

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 2:15 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
I think I ended up writing a separate method that goes through all parent objects and deletes those that don't have children any more.


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