Hibernate 无法使用备用键的属性ref进行一对多映射
我们在使用property-ref的一对多映射中遇到问题。我们得到一个ClassCastException,因为property-ref列的类型与PK的类型不同。这似乎是谷歌的一个已知问题。如果您对解决方案或更好的映射方法有任何见解,我们将不胜感激 背景:Hibernate 无法使用备用键的属性ref进行一对多映射,hibernate,one-to-many,Hibernate,One To Many,我们在使用property-ref的一对多映射中遇到问题。我们得到一个ClassCastException,因为property-ref列的类型与PK的类型不同。这似乎是谷歌的一个已知问题。如果您对解决方案或更好的映射方法有任何见解,我们将不胜感激 背景: 3表帐户、用户、用户帐户;用户和帐户之间的一对多关系 联接表UserAccount具有用户的完整主键和帐户的部分主键 用户到用户帐户的一对多映射按预期工作 属性ref,用于建模UserAccount和Account之间的关系 发现以下JIR
- 3表帐户、用户、用户帐户;用户和帐户之间的一对多关系
- 联接表UserAccount具有用户的完整主键和帐户的部分主键李>
- 用户到用户帐户的一对多映射按预期工作
- 属性ref,用于建模UserAccount和Account之间的关系
- 发现以下JIRA错误,表明其他人在property ref中遇到相同问题:
帐目
使用者
用户帐户
<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”允许您在必须使用一对多的情况下使用多对多映射。这对我来说很重要,因为我需要能够在映射中使用属性ref,而hibernate DTD不允许在一对多中使用属性ref
参考:
<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>