๐ค ๊ธฐ์กด์ ๋ฌธ์ ์
๊ฐ์ฒด๋ฅผ ํ ์ด๋ธ์ ๋ง์ถ์ด ๋ชจ๋ธ๋ง
try{
//ํ ๋ฑ๋ก
Team team = new Team();
team.setName("TeamA");
em.persist(team);
//๋งด๋ฒ ๋ฑ๋ก
Member member = new Member();
member.setUsername("member1");
member.setTeamId(team.getId()); // **์ธ๋ํค ์๋ณ์๋ฅผ ์ง์ ๋ค๋ฃจ๊ณ ์๋ค.**
em.persist(member);
//์กฐํ
Member findMember = em.find(Member.class, member.getId());
Long findTeamId = findMember.getTeamId(); // ๋งด๋ฒ๊ฐ์ฒด์ ์ ์ฅ๋ ํ์์ด๋ ์ฆ ์ธ๋ํค๋ฅผ ์ฐพ๋๋ค
Team findTeam = em.find(Team.class, findTeamId); //์ฐพ์ ์ธ๋ํค๋ฅผ ์ฌ์ฉํด์ ๋ค์ ์กฐํ
tx.commit();
}
์์ ์ฝ๋์ฒ๋ผ ๊ฐ์ฒด๋ฅผ ํ ์ด๋ธ์ ๋ง์ถ์ด์ ๋ชจ๋ธ๋งํ๊ธฐ ๋๋ฌธ์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃจ๋๊ฒ์ด ์๋๋ผ ๊ด๊ณํ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ๋ค๋ฃจ๊ณ ์์ ๋ฟ ์๋๋ผ ์ ์ฐจ๊ฐ ๋ณต์กํ๊ฒ ๋๊ปด์ง๋ค.
- ํ ์ด๋ธ์ ์ธ๋ ํค๋ก ์กฐ์ธ์ ์ฌ์ฉํด์ ์ฐ๊ด๋ ํ ์ด๋ธ์ ์ฐพ๋๋ค
- ๊ฐ์ฒด๋ ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํด์ ์ฐ๊ด๋ ๊ฐ์ฒด๋ฅผ ์ฐพ๋๋ค.
โ ๊ฐ์ฒด ์งํฅ ๋ชจ๋ธ๋ง
๐ ๋จ๋ฐฉํฅ ๋งคํ
public class Member {
@Id
@GeneratedValue
private Long id;
@Column(name = "username")
private String name;
@ManyToOne
@JoinColumn(name = "TEAM_ID")
private Team team;
...
}
Memberํด๋์ค ์ ์ฅ์์ Team๊ณผ N : 1 ๊ด๊ณ์ด๊ธฐ ๋๋ฌธ์ @ManyToOne ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ์ฐธ์กฐ๊ด๊ณ์ ํ ์ด๋ธ์ ์ธ๋ ํค๋ฅผ ๋งคํํ๋ค.
try{
//ํ ๋ฑ๋ก
Team team = new Team();
team.setName("TeamA");
em.persist(team);
//๋งด๋ฒ ๋ฑ๋ก
Member member = new Member();
member.setUsername("member1");
member.setTeam(team); // ํ์ ์์ด๋๊ฐ ์๋ ํ ๊ฐ์ฒด๋ฅผ ๊ทธ๋๋ก ์ธํ
em.persist(member);
//์กฐํ
Member findMember = em.find(Member.class, member.getId());
findMember.getTeam(); // ๋ฐ๋ก findMember์ ์ฐ๊ด๋ ํ๊ฐ์ฒด๋ฅผ ์กฐํํ ์ ์๋ค.
tx.commit();
}
...
๊ฐ์ฒด์งํฅ ์ค๋ฝ๊ฒ ๋ฐ๋ก ์ฐ๊ด๋ ๋ ํผ๋ฐ์ค๋ค์ ๊ฐ์ง๊ณ ์ฌ ์ ์๋ค.
๐ ์๋ฐฉํฅ ๋งคํ
๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์๋ ์ธ๋ ํค ํ๋๋ก ์์ชฝ ํ
์ด๋ธ์ ์กฐ์ธํด์ Member์ Team์ ์ ๋ณด๋ฅผ ์กฐํํ ์ ์์์ง๋ง ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํด์๋ ๋ถ๊ฐ๋ฅํ๋ค.
์๋ฐฉํฅ ์กฐํ๊ฐ ๊ฐ๋ฅํ๋๋ก ๊ด๊ณ๊ฐ ๋๋ ๋ ๊ฐ์ฒด(Entity)์ฌ์ด์ ๋ชจ๋ ์ฐ๊ด๊ด๊ณ๋ฅผ ์ค์ ํด ์ค์ผ๋ก์จ ์๋ฐฉํฅ ์กฐํ,์์ ์ด ๊ฐ๋ฅํด์ง๋ค.
๐ก ์ฐ๊ด๊ด๊ณ ์ฃผ์ธ? mappedBy?
์๋ฐฉํฅ ๊ด๊ณ์์ ์๋ฅผ๋ค์ด ์์ ์ ํด์ผํ๋ค๋ฉด Memberํด๋์ค์ team์ ์์ ํด์ผํ ๊น์? Team์ members ๋ฆฌ์คํธ๋ฅผ ์์ ํด์ผ ํ ๊น์? ๋ผ๋ ์๋ฌธ์ด ์๊ธธ ์ ์๋ค.
์ฆ ์ธ๋ ํค๋ฅผ ์ด๋์ ๊ด๋ฆฌํ ์ง์ ๋ฌธ์ ๊ฐ ์๊ธฐ๊ฒ ๋๋๋ฐ ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ์ํด ์ธ๋ํค๋ฅผ ๊ด๋ฆฌํ์ง ์๋ ๊ฐ์ฒด์ mappedBy์ ์ฌ์ฉํด์ ๋ค๋ฅธ ๊ฐ์ฒด ์ฆ ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ผ๋ก๋ถํฐ ๋งคํ ๋์ด์ ๋ค ๋ผ๊ณ ์ค์ ํ์ฌ ์ฃผ์ธ ๊ฐ์ฒด๋ง์ด ๋ฑ๋ก, ์์ ์ด ๊ฐ๋ฅํ๊ณ ๋งคํ๋์ด์ง ๊ฐ์ฒด๋ ์ฝ๊ธฐ๋ง ๊ฐ๋ฅํ๋ค.
- ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ๋ง์ด ์ธ๋ ํค๋ฅผ ๊ด๋ฆฌ(๋ฑ๋ก, ์์ )
- ์ฃผ์ธ์ด ์๋์ชฝ์ ์ฝ๊ธฐ๋ง ๊ฐ๋ฅ
- ์ฃผ์ธ์ mappedBy ์์ฑ์ ์ฌ์ฉํ์ง ์๊ณ , ์๋์ชฝ์ mappedBy ์์ฑ์ผ๋ก ์ฃผ์ธ์ ์ง์ ํด์ค๋ค.
๐ก ๋๊ตฌ๋ฅผ ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ผ๋ก?
์ธ๋ ํค๊ฐ ์๋ ๊ณณ์ ์ฃผ์ธ์ผ๋ก ์ง์ ํ๋ค.
DB๊ด์ ์์ ๋ณด๋ฉด FK๊ฐ ์๋๊ณณ์ด N์ด๊ณ PK์ชฝ์ด 1์ด๋ค (N : 1)
FK๋ฅผ ๊ด๋ฆฌํ๋ ํ
์ด๋ธ(Member)๊ณผ ๋์๋๋ ๊ฐ์ฒด(Member)๋ฅผ ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ผ๋ก ์ค์ ํด์ผ updateํ์๋ ์ง๊ด์ ์ผ๋ก ์๊ธฐ ์ฝ๋ค.
๐ก ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ชฝ์๋ง ๊ฐ์ ์ธํ ํด์ฃผ๋ฉด ๋ ๊น?
try {
//ํ ๋ฑ๋ก
Team team = new Team();
team.setName("TeamA");
em.persist(team);
//๋งด๋ฒ ๋ฑ๋ก
Member member = new Member();
member.setUsername("member1");
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
Member findMember = em.find(Member.class, member.getId());
List<Member> members = findMember.getTeam().getMembers();
for ( Member mbr : members){
System.out.println("mbr = " + mbr.getUsername());
}
tx.commit();
}
์ ์ฝ๋์์๋ ์ ์์ ์ผ๋ก member๋ฆฌ์คํธ๋ฅผ ์กฐํํ ์ ์๋ค ๊ทธ ์ด์ ๋ flush๋ฅผ ํตํด sql๋ฌธ์ commit์ด์ ์ ๊ฐ์ ๋ก ์์ฒญํ ํ clear๋ฅผ ์ฌ์ฉํด์ 1์ฐจ์บ์๋ฅผ ๋น์์ค์ผ๋ก์จ ๋ค์์ ์ฝ๋์ธ em.find๋ฅผ ํตํด ์กฐํ๋ ๋ 1์ฐจ์บ์์ ๋ฐ์ดํฐ๊ฐ ์๊ธฐ๋๋ฌธ์ DB์ select๋ฌธ์ ์์ฒญํ๊ฒ๋๊ณ , ์ฐ๊ด๊ด๊ณ ๋งคํ์ ์ํด์ ์ฐ๊ด๋ ํ ์ด๋ธ์ joinํ์ฌ member๋ฆฌ์คํธ๋ selectํ์ฌ 1์ฐจ์บ์์ ์ ์ฅํ๊ฒ ๋๋ค.
์ด๋๋ฌธ์ member๋ฆฌ์คํธ๊ฐ ์กฐํ๋ ์ ์์๋ค.
ํ์ง๋ง flush์ clear๋ฅผ ํ์ง ์๋๋ค๋ฉด ์ฐ๊ด๊ด๊ณ๊ฐ ์ค์ ๋๊ธฐ ์ด์ ์ ํ๊ณผ ๋งด๋ฒ ๊ฐ์ฒด๊ฐ 1์ฐจ ์บ์์ ๊ทธ๋๋ก ๋จ์์๊ธฐ ๋๋ฌธ์ member๋ฆฌ์คํธ๋ ์๋ฌด๋ฐ ๊ฐ๋ ๊ฐ์ง๊ณ ์์ง ์๊ฒ๋๋ค.
์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ์ํด ์๋ฐฉํฅ ์ฐ๊ด๊ด๊ณ๋ฅผ ์ฌ์ฉํ ๋์๋ ์์ชฝ์ ๋ค ๊ฐ์ ์ธํ
ํด์ฃผ๋๊ฒ์ด ๋ง๋ค.
try {
//ํ ๋ฑ๋ก
Team team = new Team();
team.setName("TeamA");
em.persist(team);
//๋งด๋ฒ ๋ฑ๋ก
Member member = new Member();
member.setUsername("member1");
member.setTeam(team); // ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ชฝ์ ๊ฐ ์ธํ
1
em.persist(member);
team.getMembers().add(member); // ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ด ์๋์ชฝ์ ๊ฐ ์ธํ
2
//em.flush();
//em.clear();
Member findMember = em.find(Member.class, member.getId());
List<Member> members = findMember.getTeam().getMembers();
for ( Member mbr : members){
System.out.println("mbr = " + mbr.getUsername());
}
tx.commit();
}
์์ ๊ฐ์ฒด ์ํ๋ฅผ ๊ณ ๋ คํด์ ํญ์ ์์ชฝ์ ๊ฐ์ ์ค์ ํด์ผํ๋ค.
๊ฐ์ 2๋ฒ ์ค์ ํ๊ธฐ๋๋ฌธ์ ์ค์์ ์ฌ์ง๊ฐ ์๋ค ์ด๋ฌํ ์ค์๋ฅผ ์ค์ด๊ณ ์ ํธ์ ๋ฉ์๋๋ฅผ ์์ฑํ ์ ์๋ค.
// Memeber
public void changeTeam(Team team){
this.team = team; //team์ ์ค์ ํด์ฃผ๋ ์์ ์
team.getMembers.add(this); //๋ฐ๋ก this(Member)๋ฅผ member๋ฆฌ์คํธ์ ์ถ๊ฐ
}
๐
- toString(), lombok, JSON์์ฑ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฑ ๋ฌดํ๋ฃจํ๋ฅผ ์กฐ์ฌํ์!
- ๋จ๋ฐฉํฅ ๋งคํ๋ง์ผ๋ก๋ ์ด๋ฏธ ์ฐ๊ด๊ด๊ณ ๋งคํ ์๋ฃ๋ ์ํ์ด๋ค.
- ์๋ฐฉํฅ ๋งคํ์ ๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ์กฐํ(๊ฐ์ฒด ๊ทธ๋ํ ํ์) ๊ธฐ๋ฅ์ด ์ถ๊ฐ๋ ๊ฒ ๋ฟ์ด๋ค.
- ์ค๋ฌด์์๋ JPQL์์ ์ญ๋ฐฉํฅ์ผ๋ก ํ์ํ ์ผ์ด ๋ง๋ค.
- ๋จ๋ฐฉํฅ ๋งคํ์ ์ ํ๊ณ ์๋ฑกํญ์ ํ์ํ ๋ ์ถ๊ฐํด๋ ๋๋ค.(ํ ์ด๋ธ์ ์ํฅ์ ์ฃผ์ง ์์)
๐ ์ฐธ๊ณ ๋ฐ ์๋ฃ ์ถ์ฒ : ์๋ฐ ORM ํ์ค JPA ํ๋ก๊ทธ๋๋ฐ - ๊ธฐ๋ณธํธ (๊น์ํ)
'Backend > JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] ์์๊ด๊ณ ๋งคํ, @MappedSuperclass (0) | 2024.03.07 |
---|---|
[JPA] ๋ค์ํ ์ฐ๊ด๊ด๊ณ ๋งคํ ( ManyToOne, OneToMany, OneToOne, ManyToMany ) (0) | 2024.03.04 |
[JPA] JPA ๊ฐ์ฅ ์ค์ํ 2๊ฐ์ง - ์ํฐํฐ ๋งคํ (0) | 2024.02.10 |
[JPA] JPA ๊ฐ์ฅ ์ค์ํ 2๊ฐ์ง - ์์์ฑ ์ปจํ ์คํธ (0) | 2024.02.09 |
[JPA] SQL ์ค์ฌ์ ๊ฐ๋ฐ์ ๋ฌธ์ ์ ๊ณผ JPA๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ด์ (0) | 2024.02.08 |