NHibernate on-delete=“级联”多方面的关系

发布于 2024-12-09 07:04:06 字数 638 浏览 0 评论 0原文

我有一个像这样的对象模型:

  • Folder - 具有名称等的简单结构。
  • File - 包含对其中包含的 Folder 的引用的复杂对象。

文件夹不知道它的文件,而且我不想让它知道。该关系是多对一并且应该仅在文件端已知。

无论如何,我想依靠数据库的 ON DELETE CASCADE 功能,以便当我删除文件夹时,我希望自动删除该文件夹中的所有文件。我无法使用 NHibernate 的级联,因为从文件夹到文件没有关系。

我知道 on-delete=在一对多关系的情况下, 元素的“cascade” 选项,但我找不到其等效项对于我的模型 - 当关系侧定义时。

我是否做错了什么,或者我真的需要手动检查并删除已删除文件夹中的所有文件吗?

I have an object model like that:

  • Folder - simple structure with name etc.
  • File - complex object containing reference to Folder in which it is contained.

Folder doesn't know its Files and I don't want it to know. The relation is many-to-one and should be known on File's side only.

Anyway, I would like to rely on my database's ON DELETE CASCADE feature, so that when I remove Folder, I want all Files within that Folder to be deleted automatically. I can't use NHibernate's cascading as there is no relation from Folder to File.

I know that there is on-delete="cascade" option for <key> element in case of one-to-many relationship, but I can't find its equivalent for my model - when the relation is defined on many side.

Am I doing something wrong or do I really need to go through and delete all the Files within deleted Folder manually?

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

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

发布评论

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

评论(1

假装爱人 2024-12-16 07:04:06

您可以尝试使用 access="noop" 映射一对多端。这样,您的类中就不需要属性,但仍然拥有映射。

在 Fluent NHibernate 中,会是这样的:

HasMany(Reveal.Member<Folder, IEnumerable<File>>("_files"))
   .KeyColumn("column_name")
   .Access.NoOp()
   .Inverse()
   .ForeignKeyCascadeOnDelete();

注意:为此,您需要在 Folder 类中使用 IEnumerable 类型的 _files 字段(Fluent NHibernate 的限制,只能映射真正存在的字段或属性)。但该字段始终可以为 null,它永远不会被使用。

You could try to map the one-to-many side with access="noop". That way you don't need a property in your classes but still have the mapping.

In Fluent NHibernate that would be someting like this:

HasMany(Reveal.Member<Folder, IEnumerable<File>>("_files"))
   .KeyColumn("column_name")
   .Access.NoOp()
   .Inverse()
   .ForeignKeyCascadeOnDelete();

Note: For that you need an _files field of type IEnumerable<File> in the Folder class (limitation of Fluent NHibernate, can only map really existing fields or properties). But this field can always be null, it will never be used.

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