Unlike other OR/Ms – which, as always, shall remain unnamed – NHibernate offers a couple of ways to automatic filter results. Basically, we have two options:
Because filters offer everything that static restrictions do and more, we’ll focus on filters.
A filter can specify a restriction, in terms of a SQL clause, to either an entity as a whole (the class, not a specific query) or to a collection (bag, set, list, map, array, etc).
For example, imagine you have a table that holds values that can be translated and a translation table for that purpose:
You will want to retrieve only the translation for the current culture. A domain model could look like:
We would like to apply a restriction to the Translations property of Translatable, so as to filter the translations by the current culture.
First, we need to create a filter, this is done at Configuration level:
The restriction can be defined on the filter itself, or per entity or collection. In this case, I didn’t specify it on the filter (string.Empty), so I will need to do it at the collection level:
A filter needs to be explicitly made active, and, if it contains parameters, all of its parameters must be set:
Now, whenever the Translations collection is retrieved, either through a SELECT or an INNER JOIN, the “Culture = :Culture” restriction - where, of course, :Culture is replaced by the current parameter value - will be applied automatically, together with the foreign key restriction.
In this example, I define the restriction string on the filter itself, and I apply it to both the Record entity and its Children collection. This time, no parameters, but we still need to enable the filter before we issue a query:
If for any reason you want to disable a filter, it’s easy:
Or even get its definition:
Enjoy your filters!