9

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

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 的连接子类。Childen 被映射为多对一包。该袋子应用了一个名为 SoftDeletableFilter 的过滤器。过滤器映射如下所示:

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

这个问题是,当加载 Other.Children 时,过滤器被应用于子表而不是父表。有没有办法告诉 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>
4

2 回答 2

1

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

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

感谢用户组的CSharper的建议

于 2011-07-29T15:29:19.640 回答
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>
于 2011-07-06T12:50:21.483 回答