- 
                Notifications
    You must be signed in to change notification settings 
- Fork 151
[4기 박상민] Springboot-jpa weekly 미션 3 PR입니다. #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4기 박상민] Springboot-jpa weekly 미션 3 PR입니다. #334
Conversation
        
          
                src/main/java/org/programmers/jpaweeklymission/global/exception/GlobalExceptionHandler.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/main/java/org/programmers/jpaweeklymission/item/domain/Item.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      |  | ||
| public void setCustomer(Customer customer) { | ||
| if (Objects.nonNull(this.customer)) { | ||
| customer.removeOrder(this); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이게 로직이 맞나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 저부분 여쭤보려고 주석을 달았어야 했는데 깜빡했습니다.
일단 강사님께서 강의중에 저런 로직을 작성하셨습니다.
근데 그 부분에 좀 의문이 들었는데 사실 setCustomer 라는 건 제가 생각하기로는 초기에 주문(Order)에 고객(Customer)라는 걸 등록하는 거라고 생각했습니다.
근데 저걸 적으신 강사님이 의도한 건 아래 두 개라고 생각했습니다.
- 초기 주문 작성 시 미리 등록된 Customer가 있는지 한 번 더 체크하고 무시하기 위해서?
- 주문에 할당된 Customer를 수정하기 위해서?
다른 부분 Item, OrderItem 부분에도 nonNull 체크를 전부 적용하셨는데 이게 필요한가요?, 좋은 방법인가요? 멘토님들 의견 궁금합니다.
There was a problem hiding this comment.
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로 기존 구매자를 새롭게 인자로 넘어온 구매자로 대체하는데, 그렇다면 기존 구매자와 주문간 관계를 끊어주어야 하는 것이 아닐까요?
양방향 매핑이 조금 복잡하다면 단방향 매핑으로 단순하게 시작하는 것도 좋을 것 같네요
There was a problem hiding this comment.
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); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 orderItems에는 추가되는 orderItem을 add 해주지 않아도 되나요?
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
orderItem이 주도적으로 제어하는 이유가 있나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
누가 하던지 상관없다고 생각하는데, 키를 갖고 있고 수정 시 쿼리가 나가는 OrderItem 테이블에서 제어해주는게 좋다고 생각했습니다.
또, 양방향 매핑 시에 저런 메서드가 계속적으로 생겨나는데, 키를 갖고 있는 쪽에 저런 메서드를 정의하자라고 기준을 정헀습니다.
        
          
                src/test/java/org/programmers/jpaweeklymission/PersistenceContextTest.java
          
            Show resolved
            Hide resolved
        
              
          
                src/main/java/org/programmers/jpaweeklymission/global/exception/GlobalExceptionHandler.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | @Enumerated(EnumType.STRING) | ||
| @Column(name = "status", nullable = false) | ||
| @NotNull | ||
| OrderStatus status; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
접근제어자를 지정하지 않은 이유가 있나요?
There was a problem hiding this comment.
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.") | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
min 이 지정되어 있는데 not blank도 검증 해주어야 하나요?
There was a problem hiding this comment.
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) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
첫 번째 에러만 반환하는 이유가 있나요?
There was a problem hiding this comment.
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) | 
There was a problem hiding this comment.
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); | 
There was a problem hiding this comment.
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로 기존 구매자를 새롭게 인자로 넘어온 구매자로 대체하는데, 그렇다면 기존 구매자와 주문간 관계를 끊어주어야 하는 것이 아닐까요?
양방향 매핑이 조금 복잡하다면 단방향 매핑으로 단순하게 시작하는 것도 좋을 것 같네요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
승훈님 코멘트만 마저 반영해주세요
📌 과제 설명
강의 중 미션에 대해 매핑만 주어줬는데,
다음 PR 때는 Controller, Service를 구현해 보고 싶습니다.
commit 이 좀 꼬였습니다.. 죄송합니다.. git 좀 더 공부하겠습니다.
✅ 피드백 반영사항
#312 (comment)
에러메세지 출력이 생각한 대로 되지 않아 수정하였습니다.