How to delete multiple db entities with Nhibernate?

前端 未结 3 1251
情书的邮戳
情书的邮戳 2021-02-05 13:47

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

Sample code:

using (ITransaction transaction = _session.BeginTransa         


        
3条回答
  •  遥遥无期
    2021-02-05 14:37

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

    var idList = new List() { 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.

提交回复
热议问题