简体   繁体   English

使用NHibernate保存手动创建的对象

[英]Saving manually created objects using NHibernate

I am trying to use NHibernate to save an object that was completely manually created. 我正在尝试使用NHibernate保存完全手动创建的对象。 My mappings are in place and I currently have no data in the database. 我的映射到位,并且数据库中目前没有数据。 Everytime I call Save() or SaveOrUpdate(), NHibernate does a select statement for what I am trying to save. 每次我调用Save()或SaveOrUpdate()时,NHibernate都会为我要保存的内容执行一条select语句。 Then it gives me the exception: "a different object with the same identifier value was already associated with the session". 然后它给了我一个例外:“具有相同标识符值的另一个对象已与该会话关联”。 Does anyone know how I can tell NHibernate to save my manually instantiated object without thinking that a different object has already been loaded? 有谁知道我该如何告诉NHibernate保存手动实例化的对象,而不用考虑已经加载了另一个对象?

Additional Information: 附加信息:

I have a primary mapping with a one-to-many collection. 我有一个带有一对多集合的主映射。 The exception is telling me that "a different object with the same identifier has been loaded", on the collection, not the parent object. 例外是告诉我“已加载具有相同标识符的另一个对象”,而不是父对象。 I don't know if this provides any useful information. 我不知道这是否提供任何有用的信息。 The mappings are as follows: 映射如下:

<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>

and

<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 is telling me that "a different object with the same identifier value was already associated with the session" for the ProductCode key of the second mapping. NHibernate告诉我第二个映射的ProductCode键“具有相同标识符值的不同对象已与该会话关联”。 Any insight would greatly be appreciated. 任何见识将不胜感激。 Thank you. 谢谢。

I believe you will need to add a version field to your composite key class and mapping; 我相信您将需要在复合键类和映射中添加一个version字段; see this article for further details. 有关更多详细信息,请参见本文

Have you tried 你有没有尝试过

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

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM