2010-01-21 87 views
2

我有一個NHibernate攔截器,使用Spring.Net(我使用ASP.NET MVC,fwiw)在HibernateTemplate中設置,用於審計。但是,出於某種原因,當我調用genericDAO.Get(id)時觸發OnLoad方法時,當我嘗試使用genericDAO.SaveOrUpdate(object)保存某些內容時,不會調用OnSave或OnFlushDirty Interceptor方法。任何人都知道這可能是爲什麼?保存時不使用NHibernate攔截器

我已經在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 
    } 
} 
+0

你能展示一些你的配置嗎? – BennyM 2010-01-24 11:44:09

+0

我已經複製到下面的objects.xml文件中。還有什麼你認爲有助於看到的? – LucyB 2010-01-28 10:58:19

回答

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> 
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也許是有幫助的。

+0

謝謝,我會和交易管理員一起玩。 當攔截器回寫數據庫時,兩個會話工廠有意避免出現問題。 – LucyB 2010-02-16 11:06:08