【发布时间】: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