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!
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:
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).