外键必须与引用主键hibernate具有相同的列数

编程入门 行业动态 更新时间:2024-10-28 16:26:56
本文介绍了外键必须与引用主键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/> 我将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>

GroupRole

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

GroupRole.hbm.xml

< class name =GroupRoletable =group_role> < id name =idunsaved-value = - 1> < generator class =native/> < / id> 当我尝试使用主类进行测试时,出现像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。为此,您可以:

  • 为您的组合键创建一个PK对象或在映射中正确定义您的ID;
  • 在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:

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

    更多推荐

    外键必须与引用主键hibernate具有相同的列数

    本文发布于:2023-06-13 07:15:55,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:主键   hibernate

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!