-->
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: delete one item from set
PostPosted: Thu Jul 21, 2005 4:09 am 
Newbie

Joined: Tue Jun 28, 2005 4:43 am
Posts: 4
Location: Tres Cantos
I have a relationship one-to-many.

<set name="relacionEnlacesEncuesta">
<key>
<column name="CODENCUESTA" length="1" not-null="false"/>
<column name="ANIO_ENCUESTA" length="4" not-null="true"/>
</key>
<one-to-many class="es.arce.bean.EnlacesEncuesta"/>
</set>

If I want to add a item to relationship, i add the item to the set and save the object, and hibernate makes it persistent.

myObject.getRelacionEnlacesEncuesta().add(item);
Session.save(myObject);

When i want to remove a item from the relationship, i remove the item from the set and save the object.

myObject.getRelacionEnlacesEncuesta().remove(item);
Session.save(myObject);

My problem is: the item removed is not deleted from my database. How could i do this?.

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 4:59 am 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
It depends on which end is the inverse. You probably have the many to one set as non-inverse, i.e. hibernate will check the parent on the many end.

Try setting the one-many to inverse=false, or set the parent on the many to null.

BTW it is good practice (for this very reason) to have an addChild and removeChild on the parent.


Top
 Profile  
 
 Post subject: Re: delete one item from set
PostPosted: Thu Jul 21, 2005 7:14 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
superxurso wrote:
My problem is: the item removed is not deleted from my database. How could i do this?.


Try this:
Code:
<set name="relacionEnlacesEncuesta" cascade="all-delete-orphan">


and please read this 22.3. Cascading lifecycle: http://www.hibernate.org/hib_docs/v3/reference/en/html/example-parentchild.html#example-parentchild-cascades

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 7:20 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Code:
myObject.getRelacionEnlacesEncuesta().remove(item);
//session.save(myObject); !!!! why save() !!!!
session.update(myObject);


or if you just loaded the object:

Code:
myObject = session.load(...)
myObject.getRelacionEnlacesEncuesta().remove(item);
// myObject is in the session, so you don't need to reattach it again

_________________
Leonid Shlyapnikov


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.