Home > Spring > ๐Ÿƒ[Spring] ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿƒ[Spring] ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?
Spring Framework

๐Ÿƒ[Spring] ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

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

1๏ธโƒฃ ํŠน์ง•.

1๏ธโƒฃ ๋‹จ๋ฐฉํ–ฅ ์ฐธ์กฐ.

  • ํ•œ์ชฝ ์—”ํ‹ฐํ‹ฐ์—์„œ๋งŒ ๋‹ค๋ฅธ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์˜ ํƒ์ƒ‰์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์„ค๊ณ„๊ฐ€ ๊ฐ„๋‹จ.

  • ๊ด€๊ณ„ ๋ฐฉํ–ฅ์ด ๋‹จ์ˆœํ•˜๋ฏ€๋กœ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค.
  • ํ•„์š” ์ด์ƒ์œผ๋กœ ๋ณต์žกํ•œ ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ์™ธ๋ž˜ ํ‚ค(Foregin Key)

  • ์™ธ๋ž˜ ํ‚ค(Foregin Key)๋Š” ๊ด€๊ณ„๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์—”ํ‹ฐํ‹ฐ(์ฐธ์กฐํ•˜๋Š” ์ชฝ)์—๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„์˜ ์ข…๋ฅ˜.

1๏ธโƒฃ @ManyToOne

  • ์ž์‹ ์—”ํ‹ฐํ‹ฐ(N)๊ฐ€ ๋ถ€๋ชจ ์—”ํ‹ฐํ‹ฐ(1)๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„.
  • ์™ธ๋ž˜ ํ‚ค(Foreign Key)๋Š” ์ž์‹ ํ…Œ์ด๋ธ”์— ์กด์žฌ.

๐Ÿ‘‰ ์˜ˆ์ œ

  • Order์™€ Customer ๊ด€๊ณ„ : ์—ฌ๋Ÿฌ ์ฃผ๋ฌธ(Order)์ด ํ•˜๋‚˜์˜ ๊ณ ๊ฐ(Customer)์„ ์ฐธ์กฐ.
    @Entity
    public class Order {
        
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
        
      @ManyToOne
      @JoinColumn(name = "customer_id") // ์™ธ๋ž˜ ํ‚ค
      private Customer customer;
        
      // ๊ธฐํƒ€ ํ•„๋“œ ๋ฐ ๋ฉ”์„œ๋“œ
    }
    
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ Order ํ…Œ์ด๋ธ”์€ customer_id ์ปฌ๋Ÿผ์„ ์™ธ๋ž˜ ํ‚ค(Foreign Key)๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

2๏ธโƒฃ @OneToMany

  • ๋ถ€๋ชจ ์—”ํ‹ฐํ‹ฐ(1)๊ฐ€ ์ž์‹ ์—”ํ‹ฐํ‹ฐ(N)์˜ ์ปฌ๋ ‰์…˜์„ ์ฐธ์กฐํ•˜๋Š” ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„.
  • ์™ธ๋ž˜ ํ‚ค(Foreign Key)๋Š” ์ž์‹ ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋ฉฐ, ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์—๋Š” ์ปฌ๋ ‰์…˜๋งŒ ๊ด€๋ฆฌ.

๐Ÿ‘‰ ์˜ˆ์ œ

  • Customer์™€ Order ๊ด€๊ณ„ : ํ•œ ๊ณ ๊ฐ(Customer)์ด ์—ฌ๋Ÿฌ ์ฃผ๋ฌธ(Order)์„ ๊ฐ€์ง.
    @Entity
    public class Customer {
        
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
        
      @OneToMany
      @JoinColumn(name = "customer_id") // ์™ธ๋ž˜ ํ‚ค๋ฅผ ์ž์‹ ํ…Œ์ด๋ธ”์— ์„ค์ •
      private List<Order> orders = new ArrayList<>();
        
      // ๊ธฐํƒ€ ํ•„๋“œ ๋ฐ ๋ฉ”์„œ๋“œ.
    }
    
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ๋Š” ์™ธ๋ž˜ ํ‚ค๊ฐ€ ์—ฌ์ „ํžˆ Order ํ…Œ์ด๋ธ”์— ์กด์žฌ.

