Spring

TIL Spring #3-3

๋ฆฐ์˜ˆ์กฐ 2023. 12. 18. 13:42

 

Entity ์—ฐ๊ด€๊ด€๊ณ„

 

  • 1:1
  • N:1 
  • 1:N
  • N:M

 


 

 

 

์‹ค์ œ๋กœ ํ…Œ์ด๋ธ”์„ ๋”ฑ ํ•œ๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†๋‹ค.

DB table์˜ ์—ฐ๊ด€๊ด€๊ณ„ Entity ๊ฐ์ฒด์˜ ์—ฐ๊ด€๊ด€๊ณ„ ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ? ์žˆ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅผ๊นŒ?

 

์˜ˆ๋ฅผ ๋“ค์–ด, ๊ณ ๊ฐ์ด ์Œ์‹์„ ์ฃผ๋ฌธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

๊ทธ๋ ‡๋‹ค๋ฉด ๋‹น์—ฐํžˆ ์ฃผ๋ฌธํ•˜๋Š” ๊ณ ๊ฐ ํ…Œ์ด๋ธ” ํ•˜๋‚˜, ์Œ์‹ ํ…Œ์ด๋ธ” ํ•˜๋‚˜ ์ด ๋‘๊ฐœ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๊ณ ๊ฐ์ด ์Œ์‹์„ ์ฃผ๋ฌธํ–ˆ์„ ๋•Œ ํ•ด๋‹น ์ฃผ๋ฌธ๊ฑด์— ๋Œ€ํ•œ๊ฑด ์œ ์ €? ์Œ์‹? ์–ด๋Š ํ…Œ์ด๋ธ”์— ๋ช…์‹œํ•ด์ค˜์•ผํ• ๊นŒ?

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

 

 

์ด๋ ‡๊ฒŒ user_id ์ปฌ๋Ÿผ์— 1๋ฒˆ 2๋ฒˆ ์œ ์ €๊ฐ€ ์ฃผ๋ฌธํ–ˆ๋‹ค. ๋ผ๋Š” ํ‘œํ˜„ ๋ฐฉ์‹๋„ ์•ˆ๋ ๊นŒ?

์ผ๋‹จ RDBMS ์ƒ์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ผ๊ณ  ์ดํ•ดํ•˜์ž.

 

 

- 2 row ๋กœ ํ‘œํ˜„์ด ๋˜๊ธด ํ•œ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ ์Œ์‹์˜ ์ด๋ฆ„์ด ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ค‘๋ณต๋œ๋‹ค!

 

 

 

<ํ•ด๊ฒฐ๋ฐฉ์•ˆ>

์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋„ฃ์„ ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”์„ ๋”ฐ๋กœ ๋งŒ๋“ค์ž!

 

 

์ •๋ฆฌํ•ด๋ณด๋ฉด ๊ณ ๊ฐ๋„ ์Œ์‹ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๊ณ  ์Œ์‹ ๋˜ํ•œ ์—ฌ๋Ÿฌ ๊ณ ๊ฐ์—๊ฒŒ ์ฃผ๋ฌธ๋  ์ˆ˜ ์žˆ๋‹ค. -> N:M ๊ด€๊ณ„!

N:M ๊ด€๊ณ„์ธ ํ…Œ์ด๋ธ”์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

<ํ…Œ์ด๋ธ”์˜ ๋ฐฉํ–ฅ?>

- DB ํ…Œ์ด๋ธ” ๊ฐ„ ๋ฐฉํ–ฅ์ด ์žˆ์„๊นŒ? 

- ํ…Œ์ด๋ธ”์—์„œ๋Š” ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ๊ณ ๊ฐ, ์Œ์‹ ์–ด๋”œ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋“  JOIN์„ ์‚ฌ์šฉํ•ด ์กฐํšŒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ

๋‘˜์˜ ๊ฒฐ๊ณผ๋Š” ์˜ค์ฐจ์—†์ด ๋˜‘๊ฐ™๋‹ค. -> ๋ฐฉํ–ฅ์˜ ๊ฐœ๋… ์—†์Œ 

 

 


