# [JPA] ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ - ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„
Study Repository

[JPA] ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ - ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„

by rlaehddnd0422

๋ฐ”๋กœ ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ„๋‹จํžˆ ์งš๊ณ  ๋„˜์–ด๊ฐ€์ž๋ฉด, Member โ–ถ๏ธŽ Team์˜ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์งˆ ๋•Œ Member ์—”ํ‹ฐํ‹ฐ์—์„œ๋Š” Team ์—”ํ‹ฐํ‹ฐ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ, Team ์—”ํ‹ฐํ‹ฐ์—์„œ๋Š” Member์— ๋Œ€ํ•ด์„œ ์•Œ ์ˆ˜ ์—†์—ˆ์–ด์š”.

 

์ด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ์ฒด์—์„œ๋Š” ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ฃผ์—ˆ๊ณ , ํ…Œ์ด๋ธ”์—์„œ๋Š” ์™ธ๋ž˜ํ‚ค๋ฅผ ํ†ตํ•ด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  JPA์—์„œ๋Š” ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ

Member Entity์—๋Š” @ManyToOne ์–ด๋…ธํ…Œ์ด์…˜์„ Team ํ•„๋“œ์— ๋ถ™์—ฌ์ฃผ๊ณ  @JoinColumn ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ฐธ์กฐํ•  ์™ธ๋ž˜ํ‚ค๋ฅผ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

@Entity
public class Member{

    @Id
    @Column(name = "MEMBER_ID")
    private String id;

    private String username;

    // ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘

    @ManyToOne
    @JoinColumn(name="TEAM_ID")
    private Team team;
   
   	...
}

 

 

์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•ด ์ค€ ํ›„ ์ˆ˜์ •, ์กฐํšŒ, ์‚ฝ์ž… ์ž‘์—…์€ ๊ธฐ์กด๊ณผ ๋™์ผํ•˜์ง€๋งŒ ์‚ญ์ œ ์ž‘์—…์—์„œ๋Š” ์™ธ๋ž˜ํ‚ค ์ œ์•ฝ์กฐ๊ฑด์œผ๋กœ ์ธํ•ด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋จผ์ € ์‚ญ์ œํ•ด์ฃผ์–ด์•ผ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

member1.setTeam(null);
em.remove(member1);

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋‹จ๋ฐฉํ–ฅ์ด ์•„๋‹Œ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„

์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ผ๊ณ  ํ•จ์€ ํ™”์‚ดํ‘œ๋กœ ↔๏ธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ "Member ↔๏ธ Team"์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ด ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ๋Š” Member์—์„œ Team์„ ์•Œ ์ˆ˜ ์žˆ์Œ๊ณผ ๋™์‹œ์—, Team์—์„œ๋„ Member๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐ์ฒด ์—ฐ๊ด€๊ด€๊ณ„๋ถ€ํ„ฐ ์‚ดํŽด๋ด…์‹œ๋‹ค.

ํ•œ Team์—๋Š” ์—ฌ๋Ÿฌ Member๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ธฐ์กด์˜ Team์— List ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด์„œ members๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • Member -> Team ( Member.team ), Team -> Member ( Team.members ) ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ ํ…Œ์ด๋ธ” ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค.

 

 

[JPA] ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ - ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„

์—”ํ‹ฐํ‹ฐ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋‹ค๋ฅธ ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด, Order(์ฃผ๋ฌธ) ์—”ํ‹ฐํ‹ฐ๋Š” ์–ด๋–ค ์ƒํ’ˆ์„ ์ฃผ๋ฌธํ–ˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•ด Item(์ƒํ’ˆ) ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ด€์ด ์žˆ๊ณ , ํšŒ์›(Member)์ด ์ƒํ’ˆ(Item)์„ ์ฃผ๋ฌธ(Order)

rlaehddnd0422.tistory.com

ํ…Œ์ด๋ธ”์—์„œ๋Š” ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด, ๋‘ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์™ธ๋ž˜ํ‚ค๋กœ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ๊ณ ์žˆ์œผ๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ…Œ์ด๋ธ”์—์„œ๋Š” ๊ฐ์ฒด์™€ ๋‹ฌ๋ฆฌ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด์ค„ ๋ถ€๋ถ„์ด ์—†์Šต๋‹ˆ๋‹ค.

 

์ž ์ด์ œ, JPA๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”๊ฐ„ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Member์—๋Š” Member(N) โžก๏ธ Team(1) ์—ฐ๊ด€๊ด€๊ณ„ ๊ทธ๋Œ€๋กœ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋ณ€๊ฒฝ์ ์ด ์—†์Šต๋‹ˆ๋‹ค.

@Entity
public class Member {
    @Id
    @Column(name = "MEMBER_ID")
    private String id;

    private String username;

    @ManyToOne
    @JoinColumn(name = "TEAM_ID")
    private Team team;

 

Team์—๋Š” Team โžก๏ธ Member ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์œผ๋‹ˆ ๋ณ€๊ฒฝ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

@Entity
public class Team {

    @Id
    @Column(name = "TEAM_ID")
    private String id;

    private String name;

    @OneToMany(mappedBy = "team") // mappedBy = Team (team)
    private List<Member> members = new ArrayList<Member>();
@OneToMany(mappedBy = "team") 
private List<Member> members = new ArrayList<Member>();
  • @OneToMany : ํ•œ Team์—๋Š” ์—ฌ๋Ÿฌ Member๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Member์™€ Team์„ ๋‹ค๋Œ€์ผ, ๊ฑฐ๊พธ๋กœ ๋ณด๋ฉด Team๊ณผ Member๋Š” ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์—, Team ์—”ํ‹ฐํ‹ฐ์˜ member ํ•„๋“œ์— ๋‹ค์ค‘์„ฑ ์–ด๋…ธํ…Œ์ด์…˜ @OneToMany๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
    • mappedBy ์†์„ฑ : mappedBy ์†์„ฑ์€ ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋ฐ˜๋Œ€ ์ชฝ ๋งคํ•‘์˜ ํ•„๋“œ ์ด๋ฆ„์„ ๊ฐ’์œผ๋กœ ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • List<Member> members : ๊ฐ์ฒด ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘์—์„œ ์–ธ๊ธ‰ํ•œ List ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 

์–‘๋ฐฉํ–ฅ ๋งคํ•‘์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•˜์ง€ ์•Š์ฃ ? ์ด์ œ๋ถ€ํ„ฐ๋Š” ๋‹จ๋ฐฉํ–ฅ ๋งคํ•‘์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ Team์—์„œ๋„ Member๋กœ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();

Team team1 = em.find(Team.class, "team1");
List<Member> members = team1.getMembers();

for (Member member : members) {
    System.out.println("member.getUsername() = " + member.getUsername());
}

์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ 

 

[JPA] ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ - ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„

์—”ํ‹ฐํ‹ฐ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋‹ค๋ฅธ ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด, Order(์ฃผ๋ฌธ) ์—”ํ‹ฐํ‹ฐ๋Š” ์–ด๋–ค ์ƒํ’ˆ์„ ์ฃผ๋ฌธํ–ˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•ด Item(์ƒํ’ˆ) ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ด€์ด ์žˆ๊ณ , ํšŒ์›(Member)์ด ์ƒํ’ˆ(Item)์„ ์ฃผ๋ฌธ(Order)

rlaehddnd0422.tistory.com

 

์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ Team์˜ members ํ•„๋“œ์— ๋‹ค์ค‘์„ฑ ์–ด๋…ธํ…Œ์ด์…˜ @OneToMany๋ฅผ ์ง€์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

@OneToMany์˜ mappedBy ์†์„ฑ์— team๋„ ์ง€์ •ํ•ด์ฃผ์—ˆ๋Š”๋ฐ์š”, mappedBy ์†์„ฑ์€ ์™œ ํ•„์š”ํ• ๊นŒ์š”? ์ด ์งˆ๋ฌธ์— ๋Œ€๋‹ตํ•˜๊ธฐ์— ์ „์— ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์กฐ๊ธˆ ์ž์„ธํžˆ ์งš์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ๊ฐ์ฒด์—๋Š” ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ผ๋Š” ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœํžˆ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ 2๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์„œ๋กœ๋‹ค๋ฅธ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„ 2๊ฐœ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์„ ์ž˜ ๋ฌถ์–ด์„œ ๋งˆ์น˜ ์–‘๋ฐฉํ–ฅ์ธ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•  ๋ฟ์ด์—์š”.

 

๋ฐ˜๋ฉด์— ํ…Œ์ด๋ธ”์—์„œ๋Š” ์™ธ๋ž˜ํ‚ค ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ์Šต๋‹ˆ๋‹ค.

 

๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ” ๋งคํ•‘์—์„œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ์ฐธ์กฐ๋ฅผ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง„ ์ชฝ์—์„œ ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ” ๋งคํ•‘์—์„œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ์ฐธ์กฐ๋ฅผ ๋‘ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํฌ์ธํŠธ๊ฐ€ ๋‘ ์ง€์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, ์™ธ๋ž˜ํ‚ค๋Š” ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ณณ์„ ๋ช…์‹œํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ณณ์ด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ(์™ธ๋ž˜ํ‚ค ๊ด€๋ฆฌ์ž)์ด ๋ฉ๋‹ˆ๋‹ค. 

  • ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ์‹œ ๋‘ ์—ฐ๊ด€๊ด€๊ณ„ ์ค‘ ํ•˜๋‚˜๋ฅผ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 
  • ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ๋งŒ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ด€๊ด€๊ณ„์™€ ๋งคํ•‘๋˜๊ณ  ์™ธ๋ž˜ ํ‚ค๋ฅผ ๊ด€๋ฆฌ(๋“ฑ๋ก,์ˆ˜์ •,์‚ญ์ œ,์กฐํšŒ) ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
  • ๋ฐ˜๋ฉด ์ฃผ์ธ์ด ์•„๋‹Œ์ชฝ์—์„œ๋Š” ์ฝ๊ธฐ(์กฐํšŒ)๋งŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ์—๋Š” ๋ฐ˜๋“œ์‹œ mappedBy ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์—๋Š” mappedBy ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์€ ์–ด๋–ป๊ฒŒ ์ •ํ• ๊นŒ์š”?

์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ, ์™ธ๋ž˜ํ‚ค ๊ด€๋ฆฌ์ž๋Š” ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ์ชฝ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์˜ ๋‹ค๋Œ€์ผ, ์ผ๋Œ€๋‹ค ๊ด€๊ณ„์—์„œ๋Š” ํ•ญ์ƒ ๋‹ค ์ชฝ์ด ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ @ManyToOne์€ ํ•ญ์ƒ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ๋˜๋ฏ€๋กœ mappedBy๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„ ์ €์žฅ

์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์ €์žฅํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์—๋Š” ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ์ฃผ์ธ์ด ์•„๋‹Œ๊ณณ์—๋งŒ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ ์ •์ƒ์ ์œผ๋กœ ์ €์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();

    tx.begin();
    try
    {
        // Member์—๋Š” Member -> Team ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • X
        Member member1 = new Member("member1", "ํšŒ์›1");
        em.persist(member1);
        
        // ์ฃผ์ธ์ด ์•„๋‹Œ Team์—๋Š” Team -> Member ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • 
        Team team1 = new Team("team1", "ํŒ€1");
        team1.getMembers().add(member1);
        em.persist(team1);
        
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
    }finally {
        em.close();
    }

    emf.close();
}

  • Member ํ…Œ์ด๋ธ”์˜ TEAM_ID๊ฐ€ null๋กœ ์„ค์ •๋จ.
    • ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • X

๋ฐ˜๋ฉด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์—๋งŒ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด๋„ ์ฃผ์ธ์ด ์•„๋‹Œ ๊ณณ์—๋„ ๊ฐ’์ด ์ €์žฅ ๋ฉ๋‹ˆ๋‹ค.

public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
       
