0

我有一个 User 对象,它有一个与之关联的 UserProfile 对象,每个对象都有自己的数据库表。我已经使用双向映射将休眠映射设置为级联操作,这样我就可以在 User 对象上调用 Save ,它将自动创建用户配置文件对象,在单个操作中插入键列中生成的 UserId . 这工作正常。Hibernate映射如下

<class name="User" table="[User]">
    <id name="UserId" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="FirstName" />
    <property name="LastName" />
    <property name="EmailAddress" />
    <property name="DateOfBirth" />
    <property name="Gender" />
    <property name="PostcodePartOne" />
    <property name="PostcodePartTwo" />
    <many-to-one name="UserLocation" column="UserLocationId" />
    <property name="DateJoined" />
    <property name="LastLoggedOn" />
    <property name="Status"/>
    <property name="StatusNotes" />
    <one-to-one name="Profile" class="UserProfile" cascade="all" />
  </class>

<class name="UserProfile" table="UserProfile">
    <id name="UserProfileId" column="UserId">
      <generator class="foreign">
        <param name="property">User</param>
      </generator>
    </id>
    <one-to-one name="User" class="User" constrained="true" />
    <property name="Height" />
    <many-to-one name="BodyType" column="BodyTypeId" />
    <many-to-one name="Ethnicity" column="EthnicityId" />
    <many-to-one name="MaritalStatus" column="MaritalStatusId"/>
    <property name="NumberOfChildren" />
    <many-to-one name="WantsChildren" column="WantsChildrenId" />
    <many-to-one name="Religion" column="ReligionId" />
    <many-to-one name="Drinking" column="DrinkingId" />
    <many-to-one name="Smoking" column="SmokingId" />
    <many-to-one name="Diet" column="DietId" />
    <many-to-one name="Education" column="EducationId" />
    <many-to-one name="Socialising" column="SocialisingId" />
    <many-to-one name="Travelling" column="TravellingId" />
    <many-to-one name="Romance" column="RomanceId" />
    <many-to-one name="SexualOpenness" column="SexualOpennessId" />
    <many-to-one name="SexualFrequency" column="SexualFrequencyId" />
    <many-to-one name="SharedInterests" column="SharedInterestsId" />
    <many-to-one name="TimeWithPartner" column="TimeWithPartnerId" />
    <many-to-one name="Ambition" column="AmbitionId" />
    <many-to-one name="Emigrating" column="EmigratingId" />
    <many-to-one name="FamilyInvolvement" column="FamilyInvolvementId" />
    <property name="LastUpdated" />
    <one-to-one name="ProfileText" class="UserProfileText" cascade="all" />
  </class>

当我尝试进行分离更新时不起作用。我创建了一个新的 User 和 UserProfile 对象,将它们与彼此的引用链接起来,就像我在保存时所做的那样,然后将 User.UserId 分配给我要更新的已保存用户 ID。然后我调用 session.merge(user) 但得到以下错误

    Message "Error updating item of type 'User':a different object with the same identifier value was already associated with the session: 153, of entity: UserProfile" string

这是否告诉我它很乐意合并用户对象但由于某种原因无法合并用户配置文件?

我究竟做错了什么?

4

1 回答 1

0

认为我发现了问题。我正在将 UserProfiles“用户”属性引用设置为父用户对象,但我没有将它的“用户 ID”属性设置为 PK,所以它认为它属于父用户,但它的用户 ID 仍然为零标识符。在 Profile 对象上设置 UserId 以及 User 对象有效

于 2012-11-28T13:45:06.620 回答