Many to Many: Delete one side, the relationship entry BUT don't delete the other side
Asked Answered
H

1

13

I want to delete an user which has many usergroups but those usergroups don't belong exclusivly to this user: other users can also be using this usergroups. And usergroups can exist even if no user references them.

I want to map the many-to-many relationship so that if a user is deleted, the relationship is automatically deleted but NOT the usergroup?

I tried Cascade.All as I thought cascades on many-to-many affect the relationship but not the other side. I thought that only Cascade.AllDeleteOrphan would do the otherside delete. Obviously I'm wrong.

It seems that I don't understand the cascade rules right. Can someone provide a clear explanation to me and maybe also a way to reach my goal?

Thanks

Horror answered 20/11, 2012 at 10:9 Comment(0)
S
13

NHibernate many-to-many relation does provide what we expect, let me explain it in more details. While we need only two entities User and Group, we will need Three tables: User, Group, UserGroup (with columns UserId, GroupId)

C# entities:

public class User {
   IList<Group> Groups {get;set;}
}

public class Group{
   IList<User> Users{get;set;}
}

hbm.xml our mapping will look like this:

<class name="User" ...    
  <bag name="Groups" lazy="true" 
       table="UserGroup" cascade="none" >
    <key column="UserId" />
    <many-to-many class="Group" column="GroupId" />
  </bag>
  ...

<!-- and group vica versa -->

<class name="Group" ...
  <bag name="Users" lazy="true" 
       table="UserGroup" cascade="none" >
     <key column="GroupId" />
     <many-to-many class="User" column="UserId" />
  </bag>
    ...

This mapping with important setting cascade="none" will do expected behaviour. This mapping says that there is a PairTable UserGroup, which does not have any entity representation. So, there cannot be any cascade setting effecting this table. This table is used hiddenly behind the scene.

Pair table

When some user is deleted, then NHibernate will aslo remove all the relations from the UserGroup table (in fact this will be the first statement in the batch). This is just the relational reference constraint handling. We cannot leave any UserId in the table UserGroups, which does not have its foreign key in the User table.

The other relation end

Finally the cascade setting: Because the UserGroup table is managed without any our attention, the cascade is in this case applied to entity Group - the other relation end. So setting it to all-delete-orphan could lead to complete deletion of all cross referenced records.

Summary: cascade on a bag with many-to-many relation is meant for the other end point, not the pairing table.

Stansbury answered 20/11, 2012 at 11:50 Comment(3)
Thank you! I did some testing... Cascade.None (or Cascade.SaveUpdate) works. What seems important is that one (only one!) side of the association is inverse. The if the inverse=false side is deleted it works as expected. If the inverse=true side is deleted, a foreign-key exception is thrown... but it works if its collection is emptied before, which means manually removing the association... I guess there's no way around that, right?Horror
I was using both. Mostly Cascade.None. Because usualy I have both ends already in the system (or at least Groups when adding new User), then Cascade.None will still insert into PairTable. Exceptionally we used Cascade.SaveUpdate, and it is wokring as expected;)Fardel
@RadimKöhler Yeah, every time I was trying to delete a row in one table, it deleted the other raw too. Didn't know, that cascade option was for the other table, not the link table. Thanks :)Unpredictable

© 2022 - 2024 — McMap. All rights reserved.