Samantha Branham
Samantha Branham

Reputation: 7441

Is it possible to use NHibernate Filters to filter through references?

Contrived example, but let's say I have a these entities:

public class Root
{
    public virtual Customer Customer { get; set; }
}

public class Customer
{
    public virtual CustomerData Data { get; set; }
}

public class CustomerData
{
    public virtual string FooName { get; set; }
}

Now, let's say I want to create a filter for Root based on the value of FooName. Intuitively, I tried this in my FooMap class. Using Fluent mappings.

ApplyFilter("FooNameFilter", "Customer.Data.FooName in (:argument)");

This doesn't work. A SqlClient.SqlException is thrown stating The multi-part identifier "Customer.Data.FooName" could not be bound.

Is there a way to make filters work this way, or am I forced to move that logic into all Query<Root>()s instead?

Upvotes: 2

Views: 3415

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

What could be working, is to move the filter to the CustomerData object if possible, or to create "more sophisticated SQL condition" applied on the Customer mapping. But it is about pure SQL, no references. How do the filters work?

The filters are the same as the where clause, but could be adjusted in a runtime. The extract from documentation 18.1. NHibernate filters

NHibernate adds the ability to pre-define filter criteria and attach those filters at both a class and a collection level. A filter criteria is the ability to define a restriction clause very similiar to the existing "where" attribute available on the class and various collection elements. Except these filter conditions can be parameterized. The application can then make the decision at runtime whether given filters should be enabled and what their parameter values should be. Filters can be used like database views, but parameterized inside the application.

The definition of the where:

where (optional) specify an arbitrary SQL WHERE condition to be used when retrieving objects of this class

Other words, these settings act as "add on" to our mapping. They are extending it (both where and filter) with more SQL balast. The filter could be shared among many mappings and applied to all queries inside one session, but it must target the column:

condition=":myFilterParam = MY_FILTERED_COLUMN"

Upvotes: 1

Related Questions