Filter a Bag of Joined-Subclass

77 views
Skip to first unread message

Joel P

unread,
Jun 29, 2011, 2:55:38 PM6/29/11
to nhusers
I just ran into a problem today which is quite well described in this
SO question: http://stackoverflow.com/q/3050050/65611

Is there anyway to filter a bag of joined-subclasses based upon a
super-class property?

Using the example from the OP my mappings look a bit like this.

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

<class name="Other">
...
<bag name="Children">
<key column="OtherId" />
<one-to-many class="Child" />
<filter name="deletedFilter" condition="IsDeleted = 0" />
</bag>
</class>

When I try to use this code I get the error: "Invalid column name
'IsDeleted'" which makes perfect sense since the filter would be
applied as sql not hql. However I need a way to filter based on a
column from the super-class table. I've tried using the parent class
table name (condition="Parent.IsDeleted = 0") but I get "The multi-
part identifier 'Parent.IsDeleted" could not be bound." doubtless
because the table is aliased.

Any solutions to this issue?

CSharper

unread,
Jul 12, 2011, 2:05:59 AM7/12/11
to nhusers
I am not experienced with those filters but if it's some kind of raw
SQL you can write in the conditions, you could try a subselect
"ParentId NOT IN (SELECT ParentId FROM Parent WHERE IsDeleted = 1)"

Joel P

unread,
Jul 13, 2011, 9:40:57 AM7/13/11
to nhusers
That's a good idea. I will have to try that approach. My only concern
would be performance.

On Jul 12, 2:05 am, CSharper <csharper2...@googlemail.com> wrote:
> I am not experienced with those filters but if it's some kind of raw
> SQL you can write in the conditions, you could try a subselect
> "ParentId NOT IN (SELECT ParentId FROM Parent WHERE IsDeleted = 1)"
>
> On 29 Jun., 20:55, Joel P <ithiel...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I just ran into a problem today which is quite well described in this
> > SO question:http://stackoverflow.com/q/3050050/65611
>
> > Is there anyway tofiltera bag of joined-subclasses based upon a
> > super-classproperty?
>
> > Using the example from the OP my mappings look a bit like this.
>
> > <classname="Parent">
> >   <id .. />
> >   <property name="IsDeleted" type="System.Boolean">
> >     <column name="IsDeleted" />
> >   </property>
> >   <joined-subclass name="Child">
> >     <key>
> >       <column name="ParentId" />
> >     </key>
> >     ...
> >   </joined-subclass>
> > </class>
>
> > <classname="Other">
> >   ...
> >   <bag name="Children">
> >     <key column="OtherId" />
> >     <one-to-manyclass="Child" />
> >     <filtername="deletedFilter" condition="IsDeleted = 0" />
> >   </bag>
> > </class>
>
> > When I try to use this code I get the error: "Invalid column name
> > 'IsDeleted'" which makes perfect sense since thefilterwould be
> > applied as sql not hql. However I need a way tofilterbased  on a
> > column from the super-classtable. I've tried using the parentclass

Joel P

unread,
Jul 29, 2011, 11:26:21 AM7/29/11
to nhusers
This works with one slight change. There seems to be some attempt on
NHibernate's part to map the columns in the filter condition. I had to
change it to:

"ParentId NOT IN (SELECT p.ParentId FROM Parent p WHERE p.IsDeleted =
1)"

On Jul 13, 9:40 am, Joel P <ithiel...@gmail.com> wrote:
> That's a good idea. I will have to try that approach. My only concern
> would be performance.
>
> On Jul 12, 2:05 am, CSharper <csharper2...@googlemail.com> wrote:
>
>
>
>
>
>
>
> > I am not experienced with those filters but if it's some kind of raw
> > SQL you can write in the conditions, you could try a subselect
> > "ParentId NOT IN (SELECT ParentId FROM Parent WHERE IsDeleted = 1)"
>
> > On 29 Jun., 20:55, Joel P <ithiel...@gmail.com> wrote:
>
> > > I just ran into a problem today which is quite well described in this
> > > SO question:http://stackoverflow.com/q/3050050/65611
>
> > > Is there anyway tofiltera bag ofjoined-subclasses based upon a
> > > super-classproperty?
>
> > > Using the example from the OP my mappings look a bit like this.
>
> > > <classname="Parent">
> > >   <id .. />
> > >   <property name="IsDeleted" type="System.Boolean">
> > >     <column name="IsDeleted" />
> > >   </property>
> > >   <joined-subclassname="Child">
Reply all
Reply to author
Forward
0 new messages