views:

246

answers:

1

Hi all

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".

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.

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

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.

Regards,

_NT

+2  A: 

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

This is an example of using listeners.

Canton
But that entails checking a lot of things manually. Is there no NHibernate-y way to do this?
Sorry I can't give a better suggestion for your case.Let me put it this way. "Checking a lot of things manually" is all about domain complexity. ORM cannot help on this. No offence here but it may be the time to re-think about the domain object design, if it's not too late.
Canton
Why re-think the domain object design? I just need to make a couple of entities read-only in the best way possible -a better way than doing ifs and elses, IF that exists... Hence my question.
"become a child in a parent/child relationship" this lead me to domain object design issues. Sorry if I misunderstand your situation.So are you talking about something like ORM-level Foreign Key checking? That does not exist in NH.
Canton
To be more precise: 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 DAL-level (non-NHibernate atm) constraints that I enforce (specifically checking for the group Name column/field). So that's fine. 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
My personal practice is do this kind of checking on service layer, rather than in data access layer.Back to your question. No. There should be no configuration/mapping to help you out in this case. And I don't think any other ORM can help it.
Canton