views:

250

answers:

2

I am using NHibernate 2.1. I am trying to use a filter in a property formula, but am getting the following error:

filter-def for filter named 'SiteFilter' was never used to filter classes nor collections.

Here is my mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataAccess" namespace="DataAccess.Catalog">
  <class name="Model.Catalog.Category,Model" table="Catalog.Category">

    <id name="ID" column="ID" type="Int32" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" type="string" length="50" not-null="true" />

    <property name="ProductCount" formula="(SELECT COUNT(*) from Catalog.Product WHERE Product.CategoryID = ID)" lazy="true" />
    <property name="SiteProductCount" formula="(SELECT COUNT(*) from Catalog.Product WHERE Product.CategoryID = :SiteFilter.SiteID)" lazy="true" />

    <many-to-one name="Image"
                 column="ImageID"
                 not-null="true"
                 class="Model.Catalog.Image,Model"
                 cascade="save-update" />

    <bag name="Products" table="Catalog.Product" generic="true" inverse="true">
      <key column="CategoryID" />
      <one-to-many class="Model.Catalog.Product,Model"/>
    </bag>

  </class>

  <filter-def name="SiteFilter">
    <filter-param name="SiteID" type="Int32" />
  </filter-def>
</hibernate-mapping>

What am I doing wrong? Thanks for any help!

A: 

I think you need to attach the filter within the class binding.

  <class name="Model.Catalog.Category,Model" table="Catalog.Category">
    ...
    <filter name="SiteFilter" condition=":SiteID = CategoryID"/>
  </class>

You can also enable the filter on a session by session basis:

session.EnableFilter("SiteFilter").SetParameter("SiteID", product.Category.ID)

Additional info/examples: https://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/filters.html

Turns out you can enable the filter at the beginning of your app lifecycle as well: http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx

ddango
Thanks for the reply. The Ayende blog is what I used originally to try to make this work. Everything I have matches his - he doesn't attach the filter in the class binding - he is using it in the formula just like I'm trying to do.
Mike C.
One difference I'm noticing is that your properties are flagged lazy, whereas ayende's are not. as per http://stackoverflow.com/questions/1456454/how-to-add-a-hibernate-property-thats-really-a-query
ddango
I took that off and still got the error.
Mike C.
+1  A: 

Checking the NHiberante source it appears that filters are not intended to be used in formulas.

The nhibernate 2.0 documentation does not mention that you can use filters like this.

Filters are intended to be used for entities and in collection mappings, like this:

<class name="MyClass" ...>
   ...
   <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</class>

<set ...>
 <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
</set>**

The exception "filter-def for filter named 'SiteFilter' was never used to filter classes nor collections." is being thrown after all configuration files have been read and a filter definition is found that is not used, that it is being used in a formula like you use here is not recognized.

Sorry to come with bad news :) If you feel that this is an important feature, please add it as feature request in the nhibernate jira (nhjira.koah.net).

Torkel
hmmm, thanks for the message. It appears this undocumented feature when away from 1.x to 2.x.
Mike C.