tags:

views:

45

answers:

2

I've mapped child collections with cascade="all-delete-orphan" and tried

var parent = session.Load<Parent>(id);
session.Delete(parent);

But it also loads parent and all its children before deleting them. Is there a way to delete parent and children without loading them or at least only load parent?

+1  A: 

I am no NHibernate expert, and am still very much learning about it. I stand to be corrected, but doesn't NHibernate lazy-load by default, i.e. Parent.Child will only be initialised (and the data loaded) when it is accessed.

When deleting the Parent, the relational model that is put in place by using all-delete-orphan means that the Child members related to Parent must be deleted first when deleting Parent. This could explain why they are being loaded.

If you are trying to avoid loading the Children for performance reasons, perhaps implement your own SQL for this special case.

bunn_online
A: 

It is pretty simple, just use cascading deletes on the database. If that is not an option that use a custom query that runs on the database level or just use this method.

NHibernate cannot do this by query, it needs to retrieve each seperate row to delete as it needs the keys of the rows to delete them. That is what makes this expensive.

Just use cascading deletes on the database. It is something in which a database is good at.

Ramon