简体   繁体   中英

How to manage several entities with JPA?

Context

I'm trying to create a simple API to learn Spring Boot and JPA. Informations are stored into H2 database.

I have 2 entities : mission and user .

Several users are assigned to a mission. And users can be assigned to different missions.

So, my class Mission has an attribute private ArrayList<User> users; .

Objective

My goal is to create some requests as :

  • (GET request) IP/missions/123456/users : Get all users assigned to mission 123456
  • (PUT request) IP/missions/456789 : Assigned users to mission 456789

So, tied both entities.

Problematic

But I don't know how to store/tie informations relative to mission and user . The good way is to create an "associative table" with the scheme Assignements(id_user, id_mission) ?

Thanks for help!

Edit 1 : Code

Entity Mission

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

@Entity
public class User {

    @Id
    private String id;
    private String name;

    public User() {
    }

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

    // getters and setters
}

SQL

I use a SQL script. Currently, script looks like :

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)

Edit 2 : New code (with @ManyToMany)

Entities

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

}

Repositories

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

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

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

The best would be to implement a joining table, just as you said. That would be something like:

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

Then a @ManyToMany using this table as mapping or entity like MissionUser and map both User and Mission as @OneToMany to it.

The mission class will have:

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

and User class will have:

@Entity
public class User {
...

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

this will create a table in your database with name something like: missions_uesrs and it will have missionId and userId as only columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM