-->
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: Hibernate List One-to-many delete problem
PostPosted: Mon May 21, 2007 7:45 pm 
Newbie

Joined: Thu May 17, 2007 11:27 am
Posts: 8
Location: US
Hibernate version. 3.2.3

I setup the one to many relationship between two table/object. I set the cascade to all. However, the update and save will be working for relationship. but the delete will not working. Everytime, I remove on object on the list, and save the object. From the showed SQL, I saw that it will run the update sql statement instead of delete. Please help!

_________________
Raemond


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 11:21 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi raemond1115,

Use cascade="all,delete-orphan"

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 8:45 am 
Newbie

Joined: Thu May 17, 2007 11:27 am
Posts: 8
Location: US
Hi dharmendra.pandey,
I tried that already, however, it still using the update statement and try to set all the value to null for the record which need to remove.

_________________
Raemond


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 11:22 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi raemond1115,

Post your code between transaction .begin and close

Quote:
try to set all the value to null for the record which need to remove.

this won't work

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 8:47 pm 
Newbie

Joined: Thu May 17, 2007 11:27 am
Posts: 8
Location: US
I am newbie to Hibernate. I setup a console application which has two class. The relationship is using List one-to-many. I set the cascade of the List to be all. I try to do the following
List entriesList = Parent.getChild();
entriesList.remove(oneEntry);
Parent.setChild(entriesList);
session.saveOrUpdate(Parent);

From the output, I show the SQL which generated by Hibernate. However, it keep show it try to update the child with Null value. It will throw exception since the primay key can not be set to null.
However, if the do the following the it allow me to delete
session.evict(Parent)
session.delete(oneEntry);
I think that it will have a way to delete the entry from the associate list without detact the object from session. Please help to me to solve this mystery issue. Thank alot

_________________
Raemond


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 11:39 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi raemond,

I could suggest following

List entriesList = Parent.getChild();
entriesList.remove(oneEntry);
Parent.setChild(entriesList); //not required
session.saveOrUpdate(Parent);

one entry should be the same object which is added by hibernate in entriesList .I don't know from where you are getting .

Use this way with cascade="all,delete-orphan"


List entriesList = Parent.getChild();
Child oneChild=(Child )entriesList .get(0);//Or you could apply check for ID
entriesList.remove(oneChild);
session.saveOrUpdate(Parent); //optional depend upon the transaction strategies
session.flush();//optional depend upon the transaction strategies

_________________
Dharmendra Pandey


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.