3๏ธโƒฃ @OneToOne

  • ํ•œ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ๋‹ค๋ฅธ ์—”ํ‹ฐํ‹ฐ์™€ 1:1๋กœ ๋งคํ•‘๋˜๋Š” ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„.
  • ์™ธ๋ž˜ ํ‚ค๋Š” ์ฐธ์กฐํ•˜๋Š” ์ชฝ์—๋งŒ ์กด์žฌ.

๐Ÿ‘‰ ์˜ˆ์ œ.

  • Passport์™€ Person ๊ด€๊ณ„ : ํ•œ ์‚ฌ๋žŒ(Person)์ด ํ•˜๋‚˜์˜ ์—ฌ๊ถŒ(Passport)์„ ๊ฐ€์ง.
    @Entity
    public class Passport {
        
      @Id
      @GeneratedValue(stratege = GenerationType.IDENTITY)
      private Long id;
        
      @OneToOne
      @JoinColumn(name = "person_id") // ์™ธ๋ž˜ ํ‚ค
      private Person person;
        
      // ๊ธฐํƒ€ ํ•„๋“œ ๋ฐ ๋ฉ”์„œ๋“œ
    }
    

4๏ธโƒฃ @ManyToMany

  • ๋‘ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋Š” ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„.
  • ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”์„ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜๋ฉฐ, ํ•œ์ชฝ์—์„œ๋งŒ ๋‹ค๋ฅธ ์ชฝ์„ ์ฐธ์กฐ.

๐Ÿ‘‰ ์˜ˆ์ œ.

  • Student์™€ Course ๊ด€๊ณ„ : ํ•œ ํ•™์ƒ(Student)์ด ์—ฌ๋Ÿฌ ์ˆ˜์—…(Course)์„ ๋“ฃ๋Š” ๊ฒฝ์šฐ.
    @Entity
    public class Student {
        
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
        
      @ManyToMany
      @JoinTable(
          name = "student_course",
          joinColumns = @JoinColumn(name = "student_id"),
          inverseJoinColumns = @JoinColumn(name = "courser_id")
      )
      private List<Course> courses = new ArrayList<>();
        
      // ๊ธฐํƒ€ ํ•„๋“œ ๋ฐ ๋ฉ”์„œ๋“œ
    }
    

3๏ธโƒฃ ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„์˜ ์žฅ๋‹จ์ .

1๏ธโƒฃ ์žฅ์ .

1๏ธโƒฃ ์„ค๊ณ„ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๊ฐ„๋‹จ.

  • ๊ด€๊ณ„๊ฐ€ ํ•œ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์„ค์ •๋˜๋ฏ€๋กœ ๋ณต์žก๋„๊ฐ€ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์„ฑ๋Šฅ ์ตœ์ ํ™”.

  • ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ณด๋‹ค ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ์ด๋‚˜ ๊ด€๋ฆฌ ๋น„์šฉ์ด ๋‚ฎ์Šต๋‹ˆ๋‹ค.
  • ๋ถˆํ•„์š”ํ•œ ์—ฐ๊ด€ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ ์˜์กด์„ฑ ๊ฐ์†Œ.

  • ๋‘ ์—”ํ‹ฐํ‹ฐ ๊ฐ„์˜ ์˜์กด์„ฑ์ด ๋‚ฎ์•„์ ธ ์„ค๊ณ„๊ฐ€ ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ๋‹จ์ .

1๏ธโƒฃ ํƒ์ƒ‰ ๋ฐฉํ–ฅ ์ œํ•œ.

  • ๊ด€๊ณ„์˜ ๋ฐฉํ–ฅ์ด ํ•œ์ชฝ์œผ๋กœ๋งŒ ์„ค์ •๋˜๋ฏ€๋กœ, ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋ ค๋ฉด ๋ณ„๋„์˜ ์ฟผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ: Order์—์„œ Customer๋Š” ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, Customer์—์„œ Order๋Š” ์ฐธ์กฐ ๋ถˆ๊ฐ€๋Šฅ.

2๏ธโƒฃ ์ถ”๊ฐ€ ์š”๊ตฌ์‚ฌํ•ญ์— ๋Œ€ํ•œ ํ™•์žฅ์„ฑ ์ œํ•œ.

  • ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์ถ”๊ฐ€์ ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4๏ธโƒฃ ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„ ์‚ฌ์šฉ ์‹œ์ .

1๏ธโƒฃ ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ ๋ฐฉํ–ฅ์ด ํ•œ์ชฝ์œผ๋กœ๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ.

  • ์˜ˆ: ์ž์‹ -> ๋ถ€๋ชจ(์ฃผ๋ฌธ์—์„œ ๊ณ ๊ฐ์„ ์ฐธ์กฐ)

2๏ธโƒฃ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๊ฐ€ ๋ถˆํ•„์š”ํ•œ ๊ฒฝ์šฐ.

  • ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ ํƒ์ƒ‰์ด ์š”๊ตฌ๋˜์ง€ ์•Š์œผ๋ฉฐ, ๊ฐ„๋‹จํ•œ ์„ค๊ณ„๋ฅผ ์›ํ•  ๋•Œ.

3๏ธโƒฃ ์„ฑ๋Šฅ ์ตœ์ ํ™”๊ฐ€ ์ค‘์š”ํ•œ ๊ฒฝ์šฐ,

  • ๋ถˆํ•„์š”ํ•œ ์—ฐ๊ด€ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ์ด๋‚˜ ๋ณต์žกํ•œ ๊ด€๊ณ„๋ฅผ ์ค„์ด๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ.

5๏ธโƒฃ ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„์™€ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„ ๋น„๊ต.

ํŠน์ง• ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„
์ฐธ์กฐ ๋ฐฉํ–ฅ ํ•œ์ชฝ์—์„œ๋งŒ ์ฐธ์กฐ ๊ฐ€๋Šฅ ์–‘์ชฝ์—์„œ ์ฐธ์กฐ ๊ฐ€๋Šฅ
์„ค๊ณ„ ๋ณต์žก์„ฑ ๊ฐ„๋‹จ ๋ณต์žก
์‚ฌ์šฉ ์‚ฌ๋ก€ ๋‹จ์ˆœํ•œ ๊ด€๊ณ„์—์„œ ์‚ฌ์šฉ ๋ถ€๋ชจ์™€ ์ž์‹ ๋ชจ๋‘ ํƒ์ƒ‰์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
์™ธ๋ž˜ ํ‚ค ๊ด€๋ฆฌ ์™ธ๋ž˜ ํ‚ค๋Š” ๊ด€๊ณ„ ์„ค์ •๋œ ์ชฝ์—์„œ๋งŒ ๊ด€๋ฆฌ ์™ธ๋ž˜ ํ‚ค๋Š” ๊ด€๊ณ„ ์ฃผ์ธ์ด ๊ด€๋ฆฌ
์„ฑ๋Šฅ ๋‚ฎ์€ ์—ฐ์‚ฐ ๋น„์šฉ ์ถ”๊ฐ€ ์ฟผ๋ฆฌ ๋ฐ ์—ฐ๊ด€ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ๋น„์šฉ ์ฆ๊ฐ€

6๏ธโƒฃ ๊ฒฐ๋ก .

  • ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋Š” ์„ค๊ณ„๊ฐ€ ๊ฐ„๋‹จํ•˜๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฌ์šฐ๋ฉฐ, ํƒ์ƒ‰ ๋ฐฉํ–ฅ์ด ํ•œ์ชฝ์œผ๋กœ๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • JPA์—์„œ๋Š” @ManyToOne, @OneToMany, @OneToOne, @ManyToMany ์–ด๋…ธํ…Œ์ด์…˜์„ ํ™œ์šฉํ•˜์—ฌ ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ณด๋‹ค ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ดํ•˜๋ฏ€๋กœ, ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.