繁体   English   中英

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

[英]NHibernate Many-To-One on Joined Subclass with Filter

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

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>

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

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

感谢CSharper 在用户组的建议

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

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM