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

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

by rlaehddnd0422

์—”ํ‹ฐํ‹ฐ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋‹ค๋ฅธ ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

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

 

๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ์—”ํ‹ฐํ‹ฐ ๊ฐ„ ์—ฐ๊ด€์„  ๋งบ์„๊นŒ์š”?

Order-Member-Item ์—”ํ‹ฐํ‹ฐ

 

"๊ฐ์ฒด"์—์„œ๋Š” ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์–ด๋–ป๊ฒŒ ๋งบ์„๊นŒ์š”? ๋ฐ”๋กœ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ์Šต๋‹ˆ๋‹ค.

"ํ…Œ์ด๋ธ”"์—์„œ๋Š” ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์–ด๋–ป๊ฒŒ ๋งบ์„๊นŒ์š”? ๋ฐ”๋กœ ์™ธ๋ž˜ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ์Šต๋‹ˆ๋‹ค.

 

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

 

๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์•Œ์•„๋ณด๊ธฐ์— ์•ž์„œ, ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ์˜ ์—”ํ‹ฐํ‹ฐ๊ฐ„ ๊ด€๊ณ„์— ๋Œ€ํ•œ ํ‚ค์›Œ๋“œ๋ฅผ ์•Œ์•„๋‘๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.


์—”ํ‹ฐํ‹ฐ ์—ฐ๊ด€๊ด€๊ณ„ ํ‚ค์›Œ๋“œ ์•Œ์•„๋ณด๊ธฐ 

๋ฐฉํ–ฅ[direction]

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

 

๋‹จ๋ฐฉํ–ฅ์€ ์˜ˆ๋ฅผ๋“ค์–ด, Member๋ผ๋Š” ์—”ํ‹ฐํ‹ฐ์™€ Team์ด๋ผ๋Š” ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋‘ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ,

Member โžก๏ธ Team ์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋Ÿผ ์ด ๋•Œ Member๋ฅผ ํ†ตํ•ด Team์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, Team์„ ํ†ตํ•ด Member๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋‘˜ ์ค‘ ํ•œ ์ชฝ๋งŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ "๋‹จ๋ฐฉํ–ฅ" ๊ด€๊ณ„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

 

์–‘๋ฐฉํ–ฅ์€ Member ↔๏ธ Team ์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์งˆ ๋•Œ, Member๋ฅผ ํ†ตํ•ด Team์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Œ๊ณผ ๋™์‹œ์— Team์„ ํ†ตํ•ด Member๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์–‘์ชฝ ๋ชจ๋‘ ์„œ๋กœ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ "์–‘๋ฐฉํ–ฅ" ๊ด€๊ณ„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


๋‹ค์ค‘์„ฑ[Mutiplicity] 

๋‹ค์ค‘์„ฑ์—๋Š” 1:N, N:1, 1:1, N:M ๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 

  • 1:N, N:1 ๊ด€๊ณ„ : ์˜ˆ๋ฅผ๋“ค์–ด Member์™€ Team์ด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์งˆ ๋•Œ, ์—ฌ๋Ÿฌ ํšŒ์›์€ ํ•œ ํŒ€์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ณ , ๋ฐ˜๋Œ€๋กœ ํ•œ ํŒ€์—๋Š” ์—ฌ๋Ÿฌ ํšŒ์›์ด ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ด€๊ณ„๋ฅผ ์ผ๋Œ€๋‹ค, ๋‹ค๋Œ€์ผ๊ด€๊ณ„๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. 
  • 1:1 ๊ด€๊ณ„ : ์˜ˆ๋ฅผ๋“ค์–ด, Member์™€ PhoneNumber๋ผ๋Š” ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์กด์žฌํ•  ๋•Œ, ๊ฐ๊ฐ์˜ Member๋Š” ๊ฐ๊ฐ์˜ PhoneNumber๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ด€๊ณ„๋ฅผ ์ผ๋Œ€์ผ ๊ด€๊ณ„๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ (N:M)

  • N:M (๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„) : ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„์˜ ์˜ˆ์‹œ ์ค‘ ํ•˜๋‚˜๋Š” "ํ•™์ƒ-๊ณผ๋ชฉ" ๊ด€๊ณ„์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ํ•™์ƒ๋“ค์ด ์—ฌ๋Ÿฌ ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•œ ํ•™์ƒ์ด ์—ฌ๋Ÿฌ ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•œ ๊ณผ๋ชฉ์— ์—ฌ๋Ÿฌ ํ•™์ƒ๋“ค์ด ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ด๋Ÿฌํ•œ ๊ด€๊ณ„๋Š” ์ผ๋Œ€๋‹ค๋‚˜ ๋‹ค๋Œ€์ผ ๊ด€๊ณ„๋กœ ํ‘œํ˜„ํ•˜๊ธฐ์— ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
      • ๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„์— "์ˆ˜๊ฐ•" ๊ฐ™์€ ์—ฐ๊ฒฐ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ๊ฐ์˜ ํ•™์ƒ๊ณผ ๊ณผ๋ชฉ์„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•œ ํ•™์ƒ์ด ์—ฌ๋Ÿฌ ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•œ ๊ณผ๋ชฉ์— ์—ฌ๋Ÿฌ ํ•™์ƒ๋“ค์ด ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

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

