Skip to content

Conversation

@joyewon0705
Copy link
Collaborator

📌 개요

  • 게시글 수정 및 생성 시 발생하던 Unique Key 충돌 및 flush 타이밍 문제를 해결했습니다.
  • 카테고리 매핑 시 postCategoryMappings를 즉시 초기화하면서 영속성 컨텍스트 내 동일 키 중복이 발생하던 부분을 개선했습니다.

🔨 작업 내용

1. Post.updateCategories() 로직 개선

  • 기존: postCategoryMappings.clear() 후 전부 새로 추가
    → 기존 매핑이 삭제되지 않은 상태에서 동일한 (post_id, category_id) 조합이 중복으로 flush되며 Unique Key 충돌 발생

  • 개선:

    • 현재 매핑과 새 매핑을 비교하여 실제 변경된 카테고리만 추가/삭제
    • 삭제는 removePostCategoryMapping()으로 안전하게 detach
    • 추가는 새 PostCategoryMapping(this, category) 생성으로 처리

2. PostService.createPost() 저장 순서 수정

  • 기존: Post 생성 후 카테고리 매핑 후 저장

  • 개선: Post를 먼저 save하여 영속 상태를 보장한 뒤, 카테고리 매핑 수행
    → flush 타이밍이 안정화되고, 비영속 상태에서 발생하던 key 충돌 방지

🔗 관련 이슈

Closes #{이슈 번호}

📝 참고 사항

✅ 체크리스트

  • 기능 동작 확인
  • 테스트 코드 작성
  • 문서/주석 추가 및 최신화

@github-actions github-actions bot changed the title Refactor: PostCategoryMapping Unique Key 충돌 문제 해결 Refactor: PostCategoryMapping Unique Key 충돌 문제 해결 (#245) Oct 13, 2025
@joyewon0705 joyewon0705 merged commit feb9c7a into dev Oct 13, 2025
3 checks passed
@github-actions github-actions bot deleted the Refactor/245 branch October 13, 2025 08:03
@joyewon0705 joyewon0705 restored the Refactor/245 branch October 13, 2025 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants