NHibernate 多对多关系 - 删除部分关系时出现问题

发布于 2024-08-12 07:16:22 字数 1856 浏览 1 评论 0原文

我的团队和员工实体之间存在多对多关系。

我将它们映射如下:

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        // identifier mapping
        Id(p => p.Id).Column("EmployeeID");

        // column mapping
        Map(p => p.EMail);
        Map(p => p.LastName);
        Map(p => p.FirstName);

        // relationship mapping
        HasManyToMany(m => m.Teams).Table("EmployeeTeam")
            .Inverse()
            .Cascade.All()
            .AsSet()
            .LazyLoad()
            .ParentKeyColumn("EmployeeID")
            .ChildKeyColumn("TeamID");

        HasMany(p => p.LoanedItems).Cascade.SaveUpdate().KeyColumn("EmployeeId");
    }
}

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        // identity mapping
        Id(p => p.Id).Column("TeamID");

        // column mapping
        Map(p => p.Name);

        // relationship mapping
        HasManyToMany(m => m.Employees)
            .Table("EmployeeTeam")
            .LazyLoad()
            .Cascade.All()
            .AsSet()
            .ParentKeyColumn("TeamID")
            .ChildKeyColumn("EmployeeID");
    }
}

然后我创建了 3 个团队和 2 个员工:

TeamID  EmployeeID
1       1
1       2
2       2
3       1

Employee1 还有 2 个借出物品(书籍、杂志)。 Employee2 没有借出的物品。

现在我想删除 Team1 和 Team3 中的 Employee1。 Team 1 中还有 Employee2。 因此,当我删除 Employee1 时,我假设 Employee1 被删除,Team3 也被删除,因为我还假设团队只有在拥有 Employe 时才能存在,反之亦然。所以 Team1 可能不会被删除,因为它有 Employee2 并且仍然存在。

我在新会话中使用了以下代码行:

var loadedEmployee = session.Get<Employee>(1);
session.Delete(loadedEmployee);
transaction.Commit();

但是会发生什么? -> NHibernate 删除所有团队和所有员工! -> NHibernate 通过将 FK EmployeeID 设置为 NULL 正确更新了我的 LoanedItem 表。

有什么问题吗?

I have a many to many relationship between a Team and an Employee entity.

I mapped them as following:

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        // identifier mapping
        Id(p => p.Id).Column("EmployeeID");

        // column mapping
        Map(p => p.EMail);
        Map(p => p.LastName);
        Map(p => p.FirstName);

        // relationship mapping
        HasManyToMany(m => m.Teams).Table("EmployeeTeam")
            .Inverse()
            .Cascade.All()
            .AsSet()
            .LazyLoad()
            .ParentKeyColumn("EmployeeID")
            .ChildKeyColumn("TeamID");

        HasMany(p => p.LoanedItems).Cascade.SaveUpdate().KeyColumn("EmployeeId");
    }
}

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        // identity mapping
        Id(p => p.Id).Column("TeamID");

        // column mapping
        Map(p => p.Name);

        // relationship mapping
        HasManyToMany(m => m.Employees)
            .Table("EmployeeTeam")
            .LazyLoad()
            .Cascade.All()
            .AsSet()
            .ParentKeyColumn("TeamID")
            .ChildKeyColumn("EmployeeID");
    }
}

Then I created 3 Teams and 2 Employees:

TeamID  EmployeeID
1       1
1       2
2       2
3       1

The Employee1 has also 2 LoanedItems(Books, Magazines). Employee2 has no LoanedItems.

Now I want to delete Employee1, who is in Team1 and Team3. In Team 1 is also Employee2.
So when I delete Employee1, I assume that Employee1 is deleted and also Team3, because I also assume that an Team can only exist when it has an Employe and vice versa. So Team1 may not be deleted, because it has Employee2 and can still exists.

I used the following code lines with a new Session:

var loadedEmployee = session.Get<Employee>(1);
session.Delete(loadedEmployee);
transaction.Commit();

But what happens?
-> NHibernate deletes all Teams and all Employees!
-> NHibernate updated my LoanedItem table correctly by setting the FK EmployeeID to NULL.

What is wrong there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2024-08-19 07:16:22

我在这里回答过类似的问题:
什么是在 NHibernate 中定义多对多关系以允许删除但避免重复记录的正确方法

阅读问题和我的答案可能会帮助您了解多对多关联的情况。

I have answered a similar question here:
What is the correct way to define many-to-many relationships in NHibernate to allow deletes but avoiding duplicate records

Reading the question and my answer maybe will help you understand what is going on with your many-to-many association.

自此以后,行同陌路 2024-08-19 07:16:22

因为Cascade.All对HasManyToMany的映射。

如果您希望避免删除级联操作,请改用 Cascade.SaveAndUpdate。

Because of the Cascade.All on the HasManyToMany mapping.

Use Cascade.SaveAndUpdate instead, if you wish to avoid the delete cascade action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文