NHibernate 多对一连接子类与过滤器

发布于 2024-09-06 06:53:22 字数 915 浏览 11 评论 0原文

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

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>

I have a class setup that looks something like this:

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 is mapped as a joined-subclass of Parent.
Childen is mapped as a Many-To-One bag. The bag has a filter applied to it named SoftDeletableFilter. The filter mapping looks like:

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

That problem is that when Other.Children is loaded the filter is being applied to the Child table and not the parent table. Is there any way to tell NHibernate to apply the filter to the parent class?

Edit:
Here's the parent mapping:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-09-13 06:53:22

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

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

感谢 CSharper 在用户组获取建议

Finally found an answer to this. Perhaps not the most performance friendly approach, but you can rewrite your filter condition as a subquery:

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

Thanks to CSharper over at the usergroup for the suggestion

如梦 2024-09-13 06:53:22

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

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

you need to add the filter to the parent class:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文