-->
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: Cascading delete of unidirectional relationship
PostPosted: Tue Mar 01, 2005 2:44 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
I have a unidirectional many-to-one relationship between 2 tables A and B. B can navigate to A and A cannot navigate to B. If I delete A I would like the delete to cascade to B. Is this possible? If so can I get a quick pointer on how?

Regards,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 7:03 pm 
Newbie

Joined: Tue Mar 01, 2005 6:58 pm
Posts: 7
I've been wondering if this is possible as well. Does anyone have any pointers on how to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
If you don't want to map the association from A to B, your only real option is cascading deletes in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:06 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
That's too bad. So, is this something that would be impossible to add to hibernate or would it be something worthy of an Enhancement request in JIRA? If it is possible it would seem to me very valuable.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well, you don't want to define the association, so to what should we attach that metadata?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:57 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
The metadata would have to be attached to other side in a kind of an inverse-cascade property. I don't have any idea how the internals of Hibernate work but it would obviously require the creation of some sort of listener for the delete of an A objects. Or it would require the session searching for non directly associated relationships with the object.

Again I don't know much about the inner workings of Hibernate so the thought of the session looking for inverse associations is rediculous. :)

Anyway, if it is possible it would be nice.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 7:22 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Quote:
your only real option is cascading deletes in the database


How about evicting stale object from caches? Database can't do the all jobs.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 9:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Look, its as simple as adding the association to the mapping, and having that association cascade deletes; hibernate will make sure cached stuff gets cleaned-up appropriately.

But if you are unwilling to do that, what is it you want? Should Hibernate some how magically whisk away all your bad decisions? Is it time to start looking at embedding an "intention translator" inside Hibernate? ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 9:43 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Quote:
Look, its as simple as adding the association to the mapping, and having that association cascade deletes


So, in summary:

1) 'cascading deletes in the database' is not option at all, because this is lead to problems with cache.

2) If I want cascading, then I should add reverse association, which isn't required by object model, but required by Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 12:52 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
The reason I don't want to add reverse assosiations to my object model is because I don't want to muddle up my object with tons of assosiations it doesn't need to know about. What if I have 10 different object with need to associate with the 1. The object model can get ugly really quick if I always have to add reverse assosiations solely to provide cascading deletes.

I'm not looking for Hibernate to do magic. :) The metadata for the reverse cascade could be stored on the other side of the association. But, based on how you're talking Steve it sounds like it would be impossible for hibernate to untilize this information in that location.....correct? If so then it's not a big deal I'll just have to delete the other objects in my Logic. But it sure would have been nice if hibernate could of done it for me. :)

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 12:37 am 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
Actually I just had a thought Steve. I'm not very good with Hibernate so is it possible to create an association in the mapping file without actually creating a field to associate it to?

If so I can see why you thought it was crazy my not wanting to put the reverse associating in. But if a field would be necissary then my comments and questions remain as stated. :)

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 9:37 pm 
Newbie

Joined: Fri Jun 11, 2004 4:58 pm
Posts: 9
youngm77 wrote:
The reason I don't want to add reverse assosiations to my object model is because I don't want to muddle up my object with tons of assosiations it doesn't need to know about. What if I have 10 different object with need to associate with the 1. The object model can get ugly really quick if I always have to add reverse assosiations solely to provide cascading deletes.

I'm not looking for Hibernate to do magic. :) The metadata for the reverse cascade could be stored on the other side of the association. But, based on how you're talking Steve it sounds like it would be impossible for hibernate to untilize this information in that location.....correct? If so then it's not a big deal I'll just have to delete the other objects in my Logic. But it sure would have been nice if hibernate could of done it for me. :)

Mike


Mike,

I'm in the same situation. I'd rather not bend my domain model to fit hibernate's rules. I've been working on a library to be used with Hibernate that would do cascading deletes of unidirectional associations (including removal of an entity from a collection on an associated entity).

I've requested a sourceforge project to share my work. I'll let you know when it's up.

-Alex


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 9:46 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
Alex,

Great keep me posted. My project doesn't have a huge requirement for this functionality but it would be nice as long as your solution isn't too invasive.

I tried posting an enhancement but apparently it is "Too hard to implement" for the Hibernate Team.

http://opensource.atlassian.com/project ... se/HHH-266

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 11:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You could also always implement this functionality yourself using an Interceptor or Lifecycle, unless thats "too hard for you to implement" ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 11:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
If so I can see why you thought it was crazy my not wanting to put the reverse associating in. But if a field would be necissary then my comments and questions remain as stated

Well the field would have to be there. Typically it'd just be marked as lazy (make it private if you don't want code accessing it). You have to keep in mind that the Hibernate mapping file defines the database schema as much as it defines the object model.


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.