2

我有一个使用 Spring.Net 在 HibernateTemplate 中设置的 NHibernate 拦截器(我使用的是 ASP.NET MVC,fwiw),用于审计。但是,由于某种原因,当我调用 genericDAO.Get(id) 时触发 OnLoad 方法时,当我尝试使用 genericDAO.SaveOrUpdate(object) 保存某些内容时,不会调用 OnSave 或 OnFlushDirty 拦截器方法。有谁知道这可能是为什么?

我已经在 VS 调试器中的方法上设置了断点,所以我很确定拦截器已设置并且我没有错过任何方法调用。显然,一切都被正确保存和检索。

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
    }
}
4

2 回答 2

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>
于 2010-01-28T10:56:27.707 回答
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 spring 的这个相当老的线程也可能会有所帮助。

于 2010-02-15T21:17:44.493 回答