[4기 박현지] Mission3: Order 도메인 구현, 연관 관계 매핑 및 테스트#295
Open
hyeon-z wants to merge 23 commits intoprgrms-be-devcourse:hyeon-zfrom
Open
[4기 박현지] Mission3: Order 도메인 구현, 연관 관계 매핑 및 테스트#295hyeon-z wants to merge 23 commits intoprgrms-be-devcourse:hyeon-zfrom
hyeon-z wants to merge 23 commits intoprgrms-be-devcourse:hyeon-zfrom
Conversation
beomukim
approved these changes
Jul 29, 2023
beomukim
left a comment
There was a problem hiding this comment.
현지님 코드 깔끔하게 작성하셨네요👍
궁금하신 점 말씀드리면 현재 OrderItem은 Order와 Item과 OneToMany로 매핑된 상태인데, 조인된 테이블이 삭제되면서 해당 외래키가 지워져 exception이 터진 것 같아요. orphanRemoval = true로 설정하면 자식 엔티티도 같이 삭제되니까 orderItemRepository.deleteAll()은 지우셔도 됩니다
엔티티끼리 여러 연관관계가 맺어진 경우에는 위의 경우처럼 외래키 제약 위반과 같은 예외가 발생할 수 있으니 delete 순서를 잘 고려하시면서 테스트코드 작성하시면 될 것 같습니다!
juno-junho
approved these changes
Jul 31, 2023
| @JoinColumn(name = "member_id") | ||
| private Member member; | ||
|
|
||
| @OneToMany(mappedBy = "order", cascade = CascadeType.PERSIST, orphanRemoval = true) |
There was a problem hiding this comment.
persist만 특정지어 cascade를 하는 이유가 있을까요?
| import java.time.LocalDateTime; | ||
|
|
||
| @MappedSuperclass | ||
| public class BaseEntity { |
There was a problem hiding this comment.
abstract로 설정하여 entity로서 기능을 못하게 막는것은 어떨까요?
|
|
||
| private LocalDateTime createdAt = LocalDateTime.now(); | ||
|
|
||
| private LocalDateTime updatedAt = LocalDateTime.now(); |
There was a problem hiding this comment.
now로 설정하신것은 entity가 생성될 때 초기화 해주기 위함인것인가요?
생성자에서는 그럼 updatedAt과 createdAt는 제외시켜주나요?
Author
There was a problem hiding this comment.
넵! Order가 생성될 때 현재 시각으로 초기화되어서 저장됩니다!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 과제 설명
Member, Order, OrderItem, Item의 도메인을 구현하고 연관 관계를 매핑하여 테스트까지 완료했습니다.
👩💻 요구 사항과 구현 내용
✅ 피드백 반영사항
🤔 PR 포인트 & 궁금한 점
해당 메서드를 통해 매 테스트 메서드의 실행 전에 데이터를 제거하는 코드를 실행했는데 외래키 제약 위반
exception이 발생해서@OneToMany( ~, orphanRemoval = true))를 설정했더니 해당 부분이 해결되었습니다.제가 이해한 바로는
orphanRemoval이 부모 엔티티로부터 끊어진(삭제된) 자식 엔티티가 자동으로 삭제되도록 지정하도록 하는 것인데 이것이repository의deleteAll과 함께 어떻게 적용이 되는지가 궁금합니다.또한 이렇게 여러 연관관계를 가지는 경우에는 어떻게 데이터를 지우며 테스트 코드를 작성하는 것이 좋은지도 궁금합니다.