Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete multiple db entities with Nhibernate?

What is the best practice for this problem? Is there any batching features built-in?

Sample code:

using (ITransaction transaction = _session.BeginTransaction())
{
   _session.Delete("FROM myObject o WHERE  o.Id = IN(1,2,...99999)");
   transaction.Commit();
}

Thanks in advance.

like image 674
Newbie Avatar asked Dec 08 '09 18:12

Newbie


1 Answers

HQL supports the IN clause, and if you use setParameterList you can even pass in a collection.

var idList = new List<int>() { 5,3,6,7 };

_session.CreateQuery("DELETE MyDataClass o WHERE o.Id IN (:idList)")
    .SetParameterList("idList", idList)
    .ExecuteUpdate();

Be aware, like mentioned by ddango in a comment, that relationship cascades specified in your objects will not be executed since running an HQL query simply translates to a DB query and does not actually load any entity objects.

like image 137
joshperry Avatar answered Oct 09 '22 19:10

joshperry