일단 JPA에 대한 이해 먼저
JPA, Hibernate, 그리고 Spring Data JPA의 차이점
[JPA] 다양한 연관관계 매핑 - @OneToMany @ManyToOne @OneToOne @ManyToOne
Hibernate - OneToOne, OneToMany, ManyToOne and ManyToMany
@Entity
public class Post {
@Id
@GeneratedValue
private Long postId;
private String detail;
@ManyToMany
@JoinTable(
name = "POST_USER",
joinColumns = @JoinColumn(name = "POST_ID"),
inverseJoinColumns = @JoinColumn(name = "USER_ID"))
private List<User> participants;
// Constructors, Methods, ...
}
@Entity
public class User {
@Id
@GeneratedValue
private Long userId;
private String name;
@ManyToMany(mappedBy = "participants")
private Post post;
// Constructors, Methods, ...
}