我有一个使用Spring.Net在HibernateTemplate中设置的NHibernate拦截器(我使用的是ASP.NET MVC,fwiw),它用于审计。但是,出于某种原因,当我调用genericDAO.Get(id)时触发OnLoad方法,当我尝试使用genericDAO.SaveOrUpdate(object)保存某些东西时,既不调用OnSave或OnFlushDirty Interceptor方法。谁知道为什么会这样?
我在VS调试器中的方法上设置了断点,所以我非常确定Interceptor已经设置并且我没有错过任何方法调用。显然,一切都在正确保存和检索。
public class AuditInterceptor
: EmptyInterceptor
{
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
// Implementation
// Called when using genericDAO.Get(id)
}
public override bool OnSave(Object entity, Object id, Object[] state,
String[] propertyNames, IType[] types)
{
// Implementation
// NOT called when using genericDAO.SaveOrUpdate(entity)
}
public override bool OnFlushDirty(Object entity, Object id,
Object[] currentState, Object[] previousState,
String[] propertyNames, IType[] types)
{
// Implementation
}
}
答案 0 :(得分:0)
这是我的objects.xml文件的缩减(和匿名)版本:
<object id="MyController" singleton="false" type="Project.Web.Controllers.MyController">
<constructor-arg name="dashboardService" ref="DashboardService" />
</object>
<object id="DashboardService" type="Project.Business.Services.DashboardService, Project.Business">
<constructor-arg name="dashboardDao" ref="DashboardDao" />
</object>
<object id="DashboardDao" type="Project.Data.Dao.DashboardDao, Project.Data">
<property name="SessionFactory" ref="SessionFactory"/>
<property name="HibernateTemplate" ref="myHibernateTemplate" />
</object>
<object id="myHibernateTemplate" type="Spring.Data.NHibernate.Generic.HibernateTemplate, Spring.Data.NHibernate20">
<property name="SessionFactory" ref="SessionFactory"/>
<property name="EntityInterceptor" ref="myAuditInterceptor"/>
<property name="TemplateFlushMode" value="Auto" />
<property name="CacheQueries" value="true" />
</object>
<object id="myAuditInterceptor" type="Project.Business.Audit.AuditInterceptor, Project.Business">
<constructor-arg name="userService" ref="userService" />
<constructor-arg name="auditService" ref="auditService" />
<constructor-arg name="sessionFactory" ref="myAuditSessionFactory" />
</object>
<db:provider id="myDbProvider"
provider="SqlServer-2.0"
connectionString="Data Source=(local);Initial Catalog=myDB;Integrated Security=SSPI" />
<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate20">
<property name="DbProvider" ref="myDbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Project.Data</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect"
value="NHibernate.Dialect.MsSql2005Dialect"/>
<entry key="connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
</dictionary>
</property>
</object>
<object id="myAuditSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate20">
<property name="DbProvider" ref="myDbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Project.Data</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect"
value="NHibernate.Dialect.MsSql2005Dialect"/>
<entry key="connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
</dictionary>
</property>
</object>
答案 1 :(得分:0)
您似乎不使用交易。您应该尝试使用它们,如Spring Nhibernate示例(例如:)中所示,并激活日志记录以真正查看会话是否被刷新。
<!-- Hibernate Transaction Manager-->
<object id="hibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate20">
<property name="DbProvider" ref="myDbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>
<property name="EntityInterceptor" ref="myAuditInterceptor"/>
</object>
您还要声明两个会话工厂。这与我猜不相关,但这是故意的吗? 对于Java的春天,这个rather old thread也许会有所帮助。