NHibernate Forge
The official new home for the NHibernate for .NET community

Comments: Contextual data using NHibernate filters

Wiki Page Hierarchy

Pages

Page Details

First published by:
Germán Schuager
on 02-12-2009
Last revision by:
Germán Schuager
on 03-30-2011
4 people found this article useful.
Comments (4)
History (7)

Contextual data using NHibernate filters

Sort by: Published Date | Most Recent | Most Useful
By: Jason Meckley Posted on 11-06-2009 18:29

great article!

I recently used filters with Monorail controller actions. Instead of an interceptor I created a Filter subclass to pull values from the engine context and apply the filter. works like a charm.

By: nieve Posted on 12-20-2009 11:51

Great article indeed!!

However, unless I've missed a point there, it seems the article is somewhat dated- since NH maps enums to strings automatically nowadays, the current code throws an exception when it tries to convert the Context column value (of type nvarchar - string) into int in order to execute the WHERE @p = Context.

What I had to do in order for it to work was replace the filter parameter type adding with this:

filterParametersType.Add("current", NHibernateUtil.String);

and then the SetSession method was replaced with the following code:

session.EnableFilter("contextFilter").SetParameter("current", Context.Current.ToString());

Not a big change, but in case someone gets stuck on this :)

Otherwise, the only thing that I find a bit of a shame is that when replacing the filtering with something like

":current >= Context"

this works on alphabetical rather than enum/numeric order...

By: Dor Rotman Posted on 01-12-2010 6:57

Excellent article!

By: ColinBowern Posted on 09-09-2010 17:22

Just hit a snag with this approach where one of the context-aware objects was referenced by another class.  Filters don't seem to be applied to references when the EntityJoinWalker does it's magic.  According to the Hibernate folks filters should never apply in associations (see opensource.atlassian.com/.../HHH-4026).  Any thoughts?

Powered by Community Server (Commercial Edition), by Telligent Systems