์• ์ •์ฝ”๋”ฉ ๐Ÿ’ป

Project/DARLING 2022.01.23 ๋Œ“๊ธ€ 0๊ฐœ Joana

3. ์—ฐ๊ด€๊ด€๊ณ„ - ManyToOne

Coupon Entity

package so.ego.re_darling.domains.coupon.domain;

import lombok.*;
import so.ego.re_darling.domains.user.domain.User;

import javax.persistence.*;
import java.time.LocalDateTime;

@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "coupon")
@Entity
public class Coupon {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NonNull private String title;
  private String content;
  private LocalDateTime useDate;

  @Enumerated(value = EnumType.STRING)
  private CouponStatus status;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "receiver_id")
  private User receiver;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "sender_id")
  private User sender;

  @Builder
  public Coupon(@NonNull String title, String content, LocalDateTime useDate, CouponStatus status) {
    this.title = title;
    this.content = content;
    this.useDate = useDate;
    this.status = status;
  }
}

 

@ManyToOne

์˜ˆ๋ฅผ๋“ค์–ด Coupon ์ž…์žฅ์—์„œ receriver User๋Š” 1๋ช…๋ฟ์ด๊ณ  ๊ทธ ํ•œ๋ช…์ด ์—ฌ๋Ÿฌ๊ฐœ์˜ Coupon์„ ์†Œ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” Coupon์ด Many ์ด๊ธฐ ๋•Œ๋ฌธ์—  User ์™€ Coupon ์˜ ์—ฐ๊ด€๊ด€๊ณ„๋Š” @ManyToOne ์ด๋‹ค.

๋‹จ๋ฐ˜ํ–ฅ์œผ๋กœ ์„ค์ •ํ•œ ์ด์œ ๋Š” User๋ฅผ ์กฐํšŒํ•˜๋ฉด์„œ Coupon ๊นŒ์ง€ ์กฐํšŒํ•  ๋ชฉ์ ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ฐ˜๋Œ€๋กœ Coupon์„ ์กฐํšŒํ•  ๋•Œ๋Š” ๋ฐ›๋Š”์‚ฌ๋žŒ์ธ User์˜ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— User๊นŒ์ง€ ์กฐํšŒํ•  ๋ชฉ์ ์ด ์žˆ๋‹ค.

 

๋”๋ณด๊ธฐ
๋”๋ณด๊ธฐ

fetch = FetchType.LAZY *( ๋‚˜์ค‘์— ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๊ธฐ )*

@ManyToOne ์˜ ๊ธฐ๋ณธ fetch ์ „๋žต์€ EAGER์ „๋žต์ด๋‹ค(์ฆ‰์‹œ๋กœ๋”ฉ) EAGER ์€ ํ•ด๋‹น Entity๋ฅผ ์กฐํšŒํ•˜๋ฉด ์—ฐ๊ด€๋œ Entity๊นŒ์ง€ ๋ชจ๋‘ ์กฐํšŒํ•œ๋‹ค. 

๋‹จ์ผ ์ •๋ณด๋ฉด ์กฐํšŒํ•˜๋Š”๋ฐ๋„ ์—ฐ๊ด€๋œ Entity๊นŒ์ง€ ์กฐํšŒํ•˜๋ฉฐ ์ถ”๊ฐ€ ์ฟผ๋ฆฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด N+1 ๋ฌธ์ œ์ธ๋ฐ ์ด ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด LAZY๋ฅผ ์„ค์ •ํ–ˆ๋‹ค. 

 

ํ…Œ์ŠคํŠธ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด๋ดค์œผ๋‚˜ EAGER, LAZY ๋‘˜๋‹ค ์•„๋ž˜์™€๊ฐ™์ด ๋˜‘๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. 

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป๋œ๊ฑด์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค

package so.ego.re_darling.domains.coupon.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.transaction.annotation.Transactional;
import so.ego.re_darling.domains.user.domain.User;
import so.ego.re_darling.domains.user.domain.UserRepository;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
@Transactional
class CouponTest {
  @Autowired CouponRepository couponRepository;
  @Autowired UserRepository userRepository;

  @Test
  @DisplayName("LAZY์ „๋žต ์‹คํ—˜")
  void lazyTest() {
    User a = userRepository.save(User.builder().socialToken("a").build());
    User b = userRepository.save(User.builder().socialToken("b").build());
    couponRepository.save(Coupon.builder().receiver(a).sender(b).title("non-null").build());
    couponRepository.save(Coupon.builder().receiver(b).sender(a).title("non-null").build());
    System.out.println("=====Find ALL=====");
    List<Coupon> couponList = couponRepository.findAll();
    System.out.println("==========");
    for (Coupon c : couponList) {
      System.out.println(c.getReceiver().getSocialToken());
    }
  }
}

 

 

 

cascade = CascadeType.ALL

@OneToMany, @ManyToOne ์— ๊ฑธ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ์ปฌ๋Ÿผ์ด ์‚ญ์ œ๋˜๋ฉด ๊ฐ™์ด ์‚ญ์ œ๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•œ๊ฒƒ์ด๋‹ค. 

PERSIST,REMOVE ๋“ฑ ์˜ต์…˜์ด ์žˆ์ง€๋งŒ ๋ณดํ†ต ALL์„ ์ฃผ๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.

 

์ˆ˜์ • 22.02.17

-> @ManyToOne์— ๊ฑธ๋ฉด ๋ง์ด ์•ˆ๋˜๋Š” ๋ถ€๋ถ„์ด ์ปคํ”Œํ…Œ์ด๋ธ” - ๋‹ค์ด์–ด๋ฆฌ ํ…Œ์ด๋ธ”์ด ์žˆ์„ ๊ฒฝ์šฐ ๋‹ค์ด์–ด๋ฆฌ ํ…Œ์ด๋ธ”์—์„œ cascade๋ฅผ ๊ฑธ์–ด์ฃผ๋ฉด

๋‹ค์ด์–ด๋ฆฌ๊ฐ€ ์‚ญ์ œ ๋  ๋•Œ ์ปคํ”Œ ๊นŒ์ง€ ์‚ญ์ œ ํ•˜๋Š”๊ฒƒ์ด๋‹ค. ์•ˆ์“ฐ๋Š”๊ฑธ๋กœ...

 

@JoinColumn

๋ฐ›๋Š”์‚ฌ๋žŒ๊ณผ ๋ณด๋‚ธ ์‚ฌ๋žŒ์„ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด receiver,sender ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

๋‘˜๋‹ค user ์ด๊ธฐ ๋•Œ๋ฌธ์— @JoinColumn ์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ํ•œ๋‹ค.

name = " " ์ด ๋ถ€๋ถ„์—๋Š” user์˜ pk ์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์™ธ๋ž˜ํ‚ค ์ปฌ๋Ÿผ๋ช…์„ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

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

7. CI/CD ๊ณ„ํš  (0) 2022.02.09
6. ๋ฌด์—‡์ด ๋ฌธ์ œ์ผ๊นŒ ...  (0) 2022.01.25
5. Spring REST Docs ์‚ฌ์šฉ  (0) 2022.01.24
4. ์—ฐ๊ด€๊ด€๊ณ„ - OneToMany  (0) 2022.01.23
3. ์—ฐ๊ด€๊ด€๊ณ„ - ManyToOne  (0) 2022.01.23
2. Entity  (0) 2021.12.03