使用NHibernate保存手动创建的对象 [英] Saving manually created objects using NHibernate

查看:123
本文介绍了使用NHibernate保存手动创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用NHibernate来保存完全手动创建的对象。我的映射已经就绪,我目前在数据库中没有数据。每次我调用Save()或SaveOrUpdate()时,NHibernate都会为我想要保存的内容做一个select语句。然后它给了我一个例外:具有相同标识符值的不同对象已经与会话相关联。有谁知道我怎么可以告诉NHibernate保存我的手动实例化的对象,而不会想到一个不同的对象已经被加载?

I am trying to use NHibernate to save an object that was completely manually created. 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. 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?

其他信息:

我有一对多集合的主映射。例外情况是告诉我集合中已加载了具有相同标识符的不同对象,而不是父对象。我不知道这是否提供任何有用的信息。映射如下:

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>

<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告诉我具有相同标识符值的不同对象已经与会话相关联为第二个映射的ProductCode键。任何有识之士将非常感激。谢谢。

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. 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; see this article for further details.

这篇关于使用NHibernate保存手动创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