NHibernate on-delete=“级联”多方面的关系
我有一个像这样的对象模型:
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 toFolder
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用
access="noop"
映射一对多端。这样,您的类中就不需要属性,但仍然拥有映射。在 Fluent NHibernate 中,会是这样的:
注意:为此,您需要在
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:
Note: For that you need an
_files
field of typeIEnumerable<File>
in theFolder
class (limitation of Fluent NHibernate, can only map really existing fields or properties). But this field can always benull
, it will never be used.