我制作了一个包含一些用户的Spring Boot应用程序。这些用户可以属于0,一个或多个组(为了更好的可视化,我省略了一些代码行):
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "group_user",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "group_id")}
)
private List<Group> groups = new ArrayList<>();
public User(String name, List<Group> groups) {
this.name = name;
this.groups = groups;
}
}
一个组可以包含0个,一个或多个用户。
@Entity
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany(mappedBy = "groups")
private List<User> users = new ArrayList<>();
public Group(String name, List<User> users) {
this.name = name;
this.users = users;
}
}
我正在使用MySQL,我创建了3个表:
CREATE TABLE user (
id integer NOT NULL AUTO_INCREMENT,
name varchar(255),
PRIMARY KEY (id)
);
CREATE TABLE group (
id integer NOT NULL AUTO_INCREMENT,
name varchar(255),
PRIMARY KEY (id)
);
CREATE TABLE group_user (
user_id int NOT NULL,
group_id int NOT NULL,
PRIMARY KEY (user_id, group_id),
KEY group_id (group_id),
CONSTRAINT group_user_ibfk_1
FOREIGN KEY (user_id) REFERENCES user (id),
CONSTRAINT group_user_ibfk_2
FOREIGN KEY (group_id) REFERENCES group (id)
);
我设法通过创建这个新用户,将一个用户链接到一个组,将一个组传递给它的构造函数,并调用我的userDao.save()方法:
userDao.save(new User(name, groups));
现在我想编辑我创建的用户并让他属于另一个组。 如何在不创建新用户的情况下执行此操作?
例如,我有这个用户,他没有任何组:
INSERT INTO user VALUES(1, 'Jordan');
这些团体:
INSERT INTO group VALUES(1, 'Group 1');
INSERT INTO group VALUES(2, 'Group 2');
现在,我如何(在Java中)将我的用户链接到第1组和第2组?
答案 0 :(得分:2)
您必须使用您选择的 id 从数据库中选择组实体。您可以使用 findById 方法为该实体使用JPA 界面,也可以在界面中创建自己的自定义方法。
如果您拥有组的实体,则将其添加到用户实体字段组 。之后,您保存用户实体。
同一过程以双向关系进入另一方组。
推荐阅读:
答案 1 :(得分:0)
mysql> CREATE TABLE users (
-> id integer NOT NULL AUTO_INCREMENT,
-> name varchar(255),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.24 sec)
mysql> CREATE TABLE groups (
-> id integer NOT NULL AUTO_INCREMENT,
-> name varchar(255),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.17 sec)
mysql> CREATE TABLE group_users (
-> user_id int NOT NULL,
-> group_id int NOT NULL,
-> PRIMARY KEY (user_id, group_id),
-> CONSTRAINT group_user_ibfk_1
-> FOREIGN KEY (user_id) REFERENCES users(id),
-> CONSTRAINT group_user_ibfk_2
-> FOREIGN KEY (group_id) REFERENCES groups(id)
-> );
Query OK, 0 rows affected (0.23 sec)
mysql> INSERT INTO user VALUES(1, 'Jordan');
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO users VALUES(1, 'Jordan');
Query OK, 1 row affected (0.08 sec)
mysql>
mysql> INSERT INTO groups VALUES(1, 'Group 1');
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO groups VALUES(2, 'Group 2');
Query OK, 1 row affected (0.02 sec)
mysql> insert into group_users values(1,1);
Query OK, 1 row affected (0.05 sec)
mysql> insert into group_users values(1,2);
Query OK, 1 row affected (0.08 sec)
mysql> select * from users;
+----+--------+
| id | name |
+----+--------+
| 1 | Jordan |
+----+--------+
1 row in set (0.00 sec)
mysql> select * from groups;
+----+---------+
| id | name |
+----+---------+
| 1 | Group 1 |
| 2 | Group 2 |
+----+---------+
2 rows in set (0.00 sec)
mysql> select * from group_users;
+---------+----------+
| user_id | group_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
+---------+----------+
2 rows in set (0.00 sec)
mysql> select u.id user_id, u.name user_name,g.id group_id, g.name group_name from users u, groups g, group_users gu where u.id = gu.user_id and g.id = gu.group_id;
+---------+-----------+----------+------------+
| user_id | user_name | group_id | group_name |
+---------+-----------+----------+------------+
| 1 | Jordan | 1 | Group 1 |
| 1 | Jordan | 2 | Group 2 |
+---------+-----------+----------+------------+
2 rows in set (0.05 sec)
希望,你清除了设计,以及它如何在你的情况下工作。