本文介绍了多对多:删除一侧,但关系条目不删除另一侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除一个具有许多用户组的用户,但是这些用户组并不排他地属于该用户:其他用户也可以使用该用户组.即使没有用户引用用户组,它们也可以存在.

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?

我尝试了Cascade.All,因为我认为多对多级联会影响关系,但不会影响另一侧.我以为只有Cascade.AllDeleteOrphan可以进行另一边的删除.显然我错了.

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?

谢谢

推荐答案

NHibernate many-to-many关系确实提供了我们所期望的,让我对其进行更详细的解释.虽然我们仅需要两个实体 User Group ,但我们将需要三个表:UserGroupUserGroup(带有 UserId GroupId )

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#实体:

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

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

hbm.xml ,我们的映射如下所示:

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>
    ...

具有重要设置cascade="none"的此映射将完成预期的行为.此映射表示存在一个 PairTable UserGroup,它没有任何实体表示.因此,没有任何级联设置会影响此表.该表在幕后被隐藏使用.

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.

配对表

删除某些用户后, NHibernate 还将从UserGroup表中删除所有关系(实际上这将是批处理中的第一条语句).这只是关系参考约束处理.我们不能在表UserGroups中保留任何UserId,该表在User表中没有外键.

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.

另一个关系结束

最后,级联设置:由于对UserGroup表的管理没有引起我们的注意,因此 cascade 在这种情况下适用于实体Group-另一关系端.因此,将其设置为all-delete-orphan可能导致完全删除所有交叉引用的记录.

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.

摘要:级联在具有many-to-many关系的bag上是给另一个端点的,而不是配对表.

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

这篇关于多对多:删除一侧,但关系条目不删除另一侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:28