开发者

Deleting a Collection with NHibernate Using the Criteria API

I think I know what the answer to this question is probably going to be, but I thought I'd go ahead and ask it anyway.

It appears that within 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 then delete that list of customers. From what I can tell the only way to do it is like this:

foreach(var customer in customers开发者_运维技巧)
{
    Session.Delete(customer);
}

But what I'm wondering is if there's any way I can just go:

Session.Delete(customers);

And delete the entire collection with a single call?


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 don't need to load them. You can also do it in one shot:

session.CreateQuery("delete Customer where Name = 'Steve'")
       .ExecuteUpdate();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