umanking / blog-comment

blog comment using utterance
https://umanking.github.io
1 stars 1 forks source link

2020/10/26/jpa-mapping-2/ #10

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

[JPA] 연관관계 매핑 (연관관계 편의 메서드) | 앤드류’s 개발 블로그

  1. 들어가며 JPA에서 양방향 연관관계를 매핑할때, 객체관점에서 편의 메서드를 작성하는 방법에 대해서 알아보자. 이번에는 양방향 매핑을 할때, 객체 관점에서 어떻게 처리할지?를 고민해보자.

https://umanking.github.io/2020/10/26/jpa-mapping-2/

ShanePark commented 2 years ago

안녕하세요. 제법 오래된 글이긴 하지만 마지막 코드에서

team.getMembers().add(this); // team -> member

부분이 Null Point Exception을 부를 가능성이 있기 때문에

public void setTeam(Team team) {
    if (this.team != null) {
        this.team.getMembers().remove(this);
    }
    this.team = team; 
    if (team != null)
        team.getMembers().add(this);
}

이렇게 작성하는게 좋을 것 같습니다. 혹시나 다른분들이 참고하실때 도움 되었으면 해서 댓글 남깁니다

저도 덕분에 잘 참고했습니다. 감사합니다.