๊ทธ๋ ‡๋‹ค๋ฉด JPA Entity์—์„œ๋Š”?

 

๊ฒฐ๋ก ์ ์œผ๋กœ Entity์—์„œ๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๊ฒƒ์ด DB์— ์ ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ 

๊ฐ์ฒด ํ˜•ํƒœ์—์„œ๋Š” ์ฐธ์กฐ๋ฅผ ๋‹จ๋ฐฉํ–ฅ ์–‘๋ฐฉํ–ฅ ๋‘๊ฐ€์ง€ ๋ชจ๋‘ ๋‹ค ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์ฐธ๊ณ ) ์™ธ๋ž˜ ํ‚ค ์ฃผ์ธ๋งŒ์ด ์™ธ๋ž˜ ํ‚ค๋ฅผ ๋“ฑ๋ก, ์ˆ˜์ •, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ์€ ์˜ค์ง ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

@JoinColumn ์• ๋„ˆํ…Œ์ด์…˜์œผ๋กœ ์ฃผ์ธ ์ง€์ •

 

์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น case๋ณ„๋กœ ํ™•์ธํ•ด๋ณด์ž. (์ผ๊ด€์„ฑ์„ ์œ„ํ•ด ์Œ์‹ Entityd์—๋งŒ ์ฃผ์ธ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค.)

 

 

 

<1:1>

 

# 1:1 ๊ด€๊ณ„ - ๋‹จ๋ฐฉํ–ฅ

@Entity
@Table(name = "food")
public class Food {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;
}

 

- ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๋‹ค ์ฃผ์ธ์ธ ์—”ํ‹ฐํ‹ฐ์— 1:1 ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” @OneToOne ์• ๋„ˆํ…Œ์ด์…˜ ์ž‘์„ฑ, ์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์ž„์„ ์•Œ๋ ค์ฃผ๋Š” @JoinColumn ์• ๋„ˆํ…Œ์ด์…˜ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

# 1:1 ๊ด€๊ณ„ - ์–‘๋ฐฉํ–ฅ

-์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ

@Entity
@Table(name = "food")
public class Food {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;
}

 

- ์™ธ๋ž˜ํ‚ค ์ฃผ์ธ ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @OneToOne(mappedBy = "user")
    private Food food;
}

 

- ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„์—์„œ๋Š” ์ฃผ์ธ์ด ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ์— ํ•˜๋‚˜ ์„ค์ •ํ•  ๊ฒƒ์ด ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ mappedBy ์˜ต์…˜์ด๋‹ค.

 ์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์„ ์ง€์ •ํ•ด์ค„๋•Œ ํ•ด๋‹น ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์ง€๊ธˆ ๋ณด์ด๋Š” "user"๋ผ๋Š” ์†์„ฑ๊ฐ’์€ User ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ

 ์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์ธ Food ์—”ํ‹ฐํ‹ฐ์˜ ํ•„๋“œ๋ช…์„ ์˜๋ฏธํ•œ๋‹ค. (ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ฒŒ ์ฃผ์˜) 

 

- ๋””ํดํŠธ ์˜ต์…˜ ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๊ธดํ•˜๋‚˜ ๋ชจํ˜ธํ•œ ๊ฒฝ์šฐ JPA์—์„œ ์ค‘๊ฐ„ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ๋ช…์‹œํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

<N:1>

 

# N:1 ๊ด€๊ณ„ - ๋‹จ๋ฐฉํ–ฅ

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

 ๋Œ€์‹  OneToOne ๋Œ€์‹  @ManyToOne ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

# N:1 ๊ด€๊ณ„ - ์–‘๋ฐฉํ–ฅ

- ์ด ๊ฒฝ์šฐ๋„ 1:1 ์–‘๋ฐฉํ–ฅ ๊ฒฝ์šฐ์™€ ๋น„์Šทํ•˜๋‹ค!

