๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

5 - 3) ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ๊ธฐ์ดˆ - ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ

iseunghan 2020. 10. 19. 12:26
๋ฐ˜์‘ํ˜•

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ

JPA๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜, ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ด๋ก ์ด ๋ถ€์กฑํ•˜์‹  ๋ถ„๋“ค์ด JPA์˜ ๊ธฐ๋ณธ ์ด๋ก ์„ ํƒ„ํƒ„ํ•˜๊ฒŒ ํ•™์Šตํ•ด์„œ ์ดˆ๋ณด์ž๋„ ์‹ค๋ฌด์—์„œ ์ž์‹ ์žˆ๊ฒŒ JPA๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๏ฟฝ

www.inflearn.com

 

๐Ÿš€ ์–‘๋ฐฉํ–ฅ ๋งคํ•‘

์ „์— ํ–ˆ๋˜ ๋‹จ๋ฐฉํ–ฅ ๋งคํ•‘์—์„œ Team ํ…Œ์ด๋ธ”์— Member๋ฅผ List๋กœ ์ถ”๊ฐ€ํ•ด์ค˜์„œ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๋งคํ•‘ ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.

@Entity
public class Team {
	@Id @GenerateValue
    private Long id;
    
    private String name;
    
    @OneToMany(mappedBy = "team") // Memberํด๋ž˜์Šค์— ์žˆ๋Š” team ๊ฐ์ฒด์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.
    private List<Member> members = new ArrayList<Member>();
}
@Entity
public class Member {
	...
    @ManyToOne
    @JoinColumn(name = "TEAM_ID")
    private Team team; // <--------- team์„ mappedBy๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
}

์™ธ๋ž˜ํ‚ค ํ•˜๋‚˜๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜์–ด์„œ -> ์–‘์ชฝ์œผ๋กœ ์กฐ์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT *
FROM MEMBER M
JOIN TEAM T ON M.TEAM_ID = T.TEAM_ID

SELECT *
FROM TEAM T
JOIN MEMBER M ON T.TEAM_ID = M.TEAM_ID

 

โœ…์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ(Owner)

์–‘๋ฐฉํ–ฅ ๋งคํ•‘ ๊ทœ์น™

์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ (Member) ์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ (Team)
                   @ManyToOne
                   @JoinColumn(name = "TEAM_ID")
                   private Team team;
@OneToMany(mappedBy = "team")
์™ธ๋ž˜ํ‚ค๋ฅผ ๊ด€๋ฆฌ(๋“ฑ๋ก, ์ˆ˜์ • ๊ฐ€๋Šฅ) ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅ

Member์˜ Team์„ ์ฃผ์ธ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ผ๋Œ€ ๋‹ค ์ผ๋•Œ, ๋‹ค ์ชฝ์ด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ๋˜๋Š”๊ฒŒ ๊ฐ€์žฅ ์ข‹๋‹ค.

 

!! ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์‹œ ๊ฐ€์žฅ ๋งŽ์ด ํ•˜๋Š” ์‹ค์ˆ˜

Team team = new Team();
team.setName("TeamA");
em.persist(team);

Member member = new Member();
member.setName("member1");
// ์—ญ๋ฐฉํ–ฅ ๋งŒ ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ •?
team.getMembers().add(member);

em.persist(member);

์–‘์ชฝ ๋‹ค ๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค!

Team team = new Team();
team.setName("TeamA");
em.persist(team);

Member member = new Member();
member.setName("member1");

team.getMembers().add(member);
member.setTeam(team); // ** ์–‘์ชฝ๋‹ค ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค!

em.persist(member);

์ˆœ์ˆ˜ ๊ฐ์ฒด ์ƒํƒœ๋ฅผ ๊ณ ๋ คํ•ด์„œ ํ•ญ์ƒ ์–‘์ชฝ์— ๊ฐ’์„ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.

 

โœ…์—ฐ๊ด€๊ด€๊ณ„ ํŽธ์˜ ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ

