如何在 HQL 中给定子 ID 的情况下从父级中删除一对多子级

发布于 2024-09-17 16:48:36 字数 306 浏览 3 评论 0原文

class Parent
{
    IList<Child> Children;
}

class Child
{
}

在删除 Child 时,需要从引用它的任何 Parents 中删除对它的所有引用。

我怎样才能在 NHibernate 中做到这一点?

Child 上没有 Parent FK,关系存储在第三个“链接”表中,

谢谢

I've got

class Parent
{
    IList<Child> Children;
}

class Child
{
}

When deleting a Child I need to remove all references to it from any Parents that reference it.

How can I do this in NHibernate?

There is no Parent FK on Child, the relationship is stored in a 3rd "link" table

Thanks

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

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

发布评论

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

评论(2

独留℉清风醉 2024-09-24 16:48:36

这不是亲子关系。孩子只有一个父母(属于父母)。这是独立实体之间的多对多关系。这是一个重要的区别。

实际上,您无法按照现在的设计方式直接在 HQL 中从“父级”中删除“子级”。这是您的选择:

  • 将“父母”加载到内存中并从列表中删除“孩子”。
  • 添加从“孩子”到“父母”的引用。使另一个关系呈逆关系。当您删除“子”时,它实际上应该自动删除链接表中的项目,因为那时该链接将属于“子”。
  • 使用本机 SQL 删除链接。这不太好,但也不算太糟糕,因为它是微不足道的标准 SQL。

This is not a parent-child relation. Children have only one parent (the belong to the parent). This is a many-to-many relation between independent entities. This is a important difference.

You can't actually remove the "children" from the "parent" directly in HQL the way it is designed now. This are your options:

  • load the "parents" into memory and remove the "child" from the list.
  • Add a reference from the "child" back to the "parents". Make the other relation inverse. It should actually automatically remove the item in the link table when you remove a "child", because the link would belong to the "child" then.
  • Delete the link using native SQL. This is not nice, but also not too bad because it is trivial standard sql.
So尛奶瓶 2024-09-24 16:48:36

为了实现这一点,需要有某种从子实体开始的关系。

然后你可以简单地使用级联删除到它的3d(我猜这是一个多对多)表。

To make this happen, there needs to be a relationship of some sort, starting from the Child Entity.

Then you can simply use cascade-delete to its 3d (I'm guessing this is a many-to-many) table.

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