Home > Spring > ๐Ÿƒ[Spring] JPA์˜ ์–ด๋…ธํ…Œ์ด์…˜ - `@JoinColumn`

๐Ÿƒ[Spring] JPA์˜ ์–ด๋…ธํ…Œ์ด์…˜ - `@JoinColumn`
Spring Framework

๐Ÿƒ[Spring] JPA์˜ ์–ด๋…ธํ…Œ์ด์…˜ - @JoinColumn

  • @JoinColumn์€ JPA์—์„œ ๋‘ ์—”ํ‹ฐํ‹ฐ ๊ฐ„์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•  ๋•Œ ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • @JoinColumn์€ ๊ด€๊ณ„๊ฐ€ ์„ค์ •๋˜๋Š” ํ…Œ์ด๋ธ”์— ์™ธ๋ž˜ ํ‚ค(Foreign Key)๋ฅผ ์ •์˜ํ•˜๊ณ , ํ•ด๋‹น ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์ด ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

1๏ธโƒฃ @JoinColumn์˜ ์ฃผ์š” ์†์„ฑ.

  • name
    • ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
      • ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฐธ์กฐํ•˜๋Š” ์—”ํ‹ฐํ‹ฐ์˜ ํ•„๋“œ๋ช… ๋˜๋Š” ํ”„๋กœํผํ‹ฐ๋ช… + _id ํ˜•์‹์œผ๋กœ ์ด๋ฆ„์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค.
  • referencedColumnName
    • ์™ธ๋ž˜ ํ‚ค(Foreign Key)๊ฐ€ ์ฐธ์กฐํ•  ๋Œ€์ƒ ์—”ํ‹ฐํ‹ฐ์˜ ์ปฌ๋Ÿผ๋ช…์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
      • ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฐธ์กฐ ์—”ํ‹ฐํ‹ฐ์˜ ๊ธฐ๋ณธ ํ‚ค๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • nullable
    • ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์ด NULL์„ ํ—ˆ์šฉํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
      • ๊ธฐ๋ณธ๊ฐ’์€ true์ด๋ฉฐ, false๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฐ˜๋“œ์‹œ ๊ฐ’์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • unique
    • ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์ด ์œ ์ผํ•œ ๊ฐ’์ธ์ง€ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
      • ๊ธฐ๋ณธ๊ฐ’์€ false์ž…๋‹ˆ๋‹ค.
  • insertable, updatable
    • ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์˜ ๊ฐ’์ด ์‚ฝ์ž…/์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅํ•œ์ง€ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ @JoinColumn ์˜ˆ์‹œ.

  • ํŒ€(Team)๊ณผ ํšŒ์›(User)๊ฐ„์˜ ๋‹ค๋Œ€์ผ(N:1) ๊ด€๊ณ„์—์„œ User ์—”ํ‹ฐํ‹ฐ์˜ team ํ•„๋“œ๋ฅผ ํ†ตํ•ด Team ์—”ํ‹ฐํ‹ฐ์™€์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋ฉฐ, @JoinColumn์„ ์‚ฌ์šฉํ•ด ์™ธ๋ž˜ ํ‚ค(Foreign Key)๋ฅผ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ```java
    @Entity
    public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne
    @JoinColumn(name = โ€œteam_idโ€) // ์™ธ๋ž˜ ํ‚ค ์ปฌ๋Ÿผ ์„ค์ •
    private Team team;

    // getter, setter
    }

@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

// getter, setter } ```

๐Ÿ‘‰ ์„ค๋ช….

  • User ์—”ํ‹ฐํ‹ฐ์—์„œ team ํ•„๋“œ๋Š” @ManyToOne ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด Team ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
  • @JoinColumn(name = "team_id")๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ User ํ…Œ์ด๋ธ”์— team_id๋ผ๋Š” ์™ธ๋ž˜ ํ‚ค(Foreign Key) ์ปฌ๋Ÿผ์ด ์ƒ์„ฑ๋˜๋„๋ก ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ์ด ์ปฌ๋Ÿผ์€ Team ์—”ํ‹ฐํ‹ฐ์˜ ๊ธฐ๋ณธ ํ‚ค id๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ @JoinColumn์„ ์‚ฌ์šฉํ•œ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„ ์˜ˆ์‹œ.

  • @JoinColumn์€ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„์—์„œ๋„ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, User์™€ Team ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์„œ๋กœ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      ```java
      @Entity
      public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;

    private String name;

    @ManyToOne
    @JoinColumn(name = โ€œteam_idโ€)
    private Team team;
    }

@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

@OneToMany(mappedBy = "team") // ์—ฐ๊ด€๊ด€๊ณ„ ์ฃผ์ธ์ด ์•„๋‹˜์„ ๋ช…์‹œ
private List<User> users = new ArrayList<>();

// getter, setter } ```

๐Ÿ‘‰ ์„ค๋ช….

  • User ์—”ํ‹ฐํ‹ฐ๋Š” team_id ์ปฌ๋Ÿผ์„ ํ†ตํ•ด Team ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ฒฐ๋˜๋ฉฐ, ์ด ์™ธ๋ž˜ ํ‚ค(Foreign Key)๊ฐ€ ๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ๋ฉ๋‹ˆ๋‹ค.
  • Team ์—”ํ‹ฐํ‹ฐ๋Š” users ํ•„๋“œ๋ฅผ ํ†ตํ•ด User ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋ฉฐ, mappedBy ์†์„ฑ์„ ํ†ตํ•ด ์—ฐ๊ด€๊ด€๊ณ„ ์ฃผ์ธ์ด ์•„๋‹˜์„ ๋ช…์‹œํ–ˆ์Šต๋‹ˆ๋‹ค.

4๏ธโƒฃ ์š”์•ฝ.

  • @JoinColumn์€ ๋‘ ์—”ํ‹ฐํ‹ฐ ๊ฐ„์˜ ๊ด€๊ณ„์—์„œ ์™ธ๋ž˜ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ, ์™ธ๋ž˜ ํ‚ค ์ปฌ๋Ÿผ์˜ ์ด๋ฆ„๊ณผ ์†์„ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‹จ๋ฐฉํ–ฅ, ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋ช…ํ™•ํ•œ ์™ธ๋ž˜ ํ‚ค ์„ค์ •์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • name. nullable, unique ๋“ฑ ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ์„ค์ •ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปฌ๋Ÿผ์˜ ์ œ์•ฝ ์กฐ๊ฑด์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.