外键必须与引用主键hibernate具有相同的列数 - 多对多 [英] foreign key must have same number of columns as the reference primary key hibernate - many to many

查看:145
本文介绍了外键必须与引用主键hibernate具有相同的列数 - 多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有域类 - 用户,角色,组,组角色

用户域

  private long id,
private String userName,
private String password,
Set< Role> roles = new HashSet< Role>();

User.hbm.xml

 < hibernate-mapping package =uk.co.jmr.sdp.domain> 
< class name =Usertable =user>
< id name =idunsaved-value = - 1>
< generator class =native/>
< / id>
< property name =userNamecolumn =user_name/>
< property name =passwordcolumn =password/>
< property name =emailIdcolumn =email_id/>

< set name =rolestable =user_rolelazy =falsecascade =all>
< key column =user_id/>
< / set>

< key column =user_id/>
< / set>

< / class>
< / hibernate-mapping>

我将user_grouprole表作为用户和组的集合的联接表
我有user_role表作为用户和角色集合的连接表

组域

 私人长ID; 
private String groupName;
私人设置<角色> roles = new HashSet< Role>();

Group.hbm.xml

 < hibernate-mapping package =uk.co.jmr.sdp.domain> 
< class name =Grouptable =group>
< id name =idunsaved-value = - 1>
< generator class =native/>
< / id>
< property name =groupNamecolumn =group_name>< / property>

< key column =group_id/>
< / set>




GroupRole

 私人长ID; 
私人角色角色;
私人小组;

GroupRole.hbm.xml

 < class name =GroupRoletable =group_role> 
< id name =idunsaved-value = - 1>
< generator class =native/>
< / id>
column =role_idlazy =falsenot-null = true/>

column =group_idlazy =falsenot -null =true/>

< / class>
< / hibernate-mapping>

当我尝试使用主类进行测试时,出现像hibernate映射错误那样的映射错误
外键(FK5110401A8398947:user_grouprole [group_role_id]))的列数必须与引用的主键相同(group_role [group_id,role_id])



这个错误是什么?为什么我得到这个错误?我应该怎么做才能纠正这个错误?任何方案?任何人都可以解释这个错误是什么?



在此先感谢

解决方案

你的错误是告诉你的表USER在一个名为GROUP_ROLE_ID的列上包含一个外键,但是你引用的表GROUP_ROLE定义了它的主键和两个列ROLE_ID和GROUP_ID,这对于关系表是非常常见的。 p>

在我看来,映射GroupRole的唯一原因是因为您在User实体中需要它。那么,如果你的域模型真的是正确的,那么你需要考虑如何同步你的用户的FK和GroupRole的PK。为此,您可以:


  1. 为您的组合键创建一个PK对象或在映射中正确定义您的ID;

  2. 在GroupRole表中更改您的PK定义。

祝好。

I have domain Classes - User, Role, Group, Group Role

User Domain

private long id, 
private String userName, 
private String password, 
Set<Role> roles = new HashSet<Role>();

User.hbm.xml

<hibernate-mapping package="uk.co.jmr.sdp.domain">
<class name="User" table="user">
        <id name="id" unsaved-value="-1">
            <generator class="native"/>
        </id>
        <property name="userName" column="user_name"/>
        <property name="password" column="password"/>
        <property name="emailId" column="email_id"/>

        <set name="roles" table="user_role" lazy="false" cascade="all">
            <key column="user_id"/>
            <many-to-many column="role_id" class="Role" fetch="join"/>
        </set>

        <set name="groupRoles" table="user_grouprole" lazy="false" cascade="all">
            <key column="user_id"/>
            <many-to-many column="group_role_id" class="GroupRole" fetch="join"/>
        </set> 

</class>
</hibernate-mapping>

I have user_grouprole table as a join table for an User and Set of grouproles I have user_role table as a join table for an user and set of roles

Group Domain

private long id;
private String groupName;
private Set<Role> roles = new HashSet<Role>();

Group.hbm.xml

<hibernate-mapping package="uk.co.jmr.sdp.domain">
 <class name="Group" table="group">
 <id name="id" unsaved-value="-1">
        <generator class="native"/>
 </id>
 <property name="groupName" column="group_name"></property>

 <set name="roles" table="group_role" lazy="false" cascade="all">
        <key column="group_id"/>
        <many-to-many column="role_id" class="Role" fetch="join"/>
    </set>

GroupRole

private long id;
private Role role;
private Group group;

GroupRole.hbm.xml

<class name="GroupRole" table="group_role">
    <id name="id" unsaved-value="-1">
            <generator class="native"/>
    </id>
    <many-to-one name="role" class="uk.co.jmr.sdp.domain.Role"
            column="role_id" lazy="false" not-null="true" />

    <many-to-one name="group" class="uk.co.jmr.sdp.domain.Group"
            column="group_id" lazy="false" not-null="true" /> 

 </class>
</hibernate-mapping>

When I try to test with a main class, I get a mapping error like a hibernate mapping error like Foreign key (FK5110401A8398947:user_grouprole [group_role_id])) must have same number of columns as the referenced primary key (group_role [group_id,role_id])

What is this error? Why I get this error? What should I do to rectify this error??? Any Solutions ? Can anyone explain what is this error?

Thanks in Advance

解决方案

Your error is telling that your table USER contains a foreign key on a column named GROUP_ROLE_ID, but your referenced table, GROUP_ROLE, defines it's primary key with two columns ROLE_ID and GROUP_ID, which by the way is very common for relationship tables.

Seems to me that the only reason that you are mapping GroupRole is because you need it in your User entity. Well, if your domain model is really correct, then you need to think about how you want to synchronize your User's FK and GroupRole's PK. For that you might:

  1. Create a PK object for your composite key or define your ID's correctly in your mappings;
  2. Change your PK definition in GroupRole table.

Best regards.

这篇关于外键必须与引用主键hibernate具有相同的列数 - 多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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