2010-08-12 65 views
0

我想使用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的是告诉我,“具有相同的标识符值不同的物体已经与所述会话相关联”为第二映射的产品代码键。任何有识之士将非常感激。谢谢。

回答

1

我相信你需要为你的组合键类和映射添加一个版本字段;进一步的细节见this article

+0

我相信你也可以设置类属性optimistic-lock = false作为替代。 – Scozzard 2010-08-13 02:06:58

+0

你对版本领域是正确的。 NHibernate In Action提供了一个完整的方法。我实际上决定根据我认为是复合的字段的散列来创建一个指定的ID。它似乎工作。除非使用架构无法更改的遗留数据库,否则无需使用复合标识。 – SideFX 2010-08-13 19:49:41

0

你试过

session.SaveOrUpdateCopy(entity); 
session.Flush();