Refactor: PostCategoryMapping Unique Key 충돌 문제 해결 (#245) #263
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.
📌 개요
postCategoryMappings를 즉시 초기화하면서 영속성 컨텍스트 내 동일 키 중복이 발생하던 부분을 개선했습니다.🔨 작업 내용
1.
Post.updateCategories()로직 개선기존:
postCategoryMappings.clear()후 전부 새로 추가→ 기존 매핑이 삭제되지 않은 상태에서 동일한
(post_id, category_id)조합이 중복으로 flush되며 Unique Key 충돌 발생개선:
removePostCategoryMapping()으로 안전하게 detachPostCategoryMapping(this, category)생성으로 처리2.
PostService.createPost()저장 순서 수정기존:
Post생성 후 카테고리 매핑 후 저장개선: Post를 먼저 save하여 영속 상태를 보장한 뒤, 카테고리 매핑 수행
→ flush 타이밍이 안정화되고, 비영속 상태에서 발생하던 key 충돌 방지
🔗 관련 이슈
Closes #{이슈 번호}
📝 참고 사항
✅ 체크리스트