【问题标题】:Bug generating SQL with NHibernate, Bag and Enumerable.Any()?使用 NHibernate、Bag 和 Enumerable.Any() 生成 SQL 的错误?
【发布时间】:2012-02-21 18:53:23
【问题描述】:

我正面临以下情况。

我的对象模型如下:

EntityA --*> EntityB -> EntityC

EntityA 引用EntityB 的列表。 EntityB 引用单个 EntityC

我的映射是:

  <class name="EntityA" table="TAB_A" >
    <id name="Id" column="A_ID" >
      <generator class="native" />
    </id>
    <version name="Version" column="VERSION" />
    <bag name="listOfB" cascade="save-update" table="A_TO_C" lazy="false" >
      <key column="FK_A_ID" />

      <composite-element class="EntityB" >
        <many-to-one name="refToC" class="EntityC" column="FK_C_ID" cascade="save-update" />
      </composite-element>
    </bag>
  </class>

我省略了所有额外的属性和东西。

我查询数据库:

var query = session.Query<EntityA>()
                    .Where(a=> a.listOfB.Any(b => b.refToC == anInstanceOfC));

// count all matches
var count = query.ToFutureValue(t => t.Count());

好的,到目前为止这对我来说看起来不错;)。当我执行查询时,我收到一个错误,即 SQL 语句失败。生成的 SQL 包含以下子语句:

select listOfB1_.A_ID
from A_TO_C listOfB1_
where a0_.A_ID=listOfB1_.FK_A_ID
and listOfB1_.FK_C_ID=?

该语句需要表 A_TO_C 上的列 A_ID(实际上是 TAB_A 的 id 列的列名)。它应该做的是选择列 listOfBl_.FK_A_ID ,它是引用 TAB_A 的 A_ID 列的外键列。

现在的问题是:我错了还是 SQL 生成器?

【问题讨论】:

  • 您是否为表 A 设置了主键,以及其他两个表 A 的辅助键或外键作为主键
  • 其实我不在乎 :D。但是 NHibernate 为 TAB_A 生成了主键 A_ID,为 TAB_C 生成了 C_ID。外键在列 FK_A_ID 和 FK_C_ID 上定义。这正如我预期的那样工作。

标签: c# linq nhibernate


【解决方案1】:

其实是个bug。

another thread on stackoverflow

我的问题和discussion on the nhibernate googlegroup

the actual bug report 在 nhibernate 问题跟踪器中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多