views:

44

answers:

2

Dear ladies and sirs.

We use NHibernate as our ORM on the server side.

Sometimes, there is a need to delete an object from the database, given only the type and ID of that object. In addition, the deleted object was not fetched before (so it is not in the session cache or whatever).

Anyway, I am using the ISession.Delete(query) overload, where the query is as trivial as from Dummy where Id=5.

My question is why does NHibernate fetch the object before deleting it? As far as I can see I am paying for the double round-trip to the server for an operation, which intuitively should take just one round-trip.

Is there a way to delete an object from the database by its type and ID with NHibernate, so that it takes just one round-trip?

Thanks.

+2  A: 

You can also use HQL to delete it yourself.

session.CreateQuery("delete from Table where id = :id")
     .SetParameter("id", id)
     .ExecuteUpdate();

But this will not cascade deletes and I assume that is one reason it needs to load first.

dotjoe
A: 

Do it really hit the db if you call ISession.Delete(Session.Load(type, id)); since Load() should return a proxy and not hit the db?

Kenny Eliasson
yep, my guess is delete function must access proxy in a way that triggers load.
dotjoe
And lazy-loading is on?
Kenny Eliasson