        try
        {	
        	// ํŒ€1 ์„ค์ • 
            Team team1 = new Team("team1", "ํŒ€1");
            em.persist(team1); // ์ €์žฅ
	            
            // ํšŒ์›1 ์„ค์ •
            Member member1 = new Member("member1", "ํšŒ์›1");
            member1.setTeam(team1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • Member->Team
            em.persist(member1); // ์ €์žฅ
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }finally {
            em.close();
        }

        emf.close();
    }

 

๊ทธ๋ ‡๋‹ค๋ฉด ์—ฐ๊ด€๊ด€๊ฒŒ์˜ ์ฃผ์ธ์—๋งŒ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ์—๋Š” ๊ฐ’์„ ์ €์žฅํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ?

๊ฐ์ฒด ๊ด€์ ์—์„œ๋Š” ์–‘์ชฝ ๋ฐฉํ–ฅ์— ๋ชจ๋‘ ๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด ๊ด€์ ๊ณผ ํ…Œ์ด๋ธ” ๊ด€์  ์–‘์ชฝ ๋ชจ๋‘๋ฅผ ๊ณ ๋ คํ•ด์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

 

JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ˆœ์ˆ˜ํ•œ ๊ฐ์ฒด ๊ด€์ ์—์„œ ๋ดค์„ ๋•Œ, ์–‘์ชฝ ๋ฐฉํ–ฅ์— ๋ชจ๋‘ ๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด Member -> Team ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค.

Team team1 = new Team("team1","ํŒ€1");
em.persist(team1);

Member member1 = new Member("member1", "ํšŒ์›1");
member1.setTeam(team1);
em.persist(member1);

List<Member> list = team1.getMembers();
System.out.println("size = " + list.size()); // expected : 1
                                        // result : 0

 

๊ฒฐ๋ก ์ ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ๋Š” ๊ฐ์ฒด๊นŒ์ง€ ๊ณ ๋ คํ•˜์—ฌ ์–‘์ชฝ ๋ชจ๋‘ ๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ค์‹œ๋‹ค.

// ํŒ€1 ์„ค์ • 
Team team1 = new Team("team1", "ํŒ€1");
em.persist(team1); // ์ €์žฅ

// ํšŒ์›1 ์„ค์ •
Member member1 = new Member("member1", "ํšŒ์›1");
member1.setTeam(team1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • Member->Team
team1.getMembers().add(member1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • Team->Member

em.persist(member1); // ์ €์žฅ

 

์–‘๋ฐฉํ–ฅ ๋งคํ•‘์ž„์„ ๋” ๊ฐ€์‹œ์ ์œผ๋กœ ๋ฆฌํŽ™ํ† ๋ง ํ•ด๋ด…์‹œ๋‹ค.

// Members Entity
public void setTeam(Team team){

	// ๊ธฐ์กด ํŒ€์ด ์žˆ์„ ๊ฒฝ์šฐ ์ œ๊ฑฐ
    if(this.team != null){
    	this.team.getMembers().remove(this);
    }
    
    this.team = team;
    team.getMembers().add(this);
}
// ํŒ€1 ์„ค์ • 
Team team1 = new Team("team1", "ํŒ€1");
em.persist(team1); // ์ €์žฅ

// ํšŒ์›1 ์„ค์ •
Member member1 = new Member("member1", "ํšŒ์›1");
member1.setTeam(team1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • Member->Team
// team1.getMembers().add(member1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • Team->Member

em.persist(member1); // ์ €์žฅ
  • ๊ธฐ์กด ํŒ€์ด ์žˆ์„ ๊ฒฝ์šฐ ํ•ด๋‹น team์—์„œ ํ•ด๋‹น member ๊ฐ์ฒด๋ฅผ ์ง€์›Œ์ฃผ๊ณ  ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
    • ์‚ฌ์‹ค ๊ธฐ์กด ํŒ€์ด ์žˆ์„ ๊ฒฝ์šฐ ๊ธฐ์กด ํŒ€์—์„œ ํ•ด๋‹น ๋ฉค๋ฒ„๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š์•„๋„ Team์ด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ž˜ํ‚ค๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.  

member1 -> team1 ์—์„œ member1 -> team2 ๋กœ ๋ณ€๊ฒฝํ•ด๋ด…์‹œ๋‹ค.

try
{
    // ํŒ€1 ์„ค์ •
    Team team1 = new Team("team1", "ํŒ€1");
    em.persist(team1); // ์ €์žฅ

    Team team2 = new Team("team2", "ํŒ€2");
    em.persist(team2); // ์ €์žฅ
    
    // ํšŒ์›1 ์„ค์ •
    Member member1 = new Member("member1", "ํšŒ์›1");
    member1.setTeam(team1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ • Member -> Team
    member1.setTeam(team2); // ์—ฐ๊ด€๊ด€๊ณ„ ๋ณ€๊ฒฝ Member -> Team (team2)
    em.persist(member1); // ์ €์žฅ

    for (Member member : team1.getMembers()) {
        System.out.println("member.getUsername() = " + member.getUsername());
    }
    
    tx.commit();
} catch (Exception e) {
    tx.rollback();
}finally {
    em.close();
}
  • member1->team1 ์—์„œ member1->team2๋กœ ๋ฐ”๊พผ ํ›„ team1์˜ getMembers()๋กœ team1์— ์†ํ•œ ํšŒ์› ํ™•์ธ

์ถœ๋ ฅ ๊ฒฐ๊ณผ

member.getUsername() = ํšŒ์›1

์ƒˆ๋กœ์šด ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์—์„œ๋Š” team1์—์„œ members๋ฅผ ์กฐํšŒํ•ด๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์™ธ๋ž˜ ํ‚ค์—๋Š” ๊ด€๊ณ„๊ฐ€ ๋Š์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ฌด๊ฒƒ๋„ ์กฐํšŒ๋˜์ง€ ์•Š์ง€๋งŒ(๋งˆ์ฐฌ๊ฐ€์ง€๋กœ member1์—์„œ๋„ team์„ ์กฐํšŒํ•  ์ˆ˜ ์—†์Œ),

๋ฌธ์ œ๋Š” ๊ด€๊ณ„๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ์‚ด์•„์žˆ๋Š” ์ƒํƒœ์—์„œ teamA์˜ getMembers()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด member1์ด ๋ฆฌํ„ด๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€๊ฒฝ๋œ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ๋Š” ๊ด€๊ณ„๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ข…๋ฃŒ๋œ๋‹ค๋ฉด, ์ด ๊ณต๊ฐ„์— ๋ณด๊ด€๋˜์–ด ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋“ค์€ ์ค€์˜์† ์ƒํƒœ๋กœ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค.
์ค€์˜์† ์ƒํƒœ์˜ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋Š” ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์—์„œ ๋ถ„๋ฆฌ๋˜์–ด์„œ ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ์™ธ๋ž˜ํ‚ค ๊ด€๊ณ„๋„ ํ•จ๊ป˜ ๋Š์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ฆ‰, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ์™ธ๋ž˜ํ‚ค๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•œ ๊ฒƒ์ด๋ฉฐ, ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๊ด€๋ฆฌ๋ฅผ ๋ฐ›๋Š” ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๊ฐ€ ์ค€์˜์† ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์™€์˜ ๊ด€๊ณ„๊ฐ€ ๋Š์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋‹น ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด์™€ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์˜ ์™ธ๋ž˜ํ‚ค ๊ด€๊ณ„๋„ ๋Š์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ƒˆ๋กœ์šด ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์—์„œ team1์˜ members ์กฐํšŒ

try
{
    Team team1 = em.find(Team.class, "team1");

    for (Member member : team1.getMembers()) {
        System.out.println("member.getUsername() = " + member.getUsername());
    }
    
    tx.commit();
} catch (Exception e) {
    tx.rollback();
}finally {
    em.close();
}
  • ์ถœ๋ ฅ ๊ฒฐ๊ณผ X

<์ •๋ฆฌ>

  • ๋‹จ๋ฑกํ•ญ ๋งคํ•‘์— ๋น„ํ•ด ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์€ ์กฐ๊ธˆ ๋ณต์žกํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ๋„ ์ •ํ•ด์•ผ ํ•˜๊ณ , ๋‘ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋กœ์ง๋„ ์ž˜ ๊ด€๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹จ๋ฐฉํ–ฅ ๋งคํ•‘์—์„œ๋Š” ๊ฐ€๋ฆฌํ‚ค๋Š” ์ชฝ์ด ์–ธ์ œ๋‚˜ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ, ์™ธ๋ž˜ํ‚ค ๊ด€๋ฆฌ์ž๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
  • ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์˜ ์žฅ์ ์€ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ์ฒด๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ ๋‹จ๋ฐฉํ–ฅ์„ ์šฐ์„ ์œผ๋กœ ํ•˜๊ณ  ๋ฐ˜๋Œ€์ชฝ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์„ ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•ฉ์‹œ๋‹ค.
  • ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์€ ๋น„์ฆˆ๋‹ˆ์Šค ์ค‘์š”๋„์™€ ๊ด€๊ณ„๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ฌด์กฐ๊ฑด ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์ชฝ์„ ์ฃผ์ธ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ๋ฐ˜๋Œ€์ชฝ์€ mappedBy ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.
    • + Tip : @ManyToOne ์—์„œ๋Š” ํ•ญ์ƒ Many์ชฝ์ด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ.

<์ฐธ๊ณ ์ž๋ฃŒ>

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