Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,125 | Comments: 45,492

filter by tags archive

NHibernate Filters

time to read 3 min | 505 words

One of the more interesting ability of NHibernate is to selectively filter records based on some global filters. This allow us to very easily create global where clauses that we can flip on and off at the touch of a switch.

Let us take a look at see what I mean.

We define the filter effectiveDate:

<filter-def name="effectiveDate">
	<filter-param name="asOfDate" type="System.DateTime"/>

A filter definition is most commonly just a set of parameters that we can define, which will later be applied to in the appropriate places. An example of an appropriate place would be Post.PostedAt, we don’t want to show any post that was posted at a later time than the effective date. We can define this decision in the mapping, like this:

<class name="Post"
		<id name="Id">
			<generator class="identity"/>
		<property name="Title"/>
		<property name="Text"/>
		<property name="PostedAt"/>
		<filter name="effectiveDate"
						condition=":asOfDate >= PostedAt"/>

And now we can start play:


	.SetParameter("asOfDate", DateTime.Now);


Who do you think this will generate?

Well, the first query is pretty easy to understand:


But the second one is much more interesting:


We have selectively applied the filter so only posted posted after the 16th can be seen.

This is a very powerful capability to have, since we can use this globally, to define additional condition. For that matter, we can apply it in multiple places, so comments would also be so limited, etc.

For that matter, we can also put filters on associations as well:

<set name="Comments"
	<key column="PostId"/>
	<one-to-many class="Comment"/>
	<filter name="effectiveDate"
					condition=":asOfDate >= PostedAt"/>

And trying to access the Comments collection on a Post would generate the following SQL when the filter is active:


Nice, isn’t it?


Frank Quednau

Hi Ayende,

would this be the right feature to leverage when implementing data restrictions based on user privileges?

Erik van Brakel

Wow, this is one feature I didn't know of. Thanks!

Ayende Rahien


Depending on what you are doing.

I would tend to use Rhino Security for such tasks


Filters are certainly very useful especially when dealing with records that expire or can have a temporary invalid/restricted state.

What I really don't like is that Session.Get and Session.Load commands ignore filters. Session.Load is not much of a problem because it is only being used when it is known that the record exists. But I always end up reimplementing the Get() method in the repository using a query to make sure developers do not accidentally load an invalid record and probably ending up losing some optimization benefits.


Hi Ayende,

is it possible to use this filter with properties residing deeper in the objectgraph, for example on a
<many association within the filtered bag?

something like this, where Edits would be a mapped <many-to-one holding the timestamp:

<filter= Edits.EditTS"/>


Sorry, that did not work. should have masked the xml.

Here is the example in pure text:

class blog

has a list of comments

class comment

has a list of edits

class edit

has a edit-timestamp property



condition=":asOfDate >= Edits.EditTS"

would this be possible using filters?

Comment preview

Comments have been closed on this topic.


  1. RavenDB 3.5 Whirlwind tour: I need to be free to explore my data - 13 hours from now
  2. RavenDB 3.5 whirl wind tour: I'll have the 3+1 goodies to go, please - 4 days from now
  3. The design of RavenDB 4.0: Voron has a one track mind - 5 days from now
  4. RavenDB 3.5 whirl wind tour: Digging deep into the internals - 6 days from now
  5. The design of RavenDB 4.0: Separation of indexes and documents - 7 days from now

And 11 more posts are pending...

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    05 May 2016 - Physically segregating collections
  2. RavenDB 3.5 whirl wind tour (14):
    04 May 2016 - I’ll find who is taking my I/O bandwidth and they SHALL pay
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats