在对应的映射表中保存多对多关系 [英] save many-to-many relationship in corresponding mapping-table
问题描述
在我的Spring-Boot应用程序中,我想在数据库的对应映射表中保存用户角色多对多关系,但是休眠会给我一条错误消息.
in my Spring-Boot application i want to save a User-Role many-to-many relationship in a corresponding mapping-table in my database, but hibernate gives me an error message.
我的 User.java 类:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String firstname;
String lastname;
String username;
String password;
@ManyToMany(mappedBy = "users")
private Set<Role> roles;
}
我的 Role.java 类:
@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String description;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "role_users",
joinColumns =
@JoinColumn(name = "users_id", referencedColumnName = "id"),
inverseJoinColumns =
@JoinColumn(name = "roles_id", referencedColumnName = "id")
)
private Set<User> users;
}
我的运行方法:
@Override
@Transactional
public void run(String... strings) throws Exception {
//add new Roles
Role roleA = new Role("Rolle A");
Role roleB = new Role("Rolle B");
Role roleC = new Role("Rolle C");
//add new Users
User userA = new User("FirstnameA", "LastnameA", "UsernameA", "PasswordA");
User userB = new User("FirstnameB", "LastnameB", "UsernameB", "PasswordB");
//add new Lists of Roles
Set<Role> rolesA = new HashSet<Role>();
rolesA.add(roleA);
rolesA.add(roleB);
Set<Role> rolesB = new HashSet<Role>();
rolesB.add(roleA);
rolesB.add(roleC);
//add a list of roles in one user, two times
userA.setRoles(rolesA);
userB.setRoles(rolesB);
//save both users in db
userRepository.save(userA); //rolle AB
userRepository.save(userB); //rolle AC
//give each role the corresponding user
Set<User> usersA = new HashSet<User>();
usersA.add(userA);
usersA.add(userB);
roleA.setUsers(usersA);
roleRepository.save(roleA);
Set<User> usersB = new HashSet<User>();
usersB.add(userA);
roleB.setUsers(usersB);
roleRepository.save(roleB);
Set<User> usersC = new HashSet<User>();
usersC.add(userB);
roleC.setUsers(usersC);
roleRepository.save(roleC);
}
休眠在这方面给我一个错误消息:
Hibernate gives me an error message on that:
无法添加或更新子行:外键约束失败(
projekt_gra
.role_users
,CONSTRAINTfk_roleusers_user
外键(users_id
)参考users
(id
)删除级联更新级联)
Cannot add or update a child row: a foreign key constraint fails (
projekt_gra
.role_users
, CONSTRAINTfk_roleusers_user
FOREIGN KEY (users_id
) REFERENCESusers
(id
) ON DELETE CASCADE ON UPDATE CASCADE)
I have the construction from this site and it works with it, but in my code it doesn't work.
推荐答案
我总是通过将2个@ManToToMany关系重写为@OneToMany到一个新实体(将表传递给我)来解决这个问题.
I always pass this problem, by rewriting the 2 @ManyToMany relations to @OneToMany to a new entity which deliveres me the table inbetween.
一个很好的理由是,当我拥有@ManyToMany关系时,我永远无法轻易获得想要的东西,就像我得到的是Carthetic产品一样.将此表作为一个实体完全可以为我解决这个问题.
A good reason is that when I have @ManyToMany relations, I can never really easily get whatever I want, as I get carthetic products. Having this table inbetween as an entity totally removes this problem for me.
因此,总结一下:
- 您已经拥有您的 User 和您的 Role 类,这些都差不多
- 创建一个新实体: UserRole ,并具有以下属性:用户用户和角色角色
- 将 User 和 Role 中的两个@ManyToMany更改为@OneToMany的新@Entity UserRole
- 在 UserRole 中添加关系:2 @ManyToOne关系到 User 和 Role .
- You already have your User, and your Role class, those are almost OK
- Create a new Entity: UserRole, with properties: User user and Role role
- Change the two @ManyToMany in User and Role to @OneToMany's to your new @Entity UserRole
- Add the relations in UserRole: 2 @ManyToOne relations to User and Role.
您现在解决了这个问题,而且还有:可以查询关系UserRole.
You now got the problem gone, and extra: a posibility to query on the relation UserRole.
这篇关于在对应的映射表中保存多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!