gpt4 book ai didi

java - 如何用JPA管理多个实体?

转载 作者:行者123 更新时间:2023-12-02 01:39:10 24 4
gpt4 key购买 nike

上下文

我正在尝试创建一个简单的 API 来学习 Spring Boot 和 JPA。信息存储到H2数据库中。

我有 2 个实体:missionuser

多个用户被分配到一个任务。用户可以被分配到不同的任务。

所以,我的类 Mission 有一个属性 private ArrayList<User> users;

目标

我的目标是创建一些请求:

  • (GET 请求)IP/missions/123456/users:获取分配给任务 123456 的所有用户
  • (PUT 请求)IP/missions/456789:为用户分配任务 456789

因此,将两个实体绑定(bind)在一起。

有问题

但我不知道如何存储/绑定(bind)与 missionuser 相关的信息。好的方法是使用 Assignements(id_user, id_mission) 方案创建一个“关联表”?

感谢您的帮助!

编辑 1:代码

实体任务

@Entity
public class Mission{

@Id
private String id;
private String name;
private Date start;
private Date end;
private ArrayList<User> users;
private int status;

public Mission() {
}

public Mission(String name, Date start, Date end) {
this.name = name;
this.start = start;
this.end = end;
this.status = 0;
}

// getters and setters
}

实体用户

@Entity
public class User {

@Id
private String id;
private String name;

public User() {
}

public User(String name) {
this.name = name;
}

// getters and setters
}

SQL

我使用 SQL 脚本。目前,脚本如下所示:

INSERT INTO mission (id, name, start, end, status) VALUES ('de7d9052-4961-4b4f-938a-3cd12cbe1f82', 'mission 1', '2019-02-11', '2019-02-13', 0)
INSERT INTO mission (id, name, start, end, status) VALUES ('425e7701-02c6-4de3-9333-a2459eece1c8', 'mission 2', '2019-02-10', '2019-02-15', 0)

编辑 2:新代码(使用 @ManyToMany)

实体

@Entity
public class Mission {

@Id
private String missionid;
private String namemission;
private Date start;
private Date end;
@ManyToMany
@JoinTable(name = "mission_user",
joinColumns = @JoinColumn(name = "missionid"),
inverseJoinColumns = @JoinColumn(name = "userid")
)
private Set<User> users = new HashSet<>();
private int status;

public Mission() {}

public Mission(String name, Date start, Date end) {
this.namemission = name;
this.start = start;
this.end = end;
this.status = 0;
}
}

@Entity
public class User {

@Id
private String iduser;
private String nomuser;
@ManyToMany(mappedBy = "users")
private Set<Mission> missions = new HashSet<>();

public User() {}

public User(String nom) {
this.nomuser = nom;
}

}

存储库

@RepositoryRestResource(collectionResourceRel = "mission")
public interface MissionResource extends JpaRepository<Mission, String> {
...
}

@RepositoryRestResource(collectionResourceRel = "user")
public interface UserResource extends JpaRepository<User, String> {
...
}

休息 Controller

@RestController
@RequestMapping(value = "/missions", produces = MediaType.APPLICATION_JSON_VALUE)
@ExposesResourceFor(Mission.class)
public class MissionRepresentation {
private final MissionResource missionResource;
private final UserResource userResource;

public MissionRepresentation(MissionResource missionResource, UserResource userResource) {
this.missionResource = missionResource;
this.userResource = userResource;
}

// mapping
}

最佳答案

正如您所说,最好是实现一个连接表。那会是这样的:

create table mission_user (
mission_id int,
user_id int,
primary key (mission_id, user_id)
)

然后 @ManyToMany 使用此表作为映射或实体,例如 MissionUser 并映射 UserMission作为 @OneToMany

关于java - 如何用JPA管理多个实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54713739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com