Removing a Collection Using NHibernate Using the Criteria API

I think I know that the answer to this question is likely to be, but I thought that I would continue and ask him anyway.

It seems like in NHibernate, if I do something like this:

IList<Customer> customers = Session.CreateCriteria(typeof(Customer)) .Add(Restrictions.Eq("Name", "Steve") .List<Customer>(); 

And I want to delete this list of clients. From what I can say, the only way to do this is:

 foreach(var customer in customers) { Session.Delete(customer); } 

But I am wondering if there is a way I can just go:

 Session.Delete(customers); 

And delete the entire collection with a single call?

+4
source share
1 answer

Not with criteria, but it's easy to do with HQL:

 session.CreateQuery("delete Customer customer where customer in (:customers)") .SetParameterList("customers", customers.ToArray()) .ExecuteUpdate(); 

But you do not need to download them. You can also do this with one shot:

 session.CreateQuery("delete Customer where Name = 'Steve'") .ExecuteUpdate(); 
+5
source

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


All Articles