์–‘์ชฝ์— ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š”๊ฑธ ๊นŒ๋จน์„์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ํŽธ์˜์ƒ ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

Team team = new Team();
...

Member member = new Member();
member.setUserName("memberA");
member.setTeam(team); // member.team ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ •
em.persist(member);

team.getMembers().add(member); // ์ด ์ฝ”๋“œ๋ฅผ ๊นŒ๋จน์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ setTeam ๋ฉ”์†Œ๋“œ์— ์ถ”๊ฐ€ ์‹œ์ผœ์ค€๋‹ค.

------------------------------------

// (Member) ์— setTeam์„ ์„ค์ •ํ• ๋•Œ, team์˜ list์— ํ•จ๊ป˜ ์ €์žฅํ•˜๊ฒŒ๋” ์„ค์ •ํ•ด์ค€๋‹ค.
public void setTeam(Team team) {
	this.team = team;
    team.getMembers().add(this);
}

 

์‚ฌ์‹ค์€ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์ชฝ์—๋งŒ ๊ฐ’ ์„ธํŒ…ํ•ด์ค˜๋„ ๊ฐ’์€ ์ž˜ ๋“ค์–ด๊ฐ„๋‹ค.

Team team = new Team();
team.setName("TeamA");
em.persist(team);

Member member = new Member();
member.setName("member1");
em.persist(member);

em.flush();
em.clear();

// team.getMembers().add(member); <- team์€ ์–ด์ฐจํ”ผ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ ๋‚ ๋ฆด ๋•Œ, jpa๊ฐ€ ์ณ๋‹ค๋„ ์•ˆ๋ณธ๋‹ค.
member.setTeam(team); 

Team team = em.find(Team.class, team.getId());
System.out.println(team.getMembers().get(0).getName());	// member1

team.getMembers().add(member)๋ฅผ ์•ˆํ•ด์ค˜๋„, ๊ฐ’์„ ์ฐ์–ด๋ณด๋ฉด ์ปฌ๋ ‰์…˜์— ๊ฐ’์ด ๋“ค์–ด์žˆ๋‹ค.

๊ทผ๋ฐ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ๋ญ˜๊นŒ?

๋‚˜์ค‘์— ์ˆœ์ˆ˜ํ•˜๊ฒŒ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ๊ทธ๋•Œ ํ˜ผ๋ž€์„ ์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์ˆ˜ํ•œ ์ž๋ฐ” ๊ฐ์ฒด ์ƒํƒœ๋ฅผ ๊ณ ๋ ค ํ•˜์—ฌ ์–‘์ชฝ์—๋‹ค๊ฐ€ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค!

 

Team team = new Team();
team.setName("TeamA");
em.persist(team);

Member member = new Member();
member.setName("member1");
em.persist(member);

// em.flush();	์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์ฟผ๋ฆฌ๊ฐ€ ๋‚˜๊ฐ€์งˆ ์•Š๋Š”๋‹ค.
// em.clear();

member.setTeam(team); 

Team team = em.find(Team.class, team.getId()); // 1์ฐจ ์บ์‹œ์—์„œ ๊บผ๋‚ด์˜ด
System.out.println(team.getMembers().get(0).getName());	// error!

์ด๋Ÿฌํ•œ ์ด์œ  ๋•Œ๋ฌธ์—, ์–‘์ชฝ์—๋‹ค๊ฐ€ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค!!

 

Team team = new Team();
team.setName("TeamA");
em.persist(team);

Member member = new Member();
member.setName("member1");
member.setTeam(team);	// team ์„ค์ •
em.persist(member);

em.flush();
em.clear();

Team team = em.find(Team.class, team.getId());
System.out.println(team.getMembers().get(0).getName());	// member1

team์— ๋Œ€ํ•ด์„œ๋Š” persist๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์—ฐ๊ด€๊ด€๊ณ„ ์ฃผ์ธ์ธ member ์ชฝ์—์„œ persist๋ฅผ ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ €์žฅ์ด ๋œ๋‹ค!

๋ฐ˜์‘ํ˜•