NHibernate Many-To-One on Joined Subclass with Filter
Asked Answered
B

2

10

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>
Blackcap answered 16/6, 2010 at 0:43 Comment(2)
Ran into the same problem today. Ever find a solution?Closer
This is fixed in NH 5.4.2 - sourceCoadjutant
C
1

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

Closer answered 29/7, 2011 at 15:29 Comment(0)
M
0

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>
Markman answered 6/7, 2011 at 12:50 Comment(3)
This doesn't solve the problem. If I query the Parent class deleted items will be filtered, but if I join a bag to the child class they will not be filtered.Closer
I added the filter in the joined-subclass, can you test if that works for you? The nhibernate documentation gives a sample like that for sets.Markman
filter-def is not a valid child element of joined-subclass.Closer

© 2022 - 2024 — McMap. All rights reserved.