我遇到了一些NHibernate Code.Basically問題,我試圖保持會話生存期儘可能短,以儘量減少應用程序中的狀態信息。NHibernate:當持久化對象出現意外會話故障
我覺得很難描述我的問題沒有具體,所以我會繼續前進,並使用文章和類別隱喻。
public class Article
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public override string ToString() { return "cat-" + this.Id.ToString(); }
}
public class ArticleMapping : ClassMap<Article>
{
public ArticleMapping()
{
this.Id(x => x.Id, "id").GeneratedBy.Assigned();
this.Map(x => x.Name, "name");
this.References(x => x.Category).Fetch.Join().Cascade.None();
}
}
public class CategoryMapping : ClassMap<Category>
{
public CategoryMapping()
{
this.ReadOnly();
this.Id(x => x.Id, "id").GeneratedBy.Assigned();
this.Map(x => x.Name, "name");
}
}
我要做的就是創建一個新的條,給它一個名字,從通過下面描述的方法加載列表分配類別並嘗試保存。
我得到以下警告:
無法確定是否CAT-1與分配的標識符是瞬時的或脫
如果你看看模型和映射代碼,你會看到貓1不應該被堅持 - 它是一個類別和定義只讀和非級聯。
//code that loads the list of categories
IStatelessSession session = this.SessionService.GetStatelessSession();
IList<Category> cats = session.CreateCriteria<Category>().List<Category>();
this.SessionService.EndSession(session);
//code that's called to save the instance
ISession session = this.SessionService.GetSession();
using (ITransaction transaction = session.BeginTransaction())
{
session.SaveOrUpdate(article);
transaction.Commit();
}
this.SessionService.EndSession(session);
如果我再次調用Save方法,這一切都香蕉形:
例外:
NHibernate.StaleStateException:意外的行數:0;預計:1
堆棧跟蹤:
在NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(的Int32 rowCount時,IDbCommand的語句) 在NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation期望)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id,Object [] fields, Object [] oldFields,Object rowId,Boolean [] includeProperty,Int32 j,Object oldVersion,Object obj,SqlCommandInfo sql,ISessionImplementor sessi )
at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id,Object [] fields,Object [] oldFields,Object rowId,Boolean [] includeProperty,Int32 j,Object oldVersion,Object obj,SqlCommandInfo sql, ISessionImplementor會話)
在NHibernate.Persister.Entity.AbstractEntityPersister.Update(對象ID,對象[]字段的Int32 []類型的dirtyFieldA第一次機會異常 'NHibernate.StaleStateException' 發生在NHibernate.dll S,布爾hasDirtyCollection ,Object [] oldFields,Object oldVersion,Object obj,Object rowId,ISessionImplementor session)
at NHibernate.Action.Ent ityUpdateAction.Execute()
在NHibernate.Engine.ActionQueue.Execute(IExecutable可執行)
在NHibernate.Engine.ActionQueue。ExecuteActions(IList的列表)
在NHibernate.Engine.ActionQueue.ExecuteActions()
在NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource會話)
我曾經認爲NHibernate的是無黑魔法,但這使我想拜訪我的伏都教牧師向其他領域尋求幫助......
有沒有人有任何想法,哪裏StaleStateException來自?這次我錯了哪裏?
在此先感謝
塞比
我正在做一些不同的事情,但我也有一個觸發器在某些條件下改變了ID。所以,謝謝你回答你自己的問題。你從沮喪時間中拯救了我。 – Krauss 2017-03-22 02:58:55