Skip to content

Conversation

@smartandhandsome
Copy link

@smartandhandsome smartandhandsome commented Aug 5, 2023

📌 과제 설명

  • 미션2 : 3. 영속성컨텍스트(customer 엔티티를 이용하여 생명주기 실습)
  • 미션3 : 4-2. 연관관계매핑(order, order_item, item의 연관관계 매핑 실습)

강의 중 미션에 대해 매핑만 주어줬는데,
다음 PR 때는 Controller, Service를 구현해 보고 싶습니다.

commit 이 좀 꼬였습니다.. 죄송합니다.. git 좀 더 공부하겠습니다.

✅ 피드백 반영사항

#312 (comment)
에러메세지 출력이 생각한 대로 되지 않아 수정하였습니다.

@smartandhandsome smartandhandsome self-assigned this Aug 5, 2023

public void setCustomer(Customer customer) {
if (Objects.nonNull(this.customer)) {
customer.removeOrder(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이게 로직이 맞나요?

Copy link
Author

@smartandhandsome smartandhandsome Aug 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 저부분 여쭤보려고 주석을 달았어야 했는데 깜빡했습니다.

일단 강사님께서 강의중에 저런 로직을 작성하셨습니다.

근데 그 부분에 좀 의문이 들었는데 사실 setCustomer 라는 건 제가 생각하기로는 초기에 주문(Order)에 고객(Customer)라는 걸 등록하는 거라고 생각했습니다.

근데 저걸 적으신 강사님이 의도한 건 아래 두 개라고 생각했습니다.

  1. 초기 주문 작성 시 미리 등록된 Customer가 있는지 한 번 더 체크하고 무시하기 위해서?
  2. 주문에 할당된 Customer를 수정하기 위해서?

다른 부분 Item, OrderItem 부분에도 nonNull 체크를 전부 적용하셨는데 이게 필요한가요?, 좋은 방법인가요? 멘토님들 의견 궁금합니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 로직은 상품이 생성될 때 구매자가 중복 등록되는 것을 방지하는 코드가 맞는 것 같습니다.

if(Objects.nonNull(this.customer))는 현재 주문에 구매자가 등록되어 있는지 확인하는 구문이네요. 그런데 customer.removeOrder(this)는 인자로 넘어온 구매자와 주문간 관계를 끊어주는 것 같습니다. 이 부분이 조금 어색한 것 같아요.

아래에서 this.customer = customer로 기존 구매자를 새롭게 인자로 넘어온 구매자로 대체하는데, 그렇다면 기존 구매자와 주문간 관계를 끊어주어야 하는 것이 아닐까요?

양방향 매핑이 조금 복잡하다면 단방향 매핑으로 단순하게 시작하는 것도 좋을 것 같네요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 잘 이해한 건지 모르겠는데 "그렇다면 기존 구매자와 주문간 관계를 끊어주어야 하는 것이 아닐까요?" 이 부분을 customer.removeOrder(this)라는 문장으로 처리합니다.

}

public void addOrderItems(OrderItem orderItem) {
orderItem.setOrder(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 orderItems에는 추가되는 orderItem을 add 해주지 않아도 되나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setOrder 메서드가 아래처럼 작성 되어 있습니다.

this.order = order;
order.getOrderItems().add(this);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orderItem이 주도적으로 제어하는 이유가 있나요?

Copy link
Author

@smartandhandsome smartandhandsome Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

누가 하던지 상관없다고 생각하는데, 키를 갖고 있고 수정 시 쿼리가 나가는 OrderItem 테이블에서 제어해주는게 좋다고 생각했습니다.

또, 양방향 매핑 시에 저런 메서드가 계속적으로 생겨나는데, 키를 갖고 있는 쪽에 저런 메서드를 정의하자라고 기준을 정헀습니다.

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
@NotNull
OrderStatus status;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

접근제어자를 지정하지 않은 이유가 있나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깜빡했습니다...

수정했습니다. 9731909

import jakarta.validation.constraints.Size;

public record CustomerCreationRequest(
@NotBlank(message = "First name must not be blank.")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min 이 지정되어 있는데 not blank도 검증 해주어야 하나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각해보니 " " 같은 부분이 있기 때문에 다시 설정했습니다.

return ErrorResponse.newErrorResponse(e
.getBindingResult()
.getFieldErrors()
.get(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

첫 번째 에러만 반환하는 이유가 있나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

첫 번째 에러에 제가 validation 규칙에서 적용한 message를 담고 있어서 첫번째 에러만 반환했습니다.

뭔가 코드가 찜찜하긴 한데 혹시 다른 방법이 있나요?

public class Order extends BaseEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따로 id 생성 전략을 지정하지 않는 이유가 있나요?


public void setCustomer(Customer customer) {
if (Objects.nonNull(this.customer)) {
customer.removeOrder(this);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 로직은 상품이 생성될 때 구매자가 중복 등록되는 것을 방지하는 코드가 맞는 것 같습니다.

if(Objects.nonNull(this.customer))는 현재 주문에 구매자가 등록되어 있는지 확인하는 구문이네요. 그런데 customer.removeOrder(this)는 인자로 넘어온 구매자와 주문간 관계를 끊어주는 것 같습니다. 이 부분이 조금 어색한 것 같아요.

아래에서 this.customer = customer로 기존 구매자를 새롭게 인자로 넘어온 구매자로 대체하는데, 그렇다면 기존 구매자와 주문간 관계를 끊어주어야 하는 것이 아닐까요?

양방향 매핑이 조금 복잡하다면 단방향 매핑으로 단순하게 시작하는 것도 좋을 것 같네요

Copy link
Member

@dojinyou dojinyou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

승훈님 코멘트만 마저 반영해주세요

@smartandhandsome smartandhandsome merged commit 874f374 into prgrms-be-devcourse:smart-sangmin Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants