我已經閱讀了很多問題和答案,但是我找不到解決方案。當我從NHibernate 1.2.1遷移到2.1.2時出現問題。 我有這個常見錯誤:NHibernate 2.1.2未保存的瞬態實例
對象引用一個未保存的瞬態實例 - 在沖洗前保存瞬態實例。類型:Mikro.FareOn.Modules.Cards.Interface.BEC.CRResidenceRegion,實體:Mikro.FareOn.Modules.Cards.Interface.BEC.CRResidenceRegion
NHibernate configuration: <nhibernate> <add key="hibernate.show_sql" value="true" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" /> <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" /> <add key="driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="connection.connection_string" value="..." /> </nhibernate>
主類CRResidence的映射:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <class name="Mikro.FareOn.Modules.Cards.Interface.BEC.CRResidence, Mikro.FareOn.Modules.Cards.Interface" table="CR_Residence"> <id name="Id" column="id" type="Int64" access="nosetter.camelcase" unsaved-value="0"> <generator class="identity" /> </id> <version name="VersionId" column="version_id" type="Int64" /> <property name="IsDeleted" column="deleted" type ="Boolean" not-null="true" /> <property name="Name" column="name" type="String" not-null="true" /> <many-to-one name="Region" column="region_id" cascade="none" class="Mikro.FareOn.Modules.Cards.Interface.BEC.CRResidenceRegion, Mikro.FareOn.Modules.Cards.Interface"/> <property name="Postcode" column="postcode" type="String" not-null="true" /> <many-to-one name="Country" column="residence_country_id" cascade="none" class="Mikro.FareOn.TransportOperator.Interface.BEC.TOResidenceCountry, Mikro.FareOn.TransportOperator.Interface"/> </class> </hibernate-mapping>
而在關係類的一個多到一:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <class name="Mikro.FareOn.Modules.Cards.Interface.BEC.CRResidenceRegion, Mikro.FareOn.Modules.Cards.Interface" table="CR_ResidenceRegion" > <id name="Id" column="id" type="Int64" access="nosetter.camelcase" unsaved-value="0"> <generator class="identity" /> </id> <version name="VersionId" column="version_id" type="Int64" /> <property name="Name" column="name" type="String" not-null="true" /> <property name="IsDeleted" column="deleted" type ="Boolean" not-null="true" /> </class> </hibernate-mapping>
類CRResidenceRegion的執行情況:
[Serializable] public class CRResidence : BussinesEntityComponentBase //this is class, which implements Equals and define Id, VersionId, IsDeleted { string name; string postcode; CRResidenceRegion region; TOResidenceCountry country; #region Getters/Setters /// /// Nazev mista /// public string Name { get { return this.name; } set { this.name = value; } } /// /// Region /// public CRResidenceRegion Region { get { return this.region; } set { this.region = value; } } /// /// Stát /// public TOResidenceCountry Country { get { return this.country; } set { this.country = value; } } /// /// PSC /// public string Postcode { get { return this.postcode; } set { this.postcode = value; } } public string PostcodeAndName { get { return String.Format("{0} - {1}", Postcode, Name); } } public string NameAndPostcode { get { return String.Format("{1} - {0}", Postcode, Name); } } #endregion /// /// Konstruktor objektu rezidence /// public CRResidence() : base() { } }
我試過所有類型的級聯,但任何級聯都無法正常工作。但對於我的場合,我更喜歡cascade =「none」。 我有客戶端,我在一個NHibernate會話中通過調用服務器端的方法加載對象CRResidenceRegion。加載NHibernate會話後關閉。客戶端上的對象是正確的,數據庫中的ID是否正確等等。然後給CRResidence對象分配CRResidenceRegion對象。 服務器端則稱爲方法:
using (PersistenceSession session = Registrator.OpenSession()) { ITransaction trx = session.BeginTransaction(); try { session.Update(residence); //residence.Region = (CRResidenceRegion)session.Get(typeof(CRResidenceRegion), residence.Region.Id); //residence.Country = (TOResidenceCountry)session.Get(typeof(TOResidenceCountry), residence.Country.Id); trx.Commit(); log.Debug("CRResidence updated."); return residence; } catch (Exception ex) ............ }
我有很多這樣的對象,這個問題時,我遷移到NHibernate的2.1.2 appered。在某些情況下,在定義級聯樣式(「無」和「所有」,在我需要的地方)工作正常。可能是NHibernate配置文件中的問題。
問題的上一個定義: 當我嘗試插入或更新CRResidence的實例時,發生關於未保存的瞬態錯誤的錯誤。 CRResidenceRegion的實例已完全初始化,包括Id,因此沒有問題。我在Id上實現了Equal方法。我試圖清除緩存,但它沒有幫助。當我將級聯更改爲保存更新時,它可以工作,但CRResidenceRegion的記錄會重複。一種解決方案是我在保存CRResidence之前從同一會話中的數據庫中讀取CRResidenceRegion(在下面的代碼中註釋),但這是不可接受的。
似乎NHibernate已經失去了對CRResidenceRegion的引用,並認爲它是新創建的,但事實並非如此。
感謝您的回答,但同樣的錯誤再次發生。 – Cvachtoun 2011-06-13 12:31:28
你可以更新問題,然後如何檢索對象並修改它? – Meligy 2011-06-13 12:54:16
另外,你是否嘗試更改映射中的'cascade'屬性? – Meligy 2011-06-13 12:54:59