开发者

How to automatically delete many-to-many associations (cascade)

In my database I've got Users and UserGroups which have a many-to-many relation. The User has a set of UserGroup, the UserGroup domain object does not see the User.

<class name="User" table="UserTable">
    <set name="UserGroup" cascade="save-update" access="field.pascalcase-underscore" table="User2UserGroup">
        <key column="User_Id" />
        <many-to-many class="UserGroup" column="UserGroup_Id" />
    </set>
    ...

What I'm trying to achieve is the nhibernate deleting the correlation from the junction table when I delete either a User or a User Group. Additionally User and Group are child objects of, let's call it a Domain. Domain does cascade="all-delete-orphans", so when a Domain gets deleted it cascade-deletes all its Users and UserGroups.

Back to the User<->UserGroup relation: If I understand correctly开发者_运维技巧 I can't use any form of cascade that involves delete as I just want to delete the association between two objects and not the related object itself. (A group shall not vanish, even if it's an orphan. And a User without a group is a valid thing in my world - he just has no rights to do anything at all.)

Do I need to look at events/interceptors? Or can I do what I want to achieve by controlling the mapping?


If you are using a database that supports it can you not set the cascade on the database itself in the form of a Foreign Key constraint?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