java 外键的列数必须与参考主键休眠的列数相同 - 多对多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14579449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:45:03  来源:igfitidea点击:

foreign key must have same number of columns as the reference primary key hibernate - many to many

javaspringhibernatehibernate-mapping

提问by Sri

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

用户名.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

我有 user_grouprole 表作为一个用户和一组 grouproles 的连接表我有一个 user_role 表作为一个用户和一组角色的连接表

Group Domain

组域

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

Group.hbm.xml

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

组角色.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])

当我尝试使用主类进行测试时,出现映射错误,例如外键 (FK5110401A8398947:user_grouprole [group_role_id]) 之类的休眠映射错误, 必须与引用的主键 (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

提前致谢

回答by Thiago Uriel Garcia

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.

您的错误表明您的表 USER 在名为 GROUP_ROLE_ID 的列上包含一个外键,但您引用的表 GROUP_ROLE 将其主键定义为具有两列 ROLE_ID 和 GROUP_ID,顺便说一下,这对于关系表非常常见。

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:

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

  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.
  1. 为您的复合键创建一个 PK 对象或在您的映射中正确定义您的 ID;
  2. 更改 GroupRole 表中的 PK 定义。

Best regards.

最好的祝福。