子类上的NHibernate(3.1)子查询没有连接基类表

时间:2011-05-12 23:28:08

标签: c# oracle nhibernate subquery joined-subclass

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

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>

以下无法在子查询内的join-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完全连接?

1 个答案:

答案 0 :(得分: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提供程序相关的问题。

相关问题