Skip to content

[4기 고예성] Mission 1, 2, 3 PR 제출합니다. #310

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

Open
wants to merge 10 commits into
base: Dev-Yesung/mission-complete
Choose a base branch
from

Conversation

Dev-Yesung
Copy link
Member

📌 과제 설명

JPA 위클리 미션 1,2,3 구현입니다.

👩‍💻 요구 사항과 구현 내용

미션1

  • JPA 프로젝트를 세팅해본다.
  • 세팅한 프로젝트를 이용해서 단일 엔티티를 이용한 CRUD를 구현한다.
    • 고객(Customer) 엔티티는 ID(PK), 이름, 성을 가진다.
    • 고객엔티티를 이용한 CRUD를 구현한다.

미션2 -> (테스트 패키지의 PersistenceContextTest 클래스)

  • customer 엔티티를 이용하여 영속성컨텍스트의 엔티티 생명주기를 실습해본다.

미션3

  • order, order_item, item 의 연관관계 매핑을 실습해본다.

✅ PR 포인트 & 궁금한 점

  1. JPA 학습이 처음이라 부족한 부분이 많습니다..!
    어색한 부분이 있으면 바로 피드백 부탁드립니다~
    제시하고 싶은 학습 내용이 있으시면 그것도 부탁드립니다!

  2. JPA에 의해 객체지향적으로 테이블 설계를 하는 것은 이해가 됐습니다.
    그런데 실무(회사)에 보면 테이블을 구현하는 개발자가 짜는 경우도 있지만,
    다른 사람이 짜주는 경우도 있는데, 그런 테이블은 객체지향적으로 짜여있지 않은 테이블일텐데
    이럴 경우에는 개발자가 그 테이블을 JPA에 맞게 수정해서 테이블을 설계하게 되는건가요?
    궁금합니다!

  3. DDD 아키텍처에서는 에그리거트의 루트를 통해서만 데이터를 변경할 수 있는 걸로 알고 있습니다!
    그렇다면 이번 미션에서 제가 만든 엔티티를 예로 들었을 때,
    Order 도메인에서 주문하는 특정 Item의 수량을 변경하게 된다면
    Order에 참조 맵핑된 OrderItems에 접근한 후 수정하는게 옳은 방식인가요?
    즉, Order 도메인을 중심으로 데이터를 수정해야 되는건가요?

JPA에 매핑될 Customer 엔티티를 생성하였습니다.
CRUD 테스트를 작성했습니다.
@GeneratedValue를 통해 자동으로 id가 생성되기 때문에 매개변수가 필요없어 생성자를
수정했습니다.
기본생성자가
protected로 선언된 것에서 @NoArgConstructor(access = AccessLevel.PROTECTED)로
변경했습니다.
persist(), detach(), clear(), merge(), remove(), flush()를 이용해 영속성 컨텍스트를
테스트 했습니다.
연관관계매핑(order, order_item, item의 연관관계 매핑 실습)
필요없는 필드와 메서드를 제거하는 등의 리팩토링
미션3 : 연관관계매핑(order, order_item, item의 연관관계 매핑 실습)
OrderItem과 Order의 양방향 매핑에서 업데이트시
서로 객체에 연관관계를 추가해줘야 하는데 세팅하지 않아 수정했습니다.
JPA 미션 최종완료
미션1 : 2. JPA 소개(단일 엔티티를 이용한 CRUD를 구현)
미션2 : 3.
영속성컨텍스트(customer 엔티티를 이용하여 생명주기 실습)
미션3 : 4-2. 연관관계매핑(order, order_item,
item의 연관관계 매핑 실습)
Comment on lines +50 to +86
public void reduceQuantity(int quantity) {
int reducedQuantity = this.quantity - quantity;
if (reducedQuantity < 0) {
throw new IllegalValueException("[ERROR] 물건의 수량이 부족합니다!");
}
this.quantity = reducedQuantity;
}

private void checkName(String name) {
checkEmptyName(name);
checkIllegalCharacter(name);
}

private void checkEmptyName(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalNameException("[ERROR] 이름 값은 비어있을 수 없습니다!");
}
}

private void checkIllegalCharacter(String name) {
String pattern = "^[a-zA-Zㄱ-ㅎ가-힣]*$";
if (!Pattern.matches(pattern, name)) {
throw new IllegalNameException("[ERROR] 이름 값은 한글과 영문만 가능합니다!");
}
}

private void checkQuantity(int quantity) {
if (quantity < 0) {
throw new IllegalValueException("[ERROR] 잘못된 주문 수량입니다!");
}
}

private void checkPrice(int orderPrice) {
if (orderPrice < 0) {
throw new IllegalValueException("[ERROR] 잘못된 주문 금액입니다!");
}
}

Choose a reason for hiding this comment

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

예외 메시지에 전부 [ERROR]라고 붙여줄 필요가 있나 싶네요. ㅎㅎ

Comment on lines 34 to 36
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;

Choose a reason for hiding this comment

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

Order -> OrderItem -> Item 으로 보통 함께 조회될 것 같은데,
OrderItem -> Item은 FetchType이 Eager로 되는게 더 적절(안전)할 것 같음

Copy link
Member Author

Choose a reason for hiding this comment

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

사실 완전히 딱 이해됐다고는 못하지만...! 어느 정도 이해됐습니다.
근데 아직 코드작성할 때 이 문제를 눈치챌 수 있는 수준은 안되는거 같네요...
반영하고 계속 공부해 나가는 걸로 하겠습니다~! 감사합니다👍

OrderItem의 Item 클래스와의 fetchType을 변경했습니다.
Order->OrderItem->Item으로 조회가
일어나게 될 때
Order에서 OrderItem은 Lazy로딩입니다.
기존에 OrderItem에서 Item을 조회하는 방식도
Lazy로 설정해놨는데,
이는 N+1문제를 야기할 수 있습니다. 따라서 Eager로 하는 것이 적절합니다.
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.

2 participants