使用过滤器连接子类的NHibernate多对一

时间:2010-06-16 00:43:43

标签: c# nhibernate filter joined-subclass

我有一个类设置,看起来像这样:

public abstract class Parent
{
    public virtual bool IsDeleted { get; set; }
}

public class Child : Parent
{
}

public class Other
{
    public virtual ICollection<Child> Children { get; set; }
}

Child被映射为Parent的join-subclass。 Childen被映射为多对一包。这个包有一个名为SoftDeletableFilter的过滤器。过滤器映射如下所示:

<filter-def name="SoftDeleteableFilter" condition="(IsDeleted = 0 or IsDeleted is null)" />

问题在于,当加载Other.Children时,过滤器将应用于Child表而不是父表。有没有办法告诉NHibernate将过滤器应用于父类?

编辑: 这是父映射:

<class name="Parent">
  <id ..
  <property name="IsDeleted" type="System.Boolean">
    <column name="IsDeleted" />
  </property>
  <joined-subclass name="Child">
    <key>
      <column name="ParentId" />
    </key>
    ...
  </joined-subclass>
</class>

2 个答案:

答案 0 :(得分:1)

终于找到了答案。也许不是性能最友好的方法,但您可以将过滤条件重写为子查询:

ParentId in (Select p.ParentId from Parent p where p.IsDeleted = false)

感谢CSharper超过at the usergroup的建议

答案 1 :(得分:0)

您需要将过滤器添加到父类:

<class name="Parent">
  <id ..
  <property name="IsDeleted" type="System.Boolean">
    <column name="IsDeleted" />
  </property>
  <joined-subclass name="Child">
    <key>
      <column name="ParentId" />
    </key>
    **<filter-def name="SoftDeleteableFilter" condition="(IsDeleted = 0 or IsDeleted is null)" />**
  </joined-subclass>
  **<filter-def name="SoftDeleteableFilter" condition="(IsDeleted = 0 or IsDeleted is null)" />**
</class>