为什么NHibernate会一一删除引用的对象,而不是使用外键?

发布于 2024-11-27 07:20:25 字数 1086 浏览 0 评论 0原文

我有简单的 Parent-Child 关系,Parent 有许多 Child 对象,关系是单向的:

public class Parent
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
    public virtual IList<Child> Children { get; set; }
}

public class Child
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
}

映射为关系集级联到 AllDeleteOrphan 以删除 Parent 不再引用的 Child 对象:

HasMany(x => x.Children).Cascade.AllDeleteOrphan();

现在我正在清除 Child 列表对象:

var parent = session.Get<Parent>(1);
parent.Children.Clear();
session.Update(parent);

NHibernate 按预期删除 Child 对象,但它是通过为集合中的每个 Child 发送单独的 DELETE 查询来实现的:DELETE FROM Child WHERE Id = ... - 这可能意味着很多查询。

无论如何,可以使用单个查询轻松完成此操作,例如 DELETE FROM Child WHERE ParentId = 1。为什么 NHibernate 不使用父外键来清除集合?似乎它知道准备此类查询的一切(哪个外键,什么值等)?

I have simple Parent-Child relationship, Parent has many Child objects, the relation is unidirectional:

public class Parent
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
    public virtual IList<Child> Children { get; set; }
}

public class Child
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
}

Mapping for the relation sets cascade to AllDeleteOrphan to remove Child objects not referenced by Parent any more:

HasMany(x => x.Children).Cascade.AllDeleteOrphan();

And now I'm clearing the list of Child objects:

var parent = session.Get<Parent>(1);
parent.Children.Clear();
session.Update(parent);

NHibernate deletes the Child object as expected, but it do this by sending separate DELETE query for each Child from the collection: DELETE FROM Child WHERE Id = ... - that can mean really LOT of queries.

Anyway, it can be easily done using single query like DELETE FROM Child WHERE ParentId = 1. Why NHibernate is not using the parent foreign key to clear the collection? It seems that it knows everything to prepare such query (which foreign key, what value etc.)?

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

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

发布评论

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

评论(2

嘿咻 2024-12-04 07:20:25

在某些情况下,NHibernate可以并且将会做到这一点。

此处对此进行了全部解释。

NHibernate can and will do that in certain cases.

It's all explained here.

轻拂→两袖风尘 2024-12-04 07:20:25

NHibernate 在删除子项之前发出 NHibernate: UPDATE "Child" SET Parent_id = null WHERE Parent_id = @p0;@p0 = 1 。由于有很多不是逆的,父级必须取消设置关联,然后将删除级联到子级,因为 chuild 可能有其他必须删除的级联设置。

然而,即使使用反向一键删除也不起作用。 NH 似乎只会立即删除元素/组件集合。

原因之一可能是子级可能具有用户定义的 sqldeletes、附加级联属性等。对于 NH 最简单的方法是简单地将删除分别级联到每个子级。

NHibernate issues a NHibernate: UPDATE "Child" SET Parent_id = null WHERE Parent_id = @p0;@p0 = 1 before deleting the childs. Since the has many is not inverse the parent has to unset the association and then cascades the delete to the child because the chuild may have other cascades set which have to be deleted.

However even with inverse one shot deletes do not work. NH only seems to delete at once with element/component collections.

One reason can be childs could have user defined sqldeletes, additional cascaded properties and so on. The easiest way for NH is to simply cascade the delete to each child seperatly.

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