【问题标题】:Executing multiple updates in a single NHibernate HQL statement在单个 NHibernate HQL 语句中执行多个更新
【发布时间】:2010-02-08 10:01:02
【问题描述】:

我想在 HQL 中编写以下 SQL,以便它作为单个语句执行:

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。我无法在 MultiQuery 块和 List 中运行它,因为我得到以下异常:

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()

我似乎找不到与 ExecuteUpdate 等效的 MultiQuery。有任何想法吗?

【问题讨论】:

  • 如果你只是在一个事务中两者都做呢?这将是两个语句,但至少它是原子的。
  • 已经完成了,但这更像是一个'想知道如何'类型的问题。在这种情况下,它只有 2 条语句,但可以想象我以后会想要这样做。

标签: nhibernate hql


【解决方案1】:

似乎不可能(在 NH 2.1.2 中),因此不得不求助于使用 CreateSQLQuery 和 ExecuteUpdate :(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多