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

安稳与你 提交于 2019-12-11 04:04:13

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14579449/foreign-key-must-have-same-number-of-columns-as-the-reference-primary-key-hibern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!