优化Castle ActiveRecord生成的删除查询

时间:2011-03-24 04:36:03

标签: castle-activerecord

假设我有Id(主键)列表要删除(例如1,2,3,4)。 使用此查询:

Console.WriteLine ("DELETE DATA :");
ActiveRecordMediator<PostgrePerson>.DeleteAll ("Id IN (1, 2, 3, 4)");

我希望控制台输出为:

DELETE DATA :
NHibernate: DELETE FROM Person WHERE Id IN (1, 2, 3, 4)

,实际的控制台输出是(我使用showsql选项):

DELETE DATA :
NHibernate: select postgreper0_.Id as Id5_, postgreper0_.Name as Name5_, postgreper0_.Age as Age5_, postgreper0_.Address as Address5_ from Person postgreper0_ w
here postgreper0_.Id in (1 , 2 , 3 , 4)
NHibernate: DELETE FROM Person WHERE Id = :p0;:p0 = 1
NHibernate: DELETE FROM Person WHERE Id = :p0;:p0 = 2
NHibernate: DELETE FROM Person WHERE Id = :p0;:p0 = 3
NHibernate: DELETE FROM Person WHERE Id = :p0;:p0 = 4

如何使Castle ActiveRecord生成预期(优化)查询?

更新 这是我根据公认的答案实施的。

int[] idList = GetIdList ();
ActiveRecordMediator<PostgrePerson>.Execute ((session, obj) => {
    string hql = "DELETE PostgrePerson WHERE Id IN (:idList)";
    return session.CreateQuery (hql)
        .SetParameterList ("idList", idList)
        .ExecuteUpdate ();
}, null); 

1 个答案:

答案 0 :(得分:0)

使用Execute callback方法并在NHibernate ISession上运行DML-style HQL DELETE