728x90
์ฐ๊ด๊ด๊ณ ํธ์ ๋ฉ์๋๋ฅผ ์์ฑํ ๋, ๋ฌธ์ ๊ฐ ๋๋ ์ํฉ๊ณผ ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฐฉ๋ฒ๋ค์ ์ดํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
๋ฌธ์ ๊ฐ ๋๋ ์ฝ๋
public class Member {
private Team team;
public void setTeam(Team team){
this.team = team;
team.getMembers().add(this);
}
}
์์ setTeam() ๋ฉ์๋๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๊ทธ๋ฆผ์ ๋ณด๋ฉด ์ ์ ์๋ฏ์ด, setTeam(teamA) ์ดํ setTeam(teamB)๋ฅผ ํ๊ฒ ๋๋ฉด teamA์์ ๊ด๊ณ๊ฐ ์ญ์ ๋์ง ์์ ์ฒด ๋จ์์๊ฒ ๋ฉ๋๋ค.
์ฝ๋ ์์
public class Member {
private Team team;
public void setTeam(Team team){
if(this.team != null){
this.team.getMembers().remove(this);
}
this.team = team;
team.getMembers().add(this);
}
}
Reference
[์๋ฐ ORM ํ์ค JPA ํ๋ก๊ทธ๋๋ฐ - ๊น์ํ]
728x90
'๐๏ธ Spring > JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] ์์๊ด๊ณ ๋งคํํ๋ ๋ฐฉ๋ฒ (0) | 2021.12.15 |
---|---|
[JPA] ์ฐ๊ด๊ด๊ณ ๋งคํํ๋ ๋ฐฉ๋ฒ (2) | 2021.12.15 |
[JPA] - @JoinColumn๊ณผ ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ (mappedBy) (2) | 2021.12.14 |
[JPA] SEQUENCE (& TABLE) ์ ๋ต์์ ์ฒซ ํธ์ถ์ด 2๋ฒ ์ด๋ฃจ์ด์ง๋ ์ด์ (0) | 2021.12.14 |
[JPA] ์๋ณ์(๊ธฐ๋ณธ ํค) ์ ํ ์ ๋ต (0) | 2021.12.14 |