- ๋‹ค๋งŒ, ์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์ด ์•„๋‹Œ ํด๋ž˜์Šค์— @OneToMany ๋ผ๋Š” ์• ๋„ˆํ…Œ์ด์…˜ ์ž‘์„ฑ ํ•„์š”.

@Entity
@Table(name = "food")
public class Food {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    @ManyToOne //N:1 ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์• ๋„ˆํ…Œ์ด์…˜
    @JoinColumn(name = "user_id") //์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์„ ์ง€์ •ํ•˜๋Š” ์• ๋„ˆํ…Œ์ด์…˜
    private User user;
}

 

 

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @OneToMany(mappedBy = "user") //์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์„ ์•Œ๋ ค์คŒ.
    								//User ํด๋ž˜์Šค ์•„๋‹ˆ๊ณ 
                                    //์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์ธ Food ํด๋ž˜์Šค์˜ ํ•„๋“œ์ธ user์ž„!
    private List<Food> foodList = new ArrayList<>();
}

 

- ํ•˜๋‚˜ ์•Œ์•„ ๋‘˜ ๊ฒƒ์€ ์–‘๋ฐฉํ–ฅ ์ฐธ์กฐ๋ฅผ ์œ„ํ•ด ๊ณ ๊ฐ ์—”ํ‹ฐํ‹ฐ์—์„œ ์ž๋ฐ” ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์Œ์‹ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค! (์Œ์‹์„ ํ•˜๋‚˜๋งŒ ์ฃผ๋ฌธํ•  ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ์— ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๋‹ด์•„์•ผํ•œ๋‹ค.)

 

<1:N>

 

@OneToMany ์• ๋„ˆํ…Œ์ด์…˜ ์‚ฌ์šฉ

์œ„์—์„œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์™ธ๋ž˜ํ‚ค ์ฃผ์ธ์„ ์Œ์‹์œผ๋กœ ๋‘์ž.

๊ทธ๋Ÿฐ๋ฐ 1:N ๊ด€๊ณ„์—์„œ N์˜ ํ…Œ์ด๋ธ”์ด ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ์™ธ๋ž˜ํ‚ค๋Š” ๊ณ ๊ฐ์— ๋‘”๋‹ค.

 

-๋‹จ๋ฐฉํ–ฅ

@Entity
@Table(name = "food")
public class Food {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    @OneToMany
    @JoinColumn(name = "food_id") // users ํ…Œ์ด๋ธ”์— food_id ์ปฌ๋Ÿผ
    private List<User> userList = new ArrayList<>();
}

 

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
}

 

N ๊ด€๊ณ„์ธ User์— ์ž ์‹œ ์™ธ๋ž˜ํ‚ค๋ฅผ ๋ณด๊ด€ํ•˜๊ณ  ์Œ์‹์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ์ฐฝ๊ณ ๊ฐ™์€ ๋Š๋‚Œ..

 

- ์–‘๋ฐฉํ–ฅ 

ํ•ด๋‹น ๊ด€๊ณ„์—์„œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

mappedBy ์†์„ฑ ๋˜ํ•œ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

@JoinColumn์— insert/update์— false ๊ฐ’์„ ์ฃผ์–ด ์–‘๋ฐฉํ–ฅ์ฒ˜๋Ÿผ ์„ค์ •ํ•  ์ˆ˜๋Š” ์žˆ๋‹ค. 

 

 

 

<N:M>

@ManyToMany ์• ๋„ˆํ…Œ์ด์…˜ ์‚ฌ์šฉ

 

๊ธ€ ์•ž๋จธ๋ฆฌ์—์„œ ๋ดค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ํ•ด๋‹น ๊ด€๊ณ„์—์„œ๋Š” ์ค‘๊ฐ„ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•œ๋‹ค. (orders)

 

- ๋‹จ๋ฐฉํ–ฅ

