NHibernate on-delete =“级联".与多方面的关系 [英] NHibernate on-delete="cascade" with relationship on many side

查看:113
本文介绍了NHibernate on-delete =“级联".与多方面的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an object model like that:

  • Folder-具有名称等的简单结构.
  • File-包含对Folder的引用的复杂对象.
  • 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.

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

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.

我知道on-delete="cascade"选项用于一对多关系的<key>元素,但是我找不到与我的模型等效的模型-当关系在上定义为时许多方面.

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?

推荐答案

您可以尝试使用access="noop"映射一对多面.这样一来,您就无需在类中使用属​​性,但仍具有映射.

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.

在Fluent NHibernate中,应该是这样的:

In Fluent NHibernate that would be someting like this:

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

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

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.

这篇关于NHibernate on-delete =“级联".与多方面的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