我拥有域类 - 用户,角色,组,组角色
用户域
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/>组域
私人长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>这个错误是什么?为什么我得到这个错误?我应该怎么做才能纠正这个错误?任何方案?任何人都可以解释这个错误是什么?
在此先感谢
解决方案你的错误是告诉你的表USER在一个名为GROUP_ROLE_ID的列上包含一个外键,但是你引用的表GROUP_ROLE定义了它的主键和两个列ROLE_ID和GROUP_ID,这对于关系表是非常常见的。 p>
在我看来,映射GroupRole的唯一原因是因为您在User实体中需要它。那么,如果你的域模型真的是正确的,那么你需要考虑如何同步你的用户的FK和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:
Best regards.
更多推荐
外键必须与引用主键hibernate具有相同的列数
发布评论