๊ฐ์ฒด๋ฅผ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋กœ ๋งŒ๋“ค๋ฉด ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฃผ์ธ์„ ์ •ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

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


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

๋‹จ๋ฐฉํ–ฅ, ๋‹ค๋Œ€์ผ ๊ด€๊ณ„ Member(N) โžก๏ธ Team(1)์˜ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์„ ๋•Œ,

๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”์—์„œ ์–ด๋–ป๊ฒŒ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งบ๋Š”์ง€ ๋ด…์‹œ๋‹ค.

1. ์ˆœ์ˆ˜ํ•œ ๊ฐ์ฒด์—์„œ ์—ฐ๊ด€๊ด€๊ณ„

๊ฐ์ฒด์—์„œ๋Š” ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ๋งบ์Šต๋‹ˆ๋‹ค.

public class Member{
    private Long id;
    private String username;
    private Team team; // ํŒ€์˜ ์ฐธ์กฐ ๋ณด๊ด€
}

public class Team{
    private Long id;
    private String name;
    ...
}

์ด ๋•Œ Member์—์„œ๋Š” Team์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์ง€๋งŒ(member.getTeam()), Team์—์„œ๋Š” Member๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.(team.getMembers() X) 

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

2. ํ…Œ์ด๋ธ”์—์„œ์˜ ์—ฐ๊ด€๊ด€๊ณ„

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

create table Member (
   MEMBER_ID varchar(255) not null,
    username varchar(255),
    TEAM_ID varchar(255),
    primary key (MEMBER_ID)
)
    
create table Team (
   TEAM_ID varchar(255) not null,
    name varchar(255),
    primary key (TEAM_ID)
)
     
alter table Member 
   add constraint FKl7wsny760hjy6x19kqnduasbm 
   foreign key (TEAM_ID) 
   references Team

 

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

Member Join Team์ด ๊ฐ€๋Šฅํ•˜๋“ฏ์ด, Team Join Member๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

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

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


JPA๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐ์ฒด์˜ ์ฐธ์กฐ-ํ…Œ์ด๋ธ”์˜ ์™ธ๋ž˜ํ‚ค๋ฅผ ๋งคํ•‘

Member(N) โžก๏ธ Team(1)

  • ๊ฐ์ฒด ์—ฐ๊ด€๊ด€๊ณ„ : Member ๊ฐ์ฒด์˜ Member.team ํ•„๋“œ ์‚ฌ์šฉ
  • ํ…Œ์ด๋ธ” ์—ฐ๊ด€๊ด€๊ณ„ : MEMBER.TEAM_ID ์™ธ๋ž˜ํ‚ค ์ปฌ๋Ÿผ ์‚ฌ์šฉ

JPA๋ฅผ ์‚ฌ์šฉํ•ด Member.team ๊ณผ MEMBER.TEAM_ID์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•ด๋ด…์‹œ๋‹ค.

 

Member

@Entity
public class Member{

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

    private String username;

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

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

- @ManyToOne : ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๋‹ค๋Œ€์ผ ๊ด€๊ฒŒ๋ผ๋Š” ๋งคํ•‘์ •๋ณด. ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•  ๋•Œ๋Š” ์ด๋ ‡๊ฒŒ ๋‹ค์ค‘์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

- @JoinColumn(name="TEAM_ID") : ์™ธ๋ž˜ ํ‚ค๋ฅผ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ์ž…๋‹ˆ๋‹ค.

@JoinColumn์„ ์ƒ๋žตํ•˜๋ฉด ๊ทœ์น™์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์™ธ๋ž˜ ํ‚ค๋ฅผ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.
ํ•„๋“œ๋ช… + _ + ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๋ช… 
ex) ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ๋žตํ•˜๋ฉด team_TEAM_ID ์™ธ๋ž˜ํ‚ค๋ฅผ ์‚ฌ์šฉ

 

Team

@Entity
public class Team{

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

    private String name;


   	...
}

๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„์—์„œ ์ €์žฅ,์ˆ˜์ •,์‚ญ์ œ,์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ด…์‹œ๋‹ค.

1. ์ €์žฅ

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

// ๋ฉค๋ฒ„1 ์ €์žฅ
Member member1 = new Member("member1","๋ฉค๋ฒ„์ด๋ฆ„1");
member1.setTeam(team1); // ์—ฐ๊ด€๊ด€๊ณ„ ์„ค์ •
em.persist(member1);

