我剛剛開始一個新項目,並試圖實現ISaveOrUpdateEventListener以在我的實體上設置datecreated和dateupdated字段。Nhibernate遇到了一些麻煩ISaveOrUpdateEventListener
我一直在試圖遵循解決方案的簡化版本,在這裏找到: http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html
這裏是我的聽衆:
Namespace Repositories
Public Class UpdateAndInsertEventListener
Implements ISaveOrUpdateEventListener
Public Sub OnSaveOrUpdate([event] As NHibernate.Event.SaveOrUpdateEvent) Implements NHibernate.Event.ISaveOrUpdateEventListener.OnSaveOrUpdate
Dim entity = TryCast([event].Entity, IDateCreatedAndUpdated)
If entity IsNot Nothing Then
Dim dateUpdated = DateTime.Now
If entity.DateCreated Is Nothing Then
entity.DateCreated = dateUpdated
End If
entity.DateUpdated = dateUpdated
End If
End Sub
End Class
End Namespace
,這裏是我的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string"></property>
<property name="show_sql">true</property>
<event type="save-update">
<listener class="Project.Repositories.UpdateAndInsertEventListener, Project"/>
</event>
</session-factory>
</hibernate-configuration>
我的問題是,它從來沒有在我的偵聽器中運行代碼,所以它試圖將null插入到我的db :(我試圖把中斷點,但他們從未被擊中。
我想我錯過了某個步驟,但我不知道在哪裏。
這裏是從失敗測試的堆棧跟蹤:
System.Data.SqlClient.SqlConnection.OnError(SqlException異常,布爾breakConnection) System.Data.SqlClient.SqlInternalConnection.OnError(SqlException異常,布爾breakConnection) System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand的cmdHandler,SqlDataReader的數據流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj) System.Data.SqlClient.SqlDataReader.ConsumeMetaData() System.Data.SqlClient.SqlDataReader.get_MetaData() System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader的DS,RunBehavior runBehavior,字符串resetOptionsString) System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布爾returnStream,布爾異步) System.Data.SqlClient的。 SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result) System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法) System.Data.SqlClient.SqlCommand .ExecuteReader(CommandBehavior行爲,String方法) System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) System.Data.Common.DbCommand.System.Data.IDbComm and.ExecuteReader() NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand的CMD) NHibernate.Id.IdentityGenerator.InsertSelectDelegate.ExecuteAndExtract(IDbCommand的插入,ISessionImplementor會話) NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL,ISessionImplementor會話,IBinder binder) NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL,ISessionImplementor session,IBinder binder) NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object [] fields,Boolean [] notNull,SqlCommandInfo sql,Object obj ,ISessionImplementor會話) NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object [] fields,Object obj,ISessionImplementor session) NHibernate.Action.EntityIdentityInse rtAction.Execute() NHibernate.Engine.ActionQueue.Execute(IExecutable可執行) NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(對象實體,的EntityKey鍵,IEntityPersister持留,布爾useIdentityColumn,對象什麼,IEventSource源,布爾requiresImmediateIdAccess) NHibernate.Event.Default.AbstractSaveEventListener。執行保存(Object entity,Object id,IEntityPersister persister,Boolean useIdentityColumn,Object anything,IEventSource source,Boolean requiresImmediateIdAccess) NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity,String entityName,Object anything,IEventSource source,Boolean requiresImmediateIdAccess) NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent事件) NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent事件) NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent事件) NHibernate.Event.Default.DefaultSaveEventListener。 PerformSaveOrUpdate(SaveOrUpdateEvent事件) NHibernate.Event.Default.DefaultSaveOrUpdateEven tListener.OnSaveOrUpdate(SaveOrUpdateEvent事件) NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent事件) NHibernate.Impl.SessionImpl.Save(對象OBJ) Project.Repositories.UserRepository.Add在C(用戶用戶):\項目\項目\項目\庫\ UserRepository.vb:線10 Project.Tests.CRUDTest.CRUDTest()在C:\項目\項目\ Project.Tests \庫\ UserRepository \ CRUDTest.vb:線50
感謝您的幫幫我!
編輯:我已經改變了我的幫手,在那裏添加了監聽器。它似乎在爲更新工作,但是當我創建新實體時,它似乎還沒有到達聽衆的位置。我還去掉了「註冊」子從我
Imports NHibernate
Namespace Repositories
Public Class NhibernateHelper
Private Shared m_sessionFactory As ISessionFactory
Private Shared ReadOnly Property SessionFactory As ISessionFactory
Get
If m_sessionFactory Is Nothing Then
Dim config As New NHibernate.Cfg.Configuration
config.Configure("C:/Projects/Project/Project.Tests/hibernate.cfg.xml")
config.AddAssembly(GetType(Entities.User).Assembly)
config.EventListeners.SaveEventListeners = config.EventListeners.SaveEventListeners.Concat({New UpdateAndInsertEventListener}).ToArray
config.EventListeners.SaveOrUpdateEventListeners = config.EventListeners.SaveOrUpdateEventListeners.Concat({New UpdateAndInsertEventListener}).ToArray
config.EventListeners.UpdateEventListeners = config.EventListeners.UpdateEventListeners.Concat({New UpdateAndInsertEventListener}).ToArray
m_sessionFactory = config.BuildSessionFactory
End If
Return m_sessionFactory
End Get
End Property
Public Shared Function OpenSession() As ISession
Return SessionFactory.OpenSession()
End Function
End Class
End Namespace
我已經修復了這些問題,但它永遠不會到達他們! – Patricia
如果沒有找到它們,那麼Register方法永遠不會被調用。你的配置代碼是什麼樣的? – eulerfx
xml是在問題中,我必須做一些不同於我的nhibernate助手嗎? (我已將它添加到問題中) – Patricia