保存时不使用 NHibernate 拦截器

发布于 2024-08-19 01:30:29 字数 1030 浏览 10 评论 0原文

我有一个使用 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
    }
}

I have a NHibernate Interceptor that is set in the HibernateTemplate using Spring.Net (I'm using ASP.NET MVC, fwiw), which is used for Auditing. However, for some reason, while the OnLoad method is being triggered when I call genericDAO.Get(id), when I try to save something using genericDAO.SaveOrUpdate(object) neither the OnSave or OnFlushDirty Interceptor methods are called. Anyone know why this might be?

I've set breakpoints on the methods within VS debugger, so I'm pretty certain the Interceptor is set and that I'm not missing any method calls. Obviously, everything is being saved and retrieved correctly too.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳根太软 2024-08-26 01:30:29

这是我的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>

This is a cut-down (and anonymised) version of my objects.xml file:

  <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>
哥,最终变帅啦 2024-08-26 01:30:29

看来你没有使用交易。您应该尝试使用它们,如 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 的相当旧的线程可能会有所帮助。

It seems that you do not use transactions. You should try using them as shown in the Spring Nhibernate sample (e.g.:) and activate logging to really see if the session gets flushed.

<!-- 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>

Also you are declaring two session factories. It is not related I guess but is this intentional?
Also this rather old thread for Java's spring might be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文