2012-07-14 31 views
8

我收到錯誤錯誤脫水屬性值,同時在更新期間提交。我已經搜查,它類似於NHibernate: Error dehydrating property - What the heck is this?唯一的區別是,在refered問題NHibernate的抱怨的無法解析財產NHibernate - 錯誤脫水屬性值

價值IssuingOffice已經在數據庫中存在,因此它不能被引用的一個問題未保存記錄

以下是詳細錯誤。

Test 'Tests.Services.StickerInvoiceServiceTests.update_sticker_info_succeeds' failed: 
NHibernate.PropertyValueException : Error dehydrating property value for 
Model.StickerInvoice.StickerIssuingOffice 
----> NHibernate.TransientObjectException : object references an unsaved transient 
instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Model.IssuingOffice, Entity: Model.IssuingOffice 
at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) 
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 session) 
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 session) 
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session) 
at NHibernate.Action.EntityUpdateAction.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() 
UnitOfWork.cs(39,0): at NhRepository.UnitOfWork.Commit() 
StickerInvoiceService.cs(73,0): at Services.StickerInvoiceService.UpdateStickerInfo(StickerInvoice entity, IEnumerable`1& brokenRules) 
Services\StickerInvoiceServiceTests.cs(131,0): at Tests.Services.StickerInvoiceServiceTests.update_sticker_info_succeeds() 
--TransientObjectException 
at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session) 
at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) 
at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) 
at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) 

的映射IssuingOffice

<class name="IssuingOffice" table="IssuingOffice"> 
<id name="Id"> 
    <generator class ="hilo"> 
    <param name ="table">IdGenerator</param> 
    <param name ="column">NextHigh</param> 
    <param name ="max_lo">2</param> 
    <param name ="where">TableKey = 'IssuingOffice'</param> 
    </generator> 
</id> 
<version name="Version" column="Version" /> 
<property name ="Name" length="150" not-null="true" unique="true" /> 
<bag name ="IssuedStickers" table ="StickerInvoice" generic="true" inverse="true"> 
    <key column ="StickerIssuingOfficeId" /> 
    <one-to-many class ="StickerInvoice"/> 
</bag> 

架構爲IssuingOffice

CREATE TABLE IssuingOffice(
Id int NOT NULL, 
Name nvarchar (150) NOT NULL, 
Version int NOT NULL, 
    CONSTRAINT PK_IssuingOffice PRIMARY KEY (Id ASC) 
) 

的映射StickerInvoice

<class name="StickerInvoice" table="StickerInvoice"> 
<id name="Id"> 
    <generator class ="hilo"> 
    <param name ="table">IdGenerator</param> 
    <param name ="column">NextHigh</param> 
    <param name ="max_lo">5</param> 
    <param name ="where">TableKey = 'StickerInvoice'</param> 
    </generator> 
</id> 
<version name ="Version" /> 
<property name ="RefNo" length="50" not-null="true" /> 
<property name ="Period" not-null="true" /> 
<property name ="Amount" not-null="true"/> 
<property name ="DueDate" not-null="true"/> 
<property name ="Penalty" not-null="true"/> 
<property name ="InvoiceNo" length="50"/> 
<property name ="DateIssued" /> 
<property name ="ReceiptNo" length="50" /> 
<property name ="DatePaid" /> 
<property name ="StickerNo" length="50" /> 
<many-to-one name ="Vehicle" class="Vehicle" column ="VehicleId" /> 
<many-to-one name ="StickerIssuedBy" class="User" column ="StickerIssuedById" /> 
<many-to-one name ="StickerIssuingOffice" class="IssuingOffice" column ="StickerIssuingOfficeId" /> 

模式爲StickerInvoice

CREATE TABLE StickerInvoice(
    Id int NOT NULL, 
RefNo nvarchar(50) NOT NULL, 
VehicleId int NOT NULL, 
DateIssued datetime NOT NULL, 
Period datetime NOT NULL, 
Amount decimal(18, 0) NOT NULL, 
DueDate datetime NOT NULL, 
Penalty decimal(18, 0) NOT NULL, 
InvoiceNo nvarchar(50) NULL, 
ReceiptNo nvarchar(50) NULL, 
DatePaid datetime NULL, 
StickerNo nvarchar(50) NULL, 
StickerIssuedById int NULL, 
StickerIssuingOfficeId int NULL, 
Version int NOT NULL, 
    CONSTRAINT PK_StickerInvoice PRIMARY KEY (Id ASC) 
) 

我嘗試了數據類型不匹配或重複的特性,但沒有發現。

任何援助將不勝感激。

回答

7

如果更仔細地看看你的例外,你會看到這樣的錯誤:

NHibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Model.IssuingOffice, Entity: Model.IssuingOffice

基本上,看起來就像你正在試圖挽救IssuingOffice包含一個或多個未保存StickerInvoice。您需要將cascading option設置爲IssuingOffice包映射。

<bag name="IssuedStickers" table="StickerInvoice" 
    generic="true" inverse="true" cascade="save-update"> 
    <key column="StickerIssuingOfficeId" /> 
    <one-to-many class="StickerInvoice"/> 
</bag> 
+0

IssuingOffice已經存在於數據庫中,因此不需要保存或更新。我只想在不影響IssuingOffice的情況下更新StickerInvoice上的現有數據。在這些情況下,我真的需要級聯嗎? – kagundajm 2012-07-14 09:11:16

+0

然後,您可能正在使用StickerInvoice的臨時實例,可能是在另一個會話中加載的。 – 2012-07-14 09:29:37

+0

調查你的最新評論,我意識到IssuingOffice的版本在數據初始化時最初設置爲0。將值更改爲1解決了問題。感謝您的幫助 – kagundajm 2012-07-14 11:01:53

3

我通過設置相關的多對一部分的只讀標誌解決了這個異常:

References(x => x.Parent).ReadOnly(); 
+0

我不知道爲什麼樣本沒有提到這個簡單的修復。我花了大約2-3天的時間搜索這個簡單的修復程序。 – shankbond 2017-05-16 14:08:39