Member ์—”ํ‹ฐํ‹ฐ๋Š” Team ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. (member1.setTeam(team1); 

 

์˜ˆ์‹œ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ setter๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•ด์ฃผ์—ˆ์ง€๋งŒ, domain์— getter, setter๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ง€์–‘ํ•ฉ์‹œ๋‹ค. ์‹ค์ „์—์„œ๋Š” DTO(Data Transfer Object)๋ฅผ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค.

 

JPA๋Š” ์ฐธ์กฐํ•œ ํŒ€์˜ ์‹๋ณ„์ž(Team.id)๋ฅผ ์™ธ๋ž˜ ํ‚ค๋กœ ์‚ฌ์šฉํ•ด์„œ ์ ์ ˆํ•œ ๋“ฑ๋ก ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค!

insert into team (team_id, name) values('team1','ํŒ€1')
insert into member (member_id, name, team_id) values('member1','ํšŒ์›1','team1')
1. ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” ํŠธ๋žœ์žญ์…˜์„ ํ†ตํ•ด ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰ ์ค„ ์ฝ”๋“œ em.persist(member1) ๊ฐ€ ๋๋‚˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ commitํ•ฉ๋‹ˆ๋‹ค.

2. ๋‹น์—ฐํ•œ ์–˜๊ธฐ์ง€๋งŒ ์—”ํ‹ฐํ‹ฐ๋ฅผ DB์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์˜์† ์ƒํƒœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. - persist ํ–ˆ์œผ๋ฏ€๋กœ ์˜์†์ƒํƒœ.

2. ์กฐํšŒ

์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.

 

1. ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ( ๊ฐ์ฒด ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์‚ฌ์šฉํ•œ ์กฐํšŒ by getter )

Member member = em.find(Member.class,"member1");
Team team = member.getTeam();

๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

2. JPQL ์‚ฌ์šฉ

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

    tx.begin();

    try {
        String jpql = "select m from Member m join m.team t where t.name =: teamName";

        List<Member> resultList = em.createQuery(jpql, Member.class)
                .setParameter("teamName", "ํŒ€1")
                .getResultList();

        for (Member member : resultList) {
            System.out.println("member.getUsername() = " + member.getUsername());
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        tx.rollback();
    } finally {
        em.close();
    }
    emf.close();
}
์กฐํšŒ๋Š” ํŠธ๋žœ์žญ์…˜์„ ํ†ตํ•ด ์ง„ํ–‰ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

3. ์ˆ˜์ •

์•ž์„œ ๋ฐฐ์šด ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๋ณ€๊ฒฝ ๊ฐ์ง€ ๊ธฐ๋Šฅ์„ ๋˜‘๊ฐ™์ด ์ ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋”ฑํžˆ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค๊ณ  ํ•ด์„œ ์ฐจ์ด์ ์€ ์—†์Šต๋‹ˆ๋‹ค.

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

    tx.begin();

    try {
        Team team2 = new Team("team2", "ํŒ€2");
        em.persist(team2);

        Member member1 = em.find(Member.class, "member1");
        member1.setTeam(team2);
        em.persist(member1);

        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        tx.rollback();
    } finally {
        em.close();
    }
}

4. ์‚ญ์ œ

์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ญ์ œํ•  ๋•Œ์—๋Š” ์ฃผ์˜ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ๊ธฐ์กด์— ์žˆ๋˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋จผ์ € ์ œ๊ฑฐํ•˜๊ณ  ์‚ญ์ œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์™ธ๋ž˜ ํ‚ค ์ œ์•ฝ์กฐ๊ฑด์— ์˜ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
member1.setTeam(null);
em.remove(member1);

 


<์ •๋ฆฌ>

  • ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
  • ๋‹จ๋ฐฉํ–ฅ์—์„œ ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ์—๋Š” ์ฐธ์กฐ ํ•„๋“œ์— ๋ฐ˜๋“œ์‹œ ๋‹ค์ค‘์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์–ด๋…ธํ…Œ์ด์…˜(ex) @ManyToOne)์„ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • + ์ฐธ์กฐ ํ•„๋“œ์˜ @JoinColumn์˜ name์†์„ฑ์—๋Š” ์™ธ๋ž˜ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ปฌ๋Ÿผ๋ช…์„ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถ™ํ˜€์ฃผ์ง€ ์•Š์œผ๋ฉด ๊ทœ์น™์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ ์™ธ๋ž˜ํ‚ค ์ปฌ๋Ÿผ๋ช…๊ณผ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค.
  • ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ์–‘๋ฐฉํ–ฅ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์•Œ์•„๋ณด๊ณ  ๋งคํ•‘ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

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

  •  
 

DB ERD ๊ด€๊ณ„์„ (์‹ค์„ , ์ ์„ )๊ณผ ๊ธฐํ˜ธ์— ๋Œ€ํ•œ ์„ค๋ช…

DBA๋Š” ์•„๋‹ˆ์ง€๋งŒ, ํ”„๋กœ๊ทธ๋žจ์„ ์ž˜ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์™„์„ฑ๋œ ERD๋ฅผ ์ดํ•ดํ•˜๊ณ , ๋‚˜์•„๊ฐ€ ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ๋ง์€ ์ง์ ‘ ๋งŒ๋“ค์ค„ ์•Œ์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ERD๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ์ฒซ ๊ฑธ์Œ์ด์ž ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค ํ•  ์ˆ˜์žˆ๋Š” ๋ถ€๋ถ„

eyecandyzero.tistory.com

 

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

Study Repository

rlaehddnd0422

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