2

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

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 完全结合吗?

4

1 回答 1

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 提供程序相关的问题。

于 2011-06-18T10:30:08.447 回答