This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
When you clear all items in an entity’s collection by calling it’s Clear() method, if it has cascading option all-delete-orphan, NHibernate will then delete (when flushing) all of the collection’s items. That is because it considers them to be children of the entity and we explicitly told NHibernate that the entity no longer has any (very sad, I know! ).
Ideally, we would expect NHibernate to only issue a single DELETE, as this:
That, however, is not the case for inverse collections, which are the most common ones. In this case, NHibernate issues N DELETEs, one for each entity, which is bad in terms of performance:
The NHibernate reference has an incorrection, in which it states exactly the opposite:
“one-shot-delete apply to collections mapped inverse="true"”
There is an open issue at NHibernate JIRA meant to fix this, https://nhibernate.jira.com/browse/NH-3708, but it is not so simple, so we’ll have to wait!
In the meantime, even though it’s not so intuitive, you can use one of the delete methods I describe in this post.