开发者

Unable to do one-to-many mapping using property-ref for alternate keys

We are having a problem with a one-to-many mapping using property-ref. We are getting a ClassCastException because the type of the property-ref column is not the same as the type of the PK. It looks to be a known issue from googling. Any insight on a workaround or a better way to do this mapping would be appreciated.

Background:

  • 3 tables Account, User, UserAccount; One-to-many relationship between User and Account
  • Join table UserAccount has the full primary key of User and part of the primary key of Account.
  • One-to-many mapping for User to UserAccount which works as expected
  • Property-ref attribute to model the relationship between UserAccount and Account.
  • Found the following JIRA bug, indicating other people are experiencing the same issue with property-ref: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2052

I am using the following mapping files.

Account

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

User

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

UserAccount

<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百科="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>

Thanks


For any-one who is interested I was able to solve this by using a unidirectional one-to-many mapping with join table from the User to the Account with the below mapping on the User for accounts

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

I have been able to test this out successfully, in case anyone else has such an issue in the future working with a legacy database.

Some comments: Using unique="true" allows you to use a many-to-many mapping where you would have had to use a one-to-many. This was important for me as I needed to be able to use property-ref in the mapping with is not allowed in one-to-many by hibernate DTD.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