views:

482

answers:

2

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.

+1  A: 

you can Use HQL to delete multiple objects

Look for delete here - for session.delete example

HQL DELETE example (you can use IN with HQL):

ISession session = sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();

String hqlDelete = "delete Customer c where c.name = :oldName";
// or String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = s.CreateQuery( hqlDelete )
        .SetString( "oldName", oldName )
        .ExecuteUpdate();
tx.Commit();
session.Close();
Dani
+2  A: 

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 FROM myObject o WHERE o.Id = IN :idList")
    .setParameterList("idList", idList)
    .executeUpdate();
joshperry
What if the idList is huge? How would you batch around it? or does nhibernate have some form of built-in batching?
Newbie
I'd have to setup a test using nHibernate profiler [nhprof.com] to see what actually gets emitted to the DB, but if I had to guess I would say that it would look explicitly like your hard-coded example.
joshperry
You can set a batch size in your <hibernate-configuration><session-factory>: <property name="adonet.batch_size">100</property>. However, based on your HQL, I would guess its just one query emitted (batches would apply if you were looping through a collection and calling Delete on them).
ddango