2012-10-01 53 views
-1

我無法得到下面的更新在nhibernate的上下文中執行。NHibernate更新列=列 - 1

using(ITransaction transaction = session.BeginTransaction()) 
{ 
    // FIRST I'm GETTING A LIST OF ITEMS IN A MANNER LIKE THIS 
    var itemsToDelete = session.QueryOver<Item>() 
            .Where(i => i.ReferenceObject.Id == otherIdValue) 
            .List<Item>(); 

    // THEN I"M LOOPING THROUGH THEM 
    for(itemToDelete in itemsToDelete) 
    { 

     session.Delete(itemToDelete); 

     using (iDB2Command command = (iDB2Command)_session.Connection.CreateCommand()) 
     { 
      command.CommandText = "update TABLE_NAME set sequence = (sequence - 1) where id = @someId and sequence > @sequenceNumberDeleted"; 
      command.DeriveParameters(); 
      command.Parameters["@someId"].Value = idValue; 
      command.Parameters["@sequenceNumberDeleted"].Value = itemToDelete.Sequence; 
     } 
    } 

    transaction.commit() 
} 

該問題似乎與序列=(序列 - 1)。每次調用NHibernate都會拋出一個「意外的行數」異常。雖然研究大多數與此異常相關的文章是由更新其他行的表上的觸發器引起的。在這種情況下,表格中沒有任何觸發器。此外,如果我替換sequence = 5或其他常量,update語句將毫無問題地執行。

DATE TIME [10] ERROR App.Controllers.AController - Unexpected row count: 2; expected: 1 
DATE TIME [10] ERROR App.Controllers.AController - at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement) 
    at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation) 
    at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState) 
    at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session) 
    at NHibernate.Action.EntityDeleteAction.Execute() 
    at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) 
    at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) 
    at NHibernate.Engine.ActionQueue.ExecuteActions() 
    at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 
    at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) 
    at NHibernate.Impl.SessionImpl.Flush() 
    at NHibernate.Transaction.AdoTransaction.Commit() 
    at App.Controllers.AController.AMethod(Int32[] otherIdValues, Int32 someIdValue, String someReferenceValue) in <path>\App\Controllers\AController.cs:line 117 

有人能幫我指出正確的方向嗎?編輯: 迭戈是非常正確的。這是我得到的衝動。 對這段代碼的實際調用是在一個事務中,然後我調用了對事務的提交。

+0

我看不到正在執行的命令,正在刷新的會話,異常的實際堆棧跟蹤或哪一行拋出... –

回答

0

我已經找到了問題的原因。實際的表述並非按照我認爲的那樣順序生成。相反,update命令在delete命令之前執行,因此delete命令拋出了意外的行計數異常,而不是我之前認爲的更新命令。