-->
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: How to cascade a delete for a many-to-one or one-to-many
PostPosted: Thu Sep 06, 2007 6:46 pm 
Newbie

Joined: Wed May 09, 2007 2:25 pm
Posts: 4
Hi everyone!

The situation that I am encountering is that I need to remove a record associated with another record when that record is set to null. To be a bit more clear, here is an example. I have a Person object which contains a Phone object (PERSON table references PHONE_ID from the PHONE table). When the Phone object is set to null in the Person object, I need the Phone record to be removed from the database when the Person object is saved. But what actually happens is that only the PHONE_ID from the PERSON table gets removed, but the actual phone record is still in the database.

The following is what I have inside my mapping for the Person object:

Code:
       
<many-to-one name="phone" class="Phone" cascade="all">
   <column name="PHONE_ID" />
</many-to-one>



I tried using a one-to-one relationship but this doesn't seem to work either. Any suggestions?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 10:06 am 
Newbie

Joined: Wed May 09, 2007 2:25 pm
Posts: 4
Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 11:04 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
As far as I understand cascading is for the events that happen to the containing object. Therefore, setting the phone instance to null has nothing to do with cascading. However, you should expect your phone record to be deleted when person records are deleted. There is an exception when it comes to collection associations in which you can tell hibernate to delete orphan objects by setting cascading to delete-orphan (all does not include delete-orphan). I believe you need to write a code to delete phone objects manually.


Top
 Profile  
 
 Post subject: Re: How to cascade a delete for a many-to-one or one-to-many
PostPosted: Fri Sep 07, 2007 11:07 am 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
tracyt wrote:
Hi everyone!

But what actually happens is that only the PHONE_ID from the PERSON table gets removed, but the actual phone record is still in the database.

I tried using a one-to-one relationship but this doesn't seem to work either. Any suggestions?

Thanks in advance!


I do not know how to do it properly, via mapping. We achived this behavior by providing a custom SQL for deleting from a collection.

Indeed, it seems there is a better way, at least these posts indicate that delete is issued:

http://forum.hibernate.org/viewtopic.php?t=979189
http://forum.hibernate.org/viewtopic.ph ... highlight=


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.