[英]NHibernate ApplyFilter not filtering
I'm trying to define and use an NHibernate filter.我正在尝试定义和使用 NHibernate 过滤器。 I have looked at many other posts on SO.
我看过很多其他关于 SO 的帖子。 As an aside, I am also using FluentNhibernate.
顺便说一句,我也在使用 FluentNhibernate。 When I compare my hbm mapping files, they all look correct.
当我比较我的 hbm 映射文件时,它们看起来都是正确的。 My filter is called IsFlaggedForDelete.
我的过滤器称为 IsFlaggedForDelete。 Here are the relevant files:
以下是相关文件:
filter-def.IsFlaggedForDelete.hbm.xml过滤器定义.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:以下是 class 映射文件的示例:
<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请注意,这些都是通过代码添加的,使用 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.我尝试了不同的配置方法,但是鉴于生成的 xml (对我来说看起来正确),我无法终生弄清楚为什么过滤器不适用。 I have tried wrapping the Filter in a (FluentNH) convention, all to no avail.
我尝试将过滤器包装在(FluentNH)约定中,但无济于事。
Any suggestions are most welcome欢迎任何建议
Thanks谢谢
Filters don't work with Get
method.过滤器不适用于
Get
方法。 For applying filters you should use query API (LINQ/QueryOver/hql/Criteria).要应用过滤器,您应该使用查询 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.原因 -
Get
方法并不总是命中数据库。 It can retrieve object from cache (session or second level) - and in this case it's really hard to apply your filtering logic.它可以从缓存(会话或二级)中检索 object - 在这种情况下,很难应用您的过滤逻辑。 So if object is loaded via
Get
you have to manually check if it's deleted.因此,如果 object 通过
Get
加载,您必须手动检查它是否被删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.