我想使用NHibernate保存完全手動創建的對象。我的映射已經就緒,我目前在數據庫中沒有數據。每次我調用Save()或SaveOrUpdate()時,NHibernate都會爲我想要保存的內容做一個select語句。然後它給了我一個例外:「具有相同標識符值的不同對象已經與會話相關聯」。有誰知道我怎麼可以告訴NHibernate保存我的手動實例化的對象,而不會想到一個不同的對象已經被加載?使用NHibernate保存手動創建的對象
附加信息:
我有一對多集合的主映射。例外情況是告訴我「集合中已加載了具有相同標識符的不同對象」,而不是父對象。我不知道這是否提供任何有用的信息。的映射如下:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Program.Application.Models" assembly="Company.Application.Models">
<class name="ProductVersion" table="ClientVersion" lazy="false">
<composite-id>
<key-property name="PracticeName">
<column name="practiceName" not-null="true" />
</key-property>
<key-property name="Address">
<column name="address" not-null="true" />
</key-property>
<key-property name="City">
<column name="city" not-null="true" />
</key-property>
<key-property name="State">
<column name="state" not-null="true" />
</key-property>
<key-property name="Zip">
<column name="zip" not-null="true" />
</key-property>
</composite-id>
<property name="LegalName" column="legalName" />
<property name="Version" column="version" />
<bag name="ProductsLicensesDetail" inverse="true" lazy="false" >
<key>
<column name="practiceName" />
<column name="address" />
<column name="city" />
<column name="state" />
<column name="zip" />
</key>
<one-to-many class="ProductLicenseDetail" />
</bag>
</class>
</hibernate-mapping>
和
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Program.Application.Models" assembly="Program.Application.Models">
<class name="ProductLicenseDetail" table="ClientProductLicense">
<id name="ProductCode" column="productCode">
<generator class="assigned" />
</id>
<property name="TotalEnterpriseLicenses" column="totalEnterpriseLicenses" />
<property name="EnterpriseLicensesUsed" column="enterpriseLicensesUsed" />
<property name="TotalPracticeLicenses" column="totalPracticeLicenses" />
<property name="PracticeLicensesUsed" column="practiceLicensesUsed" />
<property name="TotalProviderLicenses" column="totalProviderLicenses" />
<property name="ProviderLicensesUsed" column="providerLicensesUsed" />
<property name="TotalUserLicenses" column="totalUserLicenses" />
<property name="UserLicensesUsed" column="userLicensesUsed" />
<property name="LicenseKey" column="licenseKey" />
<property name="LicenseActivationDate" column="licenseActivationDate" />
<property name="LicenseExpirationDate" column="licenseExpirationDate" />
<many-to-one name="ProductVersion" class="ProductVersion" cascade="none">
<column name="practiceName" />
<column name="address" />
<column name="city" />
<column name="state" />
<column name="zip" />
</many-to-one>
</class>
</hibernate-mapping>
NHibernate的是告訴我,「具有相同的標識符值不同的物體已經與所述會話相關聯」爲第二映射的產品代碼鍵。任何有識之士將非常感激。謝謝。
我相信你也可以設置類屬性optimistic-lock = false作爲替代。 – Scozzard 2010-08-13 02:06:58
你對版本領域是正確的。 NHibernate In Action提供了一個完整的方法。我實際上決定根據我認爲是複合的字段的散列來創建一個指定的ID。它似乎工作。除非使用架構無法更改的遺留數據庫,否則無需使用複合標識。 – SideFX 2010-08-13 19:49:41