I'm trying to define and use an NHibernate filter. I have looked at many other posts on SO. As an aside, I am also using FluentNhibernate. When I compare my hbm mapping files, they all look correct. My filter is called IsFlaggedForDelete. Here are the relevant files:
filter-def.IsFlaggedForDelete.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
<filter-def name="IsFlaggedForDelete" condition=":IsFlaggedForDelete.deletedFlag = IsDeleted">
<filter-param name="deletedFlag" type="Int32" />
</filter-def>
</hibernate-mapping>
Here is an example of a class mapping file:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
<class xmlns="urn:nhibernate-mapping-2.2" name="MyCompany.MyProd.Models.Admin.OneOfMyTables, MyCompany.MyProd.Modules.Service, Version=0.1.2.0, Culture=neutral, PublicKeyToken=c1c38e54126179e5" table="`OneOfMyTables`">
<id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="MyCompany.MyProd.Data.Mapping.Conventions.IdGenerator, MyCompany.MyProd.Core, Version=0.1.2.0, Culture=neutral, PublicKeyToken=c1c38e54126179e5" />
</id>
<property name="IsDeleted" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="IsDeleted" />
</property>
<property name="Code" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Code" />
</property>
<many-to-one cascade="none" class="MyCompany.MyProd.Models.Admin.PartitionAttribute, MyCompany.MyProd.Modules.Bmc.Service, Version=0.1.2.0, Culture=neutral, PublicKeyToken=c1c38e54126179e5" fetch="select" name="PartitionAttribute">
<column name="PartitionAttributeId" />
</many-to-one>
<filter name="IsFlaggedForDelete" condition=":IsFlaggedForDelete.deletedFlag = IsDeleted" />
</class>
</hibernate-mapping>
Note that these are both added via code, using FLUentNHibernate
Within my code, I have this:
Session.EnableFilter("IsFlaggedForDelete").SetParameter("deletedFlag", 0);
var gotten = Session.Get<T>(id);
I have tried different methods to configure, but given the xml generated (which look correct to me) I can't for the life of me work out why the filter isn't applying. I have tried wrapping the Filter in a (FluentNH) convention, all to no avail.
Any suggestions are most welcome
Thanks
Filters don't work with Get
method. For applying filters you should use query API (LINQ/QueryOver/hql/Criteria). Something like:
public TEntity LoadByIdWithFilters<TEntity>(ISession session, object id) where TEntity : class
{
return session.QueryOver<TEntity>().Where(Restrictions.IdEq(id)).SingleOrDefault<TEntity>();
}
Reason - Get
method not always hits the database. It can retrieve object from cache (session or second level) - and in this case it's really hard to apply your filtering logic. So if object is loaded via Get
you have to manually check if it's deleted.