-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Delete cascade
PostPosted: Thu Apr 21, 2005 9:13 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
Hibernate version:
2.1.6

Thanks for your interest in my topic!

I have a simple one-to-many mapping which looks the following using xdoclet in my class PersistentReportDefinition:

@hibernate.set inverse="true" table="time_span" lazy="false" name="timeSpans" cascade="all-delete-orphan"
@hibernate.collection-key column="report_def_id"
@hibernate.collection-one-to-many class="com.nexusatwork.nrs.collectable.persistence.mapped.PersistentTimeSpan"

I first create a ReportDefinition which has three such PersistentTimeSpanS in a Set. This works very well, they are saved cascading.

Later on I want to delete a time span from the set. But I do not have the original instance of PersistentReportDefinition, so I build up a new one but with only 2 PersistentTimeSpanS in the set. The primary keys are set, so hibernate knows it has to update and not to insert.

The problem is, that it does not delete the PersistentTimeSpan which is not in the Set. Does Hibernate only realize it has to remove when I actually call remove on the Set, or whats the problem?

Thank you very much for your help!


Top
 Profile  
 
 Post subject: deleting children
PostPosted: Thu Apr 21, 2005 9:40 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
i suppose you should first load the parent object, denn remove the children from the collection, then save the parent object again.

This should delete the children.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 9:49 am 
Beginner
Beginner

Joined: Tue Apr 19, 2005 9:39 am
Posts: 45
Did you set up the one to many relationship as a bidirectional? Looks like you did by the inverse = "true.

B


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 9:49 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
Yes that would do it, but I can't use this. But why doesn't it work the way I am doing it. I clearly call update with a set that contains less values then the one on the database, so hibernate should no that it must delete them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 9:53 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
Yes, I've set it up as bidirectional. Is that the problem? Thought that must do it together with cascade="all-delete-orphan".


Top
 Profile  
 
 Post subject: cascade deletes
PostPosted: Thu Apr 21, 2005 11:53 pm 
Newbie

Joined: Thu Apr 21, 2005 11:40 pm
Posts: 4
I am also having trouble with cascaded deletes. I can not get neither cascade="all-delete-orphans" nor cascade="all" to have any effect. Using these or not seems to have no effect, and hibernate always tries to update rows that should be deleted. It tries to set the foreign key id to null instead of deleting the row.

I did get cascaded deletes to work if I make a bi-directional association with inverse="true". This is a bit painful as it means I must maintain a reference in the child objects back to the parent object which is not a normal part of my object model.

Are these the only circumstances under which the cascade attribute has any effect?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 4:13 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
I'm still having problems with this. Lets try to explain the problem differently. I have a parent object which is loaded in a session and has some childrens. Then I remove a children from the parents set and call update in another session (the previous one was closed). This will not delete the childrens from even if cascade="all-delete-orphan" is set.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 9:43 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
This explains the problem found somewhere else:

all-delete-orphan does not work like this, it only works if you remove stuff from a collection originally loaded by hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 3:29 pm 
Newbie

Joined: Thu Apr 21, 2005 11:40 pm
Posts: 4
Someone said:

all-delete-orphan does not work like this, it only works if you remove stuff from a collection originally loaded by hibernate.

I reply:

I haven't found this to be true. I have loaded collections from the database using hibernate and removed objects from them. When I watch the SQL statements generated by this action, update statements are generated for all removed children instead of delete statements. all-delete-orphan does not DELETE orphans. It updates children so that they are no longer associated with the parent. Under a normal database schema this will fail because the foreign key is not null as the relationship is mandatory.

The only way I've been able to get cascaded deletes to work (and then only in some relationships) is by maintaining a bi-directional relationship. And this doesn't seem to work for one-to-one relationships.

I'm considering putting a method void deleteCascade(Session s) in my persistent objects and then teaching each object how to delete itself and any children. This would give me true, reliable, cascading deletes.

However, I'm just a beginner at Hibernate. Hopefully I will find a better way.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 3:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
newbridge23, please stop telling people incorrect stuff.

all-delete-orphan behaves as documented.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 7:09 pm 
Newbie

Joined: Thu Apr 21, 2005 11:40 pm
Posts: 4
So, the only way to get all-delete-orphans to delete things is to have bi-directional mappings? That's it, end of story, no other way?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No. That is not the story.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 7:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
By the way, the unit tests for this are in org.hibernate.test.orphan.

Code:
<hibernate-mapping package="org.hibernate.test.orphan">

   <class name="Product">
      <id name="name"/>
      <set name="parts" cascade="all,delete-orphan" fetch="join">
         <key column="productName" not-null="true"/>
         <one-to-many class="Part"/>
      </set>
   </class>
   
   <class name="Part">
      <id name="name"/>
      <property name="description" not-null="true"/>
   </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 9:51 pm 
Newbie

Joined: Thu Apr 21, 2005 11:40 pm
Posts: 4
Awesome. Thank you very much, this solves my problems.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 4:16 am 
Beginner
Beginner

Joined: Wed Aug 04, 2004 4:33 am
Posts: 45
Location: Switzerland
I've had a "dirty" idea.
I'm sure the hibernate implementation of the Set does this stuff especially the getOrphans() method on it. So I was thinking about instantiate a hibernate Set and will then let hibernate do the rest.
What do you think about this? It might work, but it might be too dirty...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.