This condition in the QueryOver
:
...
.JoinQueryOver<Registration>(p => p.Registrations)
.Where(r => r.IsDropped == false)
...
Is in fact a WHERE clause for a Person. I mean, this condition is deciding if there is such a Person
, having at least one Registration
which is NOT dropped. It is not a way, how to filter the Registration colleciton. BUT:
NHibernate has two nice ways how to filter collections themselves. These are where clause and filter.
1. where clause
Easily said: we can extend the collection mapping, whit a SQL Statement, which will be always added as a where condition. See where 6.2. Mapping a Collection, extract:
where="arbitrary sql where condition"
== (optional) specify an arbitrary SQL WHERE condition to be used when retrieving or removing the collection (useful if the collection should contain only a subset of the available data)
so in our case the mapping:
HasMany<Registration>(
...
.Where("IsDropped = 0"); // SQL Statement
2. filter setting
While where clause is static, baked into the assembly/mapping... filter is a way how to do that dynamically. So just imagine, that we would sometimes like to have a set of Registrations
, which are dropped, next time, which are not dropped.
We would like to be able to change "IsDropped = 0"
to "IsDropped = 1"
(better scenario could be switch by culture or lang). And that's where the filter could be applied, see 18.1. NHibernate filters
There is a small draft of the filter stuff with fluent, see the links below to get a complete picture.
HasMany
...
.ApplyFilter<MyFilter>()
And filter could be like:
public class MyFilter: FilterDefinition
{
public MyFilter()
{
WithName("dropped")
.WithCondition("IsDropped == :isDropped")
.AddParameter("isDropped", NHibernate.NHibernateUtil.Boolean);
}
}
And finally, we can enable the Filter for a whole session (coool, all collections will be filtered the same way in that transaction/session, all collection mapped with MyFilter
)
session.EnableFilter("dropped").SetParameter("isDropped", false);
See more here: