NHibernate-防止删除特定实体(即make read-only)

时间:2022-09-11 18:01:37

How can I prevent NHibernate from deleting a single entity of a specific class? A programmatic way I am using at the moment entails checking for the entity's unique field "Name".

如何防止NHibernate删除特定类的单个实体?我目前使用的编程方式需要检查实体的唯一字段“名称”。

Here's the scenario: I have a person and a group. The group can have persons and other groups. If the group named "Admins" is attempted to be deleted, it will not, due to transaction-level constraints that I enforce (specifically checking for the group's 'Name' column/field, as this is unique). So that's fine.

这是场景:我有一个人和一个团体。该小组可以有人和其他团体。如果尝试删除名为“Admins”的组,则由于我强制执行的事务级约束(特别是检查组的'Name'列/字段,因为这是唯一的),它不会被删除。所以没关系。

But now I'm thinking that if another group is created and the "Admins" becomes a sub-group of that, the check will fail. This will mean the deletion of "Admins". So I'm looking for a better way, other than traversing the parent/child containment tree, e.g. using NHibernate

但是现在我想如果创建了另一个组并且“Admins”成为其中的一个子组,则检查将失败。这将意味着删除“管理员”。所以我正在寻找一种更好的方法,除了遍历父/子包容树,例如使用NHibernate

I can't use a class-wide restriction such as 'class Mutable=false', I mean having a read-only restriction on one or two individual entities of a certain class.

我不能使用类别范围的限制,例如'class Mutable = false',我的意思是对某个类的一个或两个单独实体有一个只读限制。

Regards,

_NT

1 个解决方案

#1


3  

You can write your own implementation of IPreDeleteEventListener and hook into nhibernate's event system to programmatically stop and entity being deleted.

您可以编写自己的IPreDeleteEventListener实现并挂钩到nhibernate的事件系统,以编程方式停止并删除实体。

This is an example of using listeners.

这是使用侦听器的示例。

#1


3  

You can write your own implementation of IPreDeleteEventListener and hook into nhibernate's event system to programmatically stop and entity being deleted.

您可以编写自己的IPreDeleteEventListener实现并挂钩到nhibernate的事件系统,以编程方式停止并删除实体。

This is an example of using listeners.

这是使用侦听器的示例。