케넨킴
takoyakisoba
케넨킴
글쓰기 / 관리자
전체 방문자
오늘
어제
  • 분류 전체보기 (21)
    • 깨알팁 (2)
    • JAVA (5)
    • JPA (4)
    • Spring (1)
    • 네트워크 (3)
    • 자료구조 (0)
    • TIL (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
hELLO · Designed By 정상우.
케넨킴

takoyakisoba

JPA

[JPA] Entity안에서 데이터를 수정하는 비즈니스 로직을 만들 때

2023. 4. 25. 12:10
@Entity
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CherryBox extends BaseEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private int quantity;
    private int totalPriceBeforeDiscount;
    private double discountRate;
    private String description;
    private int pricePerCherryBox;

    public void updateCherryBox(int quantity, int totalPriceBeforeDiscount, double discountRate, String description, int pricePerCherryBox){
        this.quantity = quantity;
        this.totalPriceBeforeDiscount = totalPriceBeforeDiscount;
        this.discountRate = discountRate;
        this.description = description;
        this.pricePerCherryBox = pricePerCherryBox;
    }
}

updateCherryBox라는 엔티티의 데이터값을 수정하는 메소드를 만들었다.

처음에는 메소드의 파라미터를 아래처럼 dto객체를 통째로 넘겨주려했다.

/*현재방법 */
public void updateCherryBox(int quantity,
        int totalPriceBeforeDiscount,
        double discountRate, 
        String description, 
        int pricePerCherryBox)

/*처음에 생각난 방법 */
public void updateCherryBox(UpdateRequestDto dto){}

하지만 dto객체자체를 넘겨주니 entity에 dto를 import해야한다고 IDE에 안내가 뜨는 순간 생각이 들었다.

이러면 결합도가 높아지지 않나? 그래서 바로 검색해봤다. dto를 파라미터로 넘겨주면 코드자체는 단순해지지만 객체간 의존성이 늘어나서 사용하지 않는것을 추천한다고 한다. 하지만 필드가 너무 길어지면 VO를 따로 만들어서 dto에는 의존적이지 않은 상태로 객체를 넘겨줄 수도 있다고 한다.

어쨌든 스쳐지나간 생각을 그냥 흘리지 않아서 다행이다.

'JPA' 카테고리의 다른 글

[JPA] JPQL(1) - 객체지향 쿼리 소개  (0) 2023.04.12
연관관계 매핑 기초 - 단방향  (0) 2023.04.06
[JPA] 영속성  (0) 2023.03.14
    'JPA' 카테고리의 다른 글
    • [JPA] JPQL(1) - 객체지향 쿼리 소개
    • 연관관계 매핑 기초 - 단방향
    • [JPA] 영속성
    케넨킴
    케넨킴
    꾸준히 하는게 젤 어렵더라구요

    티스토리툴바