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.  [ 9 posts ] 
Author Message
 Post subject: Cascade delete not firing?
PostPosted: Mon Sep 25, 2006 4:02 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I'm using NHibernate 1.0.2

(fyi: I'm sure this is a bug in my mapping, but I want to confirm here)

Say you have a hierarchy (like in the book HIA) where you have:

Categories
Items
Bids

A category has many Items
An Item has many bids

---------------------------------

now if you delete a category it should delete every item and bid underneath it... so in the mapping you'd specify something like this:

Code:
//Category.hbm.xml
<bag name="Items" ......  cascade="all-delete-orphan"> ... </bag>

//Item.hbm.xml
<bag name="Bids" ........ cascade="all-delete-orphan">....</bag>


Is this correct?

I currently have a relationship just like this, but when I call delete on the root object, it sets the FK to NULL on the bottom object (bids here).

so the action goes like this:

Delete issued in code
SQL -> UPDATE Bids SET ItemId = NULL
SQL -> DELETE Item WHERE ...... //ERROR! FK VIOLATION


Are my assumptions correct?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 5:22 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
this can sometimes happen if you don't have inverse="true" set on the bag (assuming you have a relationship back from Item to Category and from Bid back to Item...

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 5:37 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I thought that too, but the associations are one-way...

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 6:19 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
The Item class is _not_ composed of a Category class (or a CategorizedItem class to quote HIA)? The Property might not be public, but you must have a member variable of the Item class that points in some way back to the Caetgory. Can you post the Item class?

Cheers,
-d


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 7:01 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
In my object-model it is a string hierarchy like that.

The entities are:

Users -> Roles -> Constraints


if I remove a users roles, the constraints should be deleted as well, however they are not... my FK in the constraints table is being set to null by NHibernate.

This smells like a directionality issue / something that might be solved by inverse, however the Constraint has no reference back to Roles (is this my error?)

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 10:15 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
not entirely understanding your model, however, if a Constraint has a life cycle outside of a user's Role(s) then no you do not need an inverse relationship (however, I would make the case this is a M-M relationship). However, if a Constraint does not have a life cycle outside of a user's Role(s) then, yes, in my opinion, you need the inverse relationship from Constraint back to Role. This relationship does _not_ have to be navigable, however it must be there at least as a member variable. Something like:

Code:
class Role {
   private User user;
   private IList constraints;

   public Role (User u) {
      this.user = u;
      this.roles = new ArrayList();
   }

   public virtual User User {
      get { return this.user; }
   }

   public virtual IList Constraints {
      get { return this.constraints; }
   }
}

class Constraint {
   private Role role;
   ...

   public Constraint (Role r) {
      this.role = r;
   }
}

Note in the Constraint class there is no public accessor for the role member variable. The relationship exists but you can't navigate it. Apologize if you know this. Like I said, I may be misunderstanding your model.

-d


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 10:18 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I wanted to keep it simple so it wouldn't be polluted with extraneous details. What you mention looks like it will solve my problem completely.

I'll try it in the morning and let you know if it solves my issue. Thanks!

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 1:01 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Thanks for your suggestion. It had quite a few mapping ramifications, but once they all followed the same pattern all my tests are passing again.

Thanks!

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:02 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
great! glad it worked out.

cheers,

-d


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.