无法使用备用键的属性引用执行一对多映射



我们使用属性引用的一对多映射时遇到了问题。我们得到了一个 ClassCastException,因为 property-ref 列的类型与 PK 的类型不同。这似乎是谷歌搜索的已知问题。有关解决方法或执行此映射的更好方法的任何见解将不胜感激。

背景:

  • 3表帐户,用户,用户帐户;用户和帐户之间的一对多关系
  • 联接表用户帐户具有"用户"的完整主键和"帐户"的部分主键。
  • 用户到用户帐户的一对多映射,按预期工作
  • 属性引用属性,用于对用户帐户和帐户之间的关系进行建模。
  • 发现以下 JIRA 错误,表明其他人在使用属性引用时遇到了相同的问题:http://opensource.atlassian.com/projects/hibernate/browse/HHH-2052

我正在使用以下映射文件。
帐户

<class name="com.model.Account" table="Account">
    <composite-id class="com.model.VersionId"
        mapped="false" name="id">
        <key-property column="entity_id" name="entityId" type="java.lang.Long" />
        <key-property column="entity_vers" name="version" type="java.lang.Long" />
    </composite-id>
    <property column="entity_id" name="entityId"
        type="java.lang.Long" insert="false" update="false" />
    <set name="userAccounts" cascade="none">
        <key column="entity_id" property-ref="entityId"/>
        <many-to-many class="com.model.UserAccount" column="acct_entity_id" property-ref="accountId"/>
    </set>
</class>

用户

<class name="com.model.User" table="User">
    <composite-id class="com.model.VersionId"
        mapped="false" name="id">
        <key-property column="entity_id" name="entityId" type="java.lang.Long" />
        <key-property column="entity_vers" name="version" type="java.lang.Long" />
    </composite-id>
    <property column="user_name" length="255" name="name"
        type="java.lang.String" />
    <set name="userAccounts" table="UserAccount">
        <key>
            <column name="entity_id" />
            <column name="entity_vers" />
        </key>
        <one-to-many class="com.model.UserAccount" />
    </set>
</class>

用户帐户

<class name="com.model.UserAccount"
    table="UserAccount">
      <composite-id class="com.model.UserAccountId"
        mapped="false" name="id">
        <key-property column="entity_id" name="userId" type="java.lang.Long" />
        <key-property column="acct_entity_id" name="accountId"
            type="java.lang.Long" />
        <key-property column="entity_vers" name="userVersion"
            type="java.lang.Long" />
    </composite-id>
    <property column="acct_entity_id" name="accountId"
            type="java.lang.Long" insert="false" update="false"></property>
       <many-to-one name="user"
        class="com.model.User" insert="false"
        update="false">
        <column name="entity_id" />
        <column name="entity_vers" />
    </many-to-one>
    <set name="accounts" cascade="none">
        <key column="acct_entity_id" property-ref="acountId"/>
        <many-to-many class="com.model.Account" column="entity_id" property-ref="entityId"/>
    </set>
</class>

谢谢

对于任何感兴趣的人,我能够通过使用单向一对多映射来解决这个问题,该映射与从用户到帐户的连接表具有以下映射

<set name="accounts" table="UserAccounts">
        <key>
            <column name="entity_id" />
            <column name="entity_vers" />
        </key>
        <many-to-many column="acct_entity_id" property-ref="entityId" unique="true" class="com.model.Account" />
    </set>

我已经能够成功地测试这一点,以防其他人将来在使用遗留数据库时遇到这样的问题。

一些评论:使用 unique="true" 允许您使用多对多映射,而您必须使用一对多映射。这对我来说很重要,因为我需要能够在映射中使用属性引用,休眠 DTD 不允许在一对多中使用属性引用。

参考:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/associations.html#assoc-unidirectional-join-12m

最新更新