How to effectively delete objects by their identifier in NHibernate?

We use NHibernate as our server side ORM.

Sometimes it becomes necessary to remove an object from the database, considering only the type and identifier of this object. In addition, the deleted object was not previously loaded (therefore, it is not in the session cache or anything else).

Anyway, I use overloading ISession.Delete(query)where the request is as trivial as it is from Dummy where Id=5.

My question is: why does NHibernate retrieve an object before deleting it? As far as I can see, I pay for a double trip to the server for an operation that intuitively should take only one round trip.

Is there a way to remove an object from the database by its type and ID using NHibernate, so it takes only one round?

+3
source share
2 answers

You can also use HQL to remove it yourself.

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

But this will not lead to the removal of cascades, and I assume that this is one of the reasons why it should first load.

+6
source

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

+1
source

Source: https://habr.com/ru/post/1734127/


All Articles