JPA 양방향 연관관계에서의 주인

Don Lee·2022년 6월 10일
0

EpiTIL

목록 보기
20/24

양방향 연관관계에서의 주인

@Entity
public class Member {
    @Id @GeneratedValue
    private Long id;
    
    private String memberName;
    
    @ManyToOne
    @JoinColumn("team_id")
    private Team team;
}

@Entity
public class Team {
    @Id @GeneratedValue
    private Long id;
    
    private String teamName;
    
    @OneToMany(mappedBy = "team")
    List<Member> members = new ArrayList<>();
}
💡 결론:
  • 매핑의 주인은 @ManyToOne을 가지고 있는 클래스(or 필드)
  • mappedBy를 사용하는 곳은 @OneTomany를 가지고 있는 클래스(or 필드)

위에서 @OneToMany(mappedBy = "team") 의 의미는 team 이라는거에 매핑한다는 것이다. 따라서 연관관계 매핑의 주인은 team 을 가지고 있는 Member 클래스가 된다.

양방향 연관관계(1:N) 관계를 영어로 설명하면 다음과 같다.

Member is the owner between relationship. Team is mapped by a list of member(s) where team is.

https://ch4njun.tistory.com/m/274


profile
쾌락코딩

0개의 댓글