如何使用JPA管理多个实体?

时间:2019-02-15 16:51:00

标签: java spring spring-boot jpa spring-hateoas

上下文

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

我有2个实体:missionuser

几个用户被分配了一个任务。并且可以将用户分配给不同的任务。

因此,我的班级Mission具有属性private ArrayList<User> users;

客观

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

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

因此,将两个实体捆绑在一起。

问题

但是我不知道如何存储/绑定与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> {
    ...
}

其他控制器

@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
}

2 个答案:

答案 0 :(得分:1)

正如您所说的,最好是实现一个联接表。就像这样:

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

然后,@ManyToMany使用此表作为映射或诸如MissionUser之类的实体,并将UserMission都以@OneToMany映射到它。

答案 1 :(得分:1)

宣教班将有:

@Entity
public class Mission {
...
    @ManyToMany(mappedBy = "missions")
    private List<User> users;
...
}

和用户类别将具有:

@Entity
public class User {
...

    @ManyToMany(mappedBy = "users")
    private List<Mission> missions;
...
}

这将在您的数据库中创建一个表,其名称类似于:missions_uesrs,并且只有MissionId和userId作为列。