2010-02-08 136 views
4

我還想寫在HQL以下SQL所以執行單個語句:在一個NHibernate的HQL語句執行多次更新

update child_thingy c 
set c.parent_thingy_id = null 
where c.common_thingy_id = @common_thingy_id 

delete 
from parent_thingy p 
where p.common_thingy_id = @common_thingy_id 

我翻譯的SQL HQL如下:

update ChildThingy c 
set c.ParentThingy = null 
where c.CommonThingy = :commonThingy 

delete 
from ParentThingy p 
where c.ParentThingy = :commonThingy 

我想運行這個作爲單個語句,但我不能在一個單一的HQL塊使用CreateQuery & ExecuteUpdate。

System.NullReferenceException: Object reference not set to an instance of an object. 
at NHibernate.Impl.MultiQueryImpl.AggregateQueriesInformation() 
at NHibernate.Impl.MultiQueryImpl.get_Parameters() 
at NHibernate.Impl.MultiQueryImpl.CreateCombinedQueryParameters() 
at NHibernate.Impl.MultiQueryImpl.List() 

我似乎無法找到一個MultiQuery等價的executeUpdate的:我得到以下例外,我不能運行這個無論是在MultiQuery塊列表。有任何想法嗎?

+2

如果有什麼你只是在一個事務中都做了?這會是兩種說法,但至少它會是原子的 – dotjoe 2010-02-08 21:32:01

+0

已經完成了,但這更像是一個「想知道如何」類型的問題,在這種情況下,它只有兩個語句,但可以想象的是,我想要爲更多稍後的。 – MPritchard 2010-02-09 07:37:42

回答

0

似乎沒有可能(在NH 2.1.2),因此不得不求助於使用createSQLQuery和executeUpdate的:(