【问题标题】:NHibernate (3.1) subquery on subclass not joining base class table子类上的 NHibernate (3.1) 子查询未加入基类表
【发布时间】:2011-05-12 23:28:08
【问题描述】:

以下是我遇到的一个实际问题的抽象。

public class Base
{
    public virtual string Id { get; set; }
    public virtual string Foo { get; set; }
}

public class Sub : Base
{
    public virtual string Bar { get; set; }
    public virtual Other Other { get; set; }
}

public class Other
{
    public virtual string Id { get; set; }
    public virtual ICollection<Sub> Subs { get; set; }
}

映射:

<class name="Base" table="base_table" xmlns="urn:nhibernate-mapping-2.2">
  <id name="Id" column="id">
    <generator class="assigned" />
  </id>
  <property name="Foo" column="Foo" />

  <joined-subclass name="Sub" table="sub_table">
    <key column="id" />
    <property    name="Bar"   column="Bar" />
    <many-to-one name="Other" column="other_id" />
  </joined-subclass>

</class>

<class name="Other" table="other_table" xmlns="urn:nhibernate-mapping-2.2">
  <id name="Id" column="id">
    <generator class="assigned" />
  </id>
  <set name="Subs" inverse="true" lazy="true">
    <key column="other_id" />
    <one-to-many class="Sub" />
  </set>
</class>

以下方法无法对子查询内的joined-subclass进行连接:

Session.Query<Other>().Where(o => o.Subs.Any(s => s.Foo == "xyz"));

SQL

select
    other0_.id as id60_
from
    other_table other0_
where
    exists (
        select
            subs1_.id
        from
            sub_table subs1_
        where
            other0_.id=subs1_.other_id
            and subs1_1_.Foo=:p0
    );
:p0 = 'xyz' [Type: String (0)]

抛出 GenericADOException,因为子查询中的 subs1_1_(例如 sub_table)没有 Foo。 我必须在 Other 的映射中做些什么才能使 Subs 与子查询中的 Base 完全连接?

【问题讨论】:

  • 请不要使用
     格式化代码
  • 你有准确的 SQL 吗?可能只是延迟加载 Base 属性。
  • 好的.. 我已经添加了我得到的确切 sql。
  • 您是否映射了您映射的实体的基本属性?
  • 是的..你可以看到 Foo 映射在 Base 中。

标签: c# oracle nhibernate subquery joined-subclass


【解决方案1】:

可能是与加入的子类映射相关的 linq 提供程序问题。该查询适用于每个层次结构的表。试试这个:

<class name="Base" table="base_table" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" column="id">
        <generator class="assigned" />
    </id>

    <discriminator column="Type" type="String" />

    <property name="Foo" column="Foo" />

    <subclass name="Sub" discriminator-value="Sub">
        <property    name="Bar"   column="Bar" />
        <many-to-one name="Other" column="other_id" />
    </subclass>
</class>

<class name="Other" table="other_table" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" column="id">
        <generator class="assigned" />
    </id>
    <set name="Subs" inverse="true" lazy="true">
        <key column="other_id" />
        <one-to-many class="Sub" />
    </set>
</class>

不幸的是,在所有情况下,每个层次结构的表都不能替换连接的子类...

在 NH jira 中注册了一些看起来相似的问题(NH-2564NH-2491),但不确定它们是否真的相关。

编辑: HQL 也有同样的问题:from Other o where exists(from o.Subs s where s.Foo = 'xyz') 因此这不是与 linq 提供程序相关的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多