@Entity
@Table(name = "food")
public class Food {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    @ManyToMany
    @JoinTable(name = "orders")// ์ค‘๊ฐ„ ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    joinColumns = @JoinColumn(name = "food_id"), // ํ˜„์žฌ ์œ„์น˜์ธ Food Entity ์—์„œ ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”๋กœ ์กฐ์ธํ•  ์ปฌ๋Ÿผ ์„ค์ •
    inverseJoinColumns = @JoinColumn(name = "user_id")) // ๋ฐ˜๋Œ€ ์œ„์น˜์ธ User Entity ์—์„œ ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”๋กœ ์กฐ์ธํ•  ์ปฌ๋Ÿผ ์„ค์ •
    private List<User> userList = new ArrayList<>();
}

 

- ํ•ด๋‹น orders ํ…Œ์ด๋ธ”์€ JPA์— ์˜ํ•ด ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. PK ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. 

 

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
}

 

 

- ์–‘๋ฐฉํ–ฅ

 

- ์™ธ๋ž˜ํ‚ค ์ฃผ์ธ์ด ์•„๋‹Œ ๊ณ ๊ฐ์˜ ์ฝ”๋“œ๋งŒ ๋ณ€๊ฒฝ๋œ๋‹ค.

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @ManyToMany(mappedBy = "userList")
    private List<Food> foodList = new ArrayList<>();
}

 

# N:M ์–‘๋ฐฉํ–ฅ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

@Test
@Rollback(value = false)
@DisplayName("N๋Œ€M ์–‘๋ฐฉํ–ฅ ํ…Œ์ŠคํŠธ : ๊ฐ์ฒด์™€ ์–‘๋ฐฉํ–ฅ์˜ ์žฅ์  ํ™œ์šฉ")
void test5() {

    User user = new User();
    user.setName("Robbie");

    User user2 = new User();
    user2.setName("Robbert");


    // addUserList() ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•ด user ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ 
    // ํ•ด๋‹น ๋ฉ”์„œ๋“œ์— ๊ฐ์ฒด ํ™œ์šฉ์„ ์œ„ํ•ด user ๊ฐ์ฒด์— food ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. user.getFoodList().add(this);
    Food food = new Food();
    food.setName("์•„๋ณด์นด๋„ ํ”ผ์ž");
    food.setPrice(50000);
    food.addUserList(user);
    food.addUserList(user2);

    Food food2 = new Food();
    food2.setName("๊ณ ๊ตฌ๋งˆ ํ”ผ์ž");
    food2.setPrice(30000);
    food2.addUserList(user);


    userRepository.save(user);
    userRepository.save(user2);
    foodRepository.save(food);
    foodRepository.save(food2);

    // User ๋ฅผ ํ†ตํ•ด food ์˜ ์ •๋ณด ์กฐํšŒ
    System.out.println("user.getName() = " + user.getName());

    List<Food> foodList = user.getFoodList();
    for (Food f : foodList) {
        System.out.println("f.getName() = " + f.getName());
        System.out.println("f.getPrice() = " + f.getPrice());
    }
}

 

- food.addUserList ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๋‹ค ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ์™ธ๋ž˜ํ‚ค๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

 

<์ค‘๊ฐ„ํ…Œ์ด๋ธ”์„ ์ง์ ‘ ์ƒ์„ฑํ•˜์—ฌ ์ž‘์„ฑํ•ด๋ณด๊ธฐ>

- order ํ…Œ์ด๋ธ”์„ ์ง์ ‘ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. -> ๋ณ€๊ฒฝ๋ฐœ์ƒ์‹œ ์ปจํŠธ๋กคํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ํ™•์žฅ์„ฑ์— ์šฉ์ดํ•˜๋‹ค.

@Entity
@Table(name = "orders")
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "food_id")
    private Food food;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
}

 

- ์ด์ œ ์™ธ๋ž˜ํ‚ค์˜ ์ฃผ์ธ์€ ์Œ์‹, ๊ณ ๊ฐ์ด ์•„๋‹Œ ์ฃผ๋ฌธ์ด ๋œ๋‹ค.

'Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL Spring #3-2  (0) 2023.12.15
TIL Spring #3-1  (0) 2023.12.14
TIL Spring #2-5  (0) 2023.12.13
TIL Spring #2-4  (0) 2023.12.12
TIL Spring #2-3  (0) 2023.12.12