Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 이 PR은 조직 문서 조회 API의 기능을 확장하여, 조직 문서와 연관된 크루 문서 목록을 함께 제공합니다. 이를 통해 클라이언트가 조직 문서와 관련된 크루 정보를 한 번의 API 호출로 얻을 수 있게 되어 데이터 접근성을 향상시키고, 추가적인 API 호출의 필요성을 줄여 사용자 경험을 개선합니다. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| List<LinkedCrewDocumentResponse> linkedCrewDocuments = documentOrganizationLinkRepository.findAllByOrganizationDocument( | ||
| organizationDocument) | ||
| .stream() | ||
| .map(link -> new LinkedCrewDocumentResponse(link.getCrewDocument())) | ||
| .toList(); |
There was a problem hiding this comment.
현재 구현은 findAllByOrganizationDocument로 DocumentOrganizationLink 목록을 가져온 후, 스트림 내에서 각 link에 대해 link.getCrewDocument()를 호출하여 N+1 쿼리 문제를 발생시킬 수 있습니다. DocumentOrganizationLink의 crewDocument가 지연 로딩(LAZY)으로 설정되어 있어, 연결된 크루 문서의 수만큼 추가적인 쿼리가 발생하여 성능 저하를 유발할 수 있습니다.
이 문제를 해결하고 코드를 더 효율적으로 만들기 위해, DocumentOrganizationLinkRepository에 JPQL을 사용하여 필요한 데이터만 LinkedCrewDocumentResponse DTO로 직접 조회하는 새로운 메서드를 추가하는 것을 권장합니다. 이렇게 하면 단일 쿼리로 모든 관련 크루 문서 정보를 가져올 수 있습니다.
DocumentOrganizationLinkRepository.java에 아래와 같은 메서드를 추가할 수 있습니다:
@Query("SELECT new com.wooteco.wiki.organizationdocument.dto.response.LinkedCrewDocumentResponse(cd.uuid, cd.title) " +
"FROM DocumentOrganizationLink dol " +
"JOIN dol.crewDocument cd " +
"WHERE dol.organizationDocument = :organizationDocument")
List<LinkedCrewDocumentResponse> findLinkedCrewDocumentsByOrganizationDocument(@Param("organizationDocument") OrganizationDocument organizationDocument);그리고 서비스 로직에서는 이 새로운 메서드를 호출하도록 수정하면 됩니다.
List<LinkedCrewDocumentResponse> linkedCrewDocuments = documentOrganizationLinkRepository.findLinkedCrewDocumentsByOrganizationDocument(organizationDocument);There was a problem hiding this comment.
DTO Proejction을 사용하면 패키지가 쿼리 내부로 들어가기 때문에, 파일 변경 시에도 추적이 어려워질 것 같다는 우려가 들었어요. 현재로써는 사용처가 하나일 뿐이지만, 하나의 DTO 패키지의 하드코딩 방식이 어려 메서드에 분산되어 있다면 유지보수 측면에서 비용이 더욱 늘거라고 생각하고요.
현재 문제점으로 제안해주신 부분이 N+1인 만큼, 이 부분은 성능 상 고려해야 할 점이라는 생각이 들어서
다음과 같은 방법을 고려해봤습니다.
"repository에서의 fetch join"
이 방식을 사용하게 되면 N+1문제도 해결하면서 런타임 환경에서의 타입 안정성도 확인할 수 있을 것 같아서요!
혹시 이 방식은 어떻게 생각하시는 지 궁금합니다.
아래 코드를 함께 남겨 놓도록 할게요!
@Query("SELECT documentOrganizationLink FROM DocumentOrganizationLink documentOrganizationLink " +
"JOIN FETCH documentOrganizationLink.crewDocument " +
"WHERE documentOrganizationLink.organizationDocument = :organizationDocument")
List<DocumentOrganizationLink> findAllByOrganizationDocumentWithCrewDocument(
@Param("organizationDocument") OrganizationDocument organizationDocument);There was a problem hiding this comment.
2Jin1031님, DTO Projection 사용 시 패키지 변경 추적의 어려움에 대한 우려는 충분히 이해합니다. 특히 DTO 구조가 자주 변경되거나 여러 곳에서 사용될 경우 유지보수 비용이 증가할 수 있습니다.
제안해주신 FETCH JOIN 방식은 N+1 문제를 해결하면서 런타임 시 타입 안정성을 유지하는 아주 좋은 대안입니다. FETCH JOIN을 사용하면 DocumentOrganizationLink 엔티티를 조회할 때 연관된 crewDocument를 즉시 로딩하여 N+1 쿼리를 방지할 수 있습니다. 이 방식은 DTO Projection과 달리 Repository 계층에서 DTO 패키지 경로를 직접 참조하지 않으므로, DTO 변경에 따른 Repository 코드 수정 부담을 줄일 수 있습니다.
다만, FETCH JOIN은 경우에 따라 불필요하게 많은 데이터를 가져올 수 있지만, 현재 DocumentOrganizationLink와 CrewDocument의 관계에서는 큰 문제가 되지 않을 것으로 보입니다.
제안해주신 findAllByOrganizationDocumentWithCrewDocument 메서드를 DocumentOrganizationLinkRepository에 추가하고, OrganizationDocumentService에서는 이 메서드를 호출하도록 수정하면 N+1 문제를 효과적으로 해결할 수 있습니다.
DocumentOrganizationLinkRepository.java에 다음 메서드를 추가해주세요:
@Query("SELECT dol FROM DocumentOrganizationLink dol JOIN FETCH dol.crewDocument WHERE dol.organizationDocument = :organizationDocument")
List<DocumentOrganizationLink> findAllByOrganizationDocumentWithCrewDocument(@Param("organizationDocument") OrganizationDocument organizationDocument);그리고 OrganizationDocumentService.java의 해당 부분을 다음과 같이 수정할 수 있습니다. 이 변경은 기존 findAllByOrganizationDocument 호출을 새로운 FETCH JOIN 메서드 호출로 대체하여 N+1 문제를 해결합니다.
| List<LinkedCrewDocumentResponse> linkedCrewDocuments = documentOrganizationLinkRepository.findAllByOrganizationDocument( | |
| organizationDocument) | |
| .stream() | |
| .map(link -> new LinkedCrewDocumentResponse(link.getCrewDocument())) | |
| .toList(); | |
| List<LinkedCrewDocumentResponse> linkedCrewDocuments = documentOrganizationLinkRepository.findAllByOrganizationDocumentWithCrewDocument( | |
| organizationDocument) | |
| .stream() | |
| .map(link -> new LinkedCrewDocumentResponse(link.getCrewDocument())) | |
| .toList(); |
PR Summary - feat/156
📌 개요
조직 문서 조회 시 연결된 크루 문서 목록을 함께 제공하는 기능 구현
🎯 주요 변경 사항
1. 조직 문서 조회 API 응답 확장
기존 동작
개선 사항
API 엔드포인트
응답 예시
{ "organizationDocumentId": 1, "organizationDocumentUuid": "...", "title": "조직 문서 제목", "contents": "...", "writer": "...", "generateTime": "...", "organizationEventResponses": [...], "linkedCrewDocuments": [ { "documentUuid": "crew-uuid-1", "title": "크루원1" }, { "documentUuid": "crew-uuid-2", "title": "크루원2" } ] }🏗️ 구현 내용
1. DTO 추가
LinkedCrewDocumentResponse.java (신규 생성)
documentUuid,title2. 응답 객체 확장
OrganizationDocumentAndEventResponse.java (수정)
linkedCrewDocuments필드 추가3. Repository 메서드 추가
DocumentOrganizationLinkRepository.java (수정)
findAllByOrganizationDocument()메서드 추가4. Service 로직 개선
OrganizationDocumentService.java (수정)
findByUuid()메서드에 크루 문서 조회 로직 추가📁 변경된 파일
신규 추가
LinkedCrewDocumentResponse.java- 크루 문서 간단 응답 DTO수정
OrganizationDocumentAndEventResponse.java- 크루 문서 목록 필드 추가DocumentOrganizationLinkRepository.java- OrganizationDocument 기준 조회 메서드 추가OrganizationDocumentService.java- 크루 문서 조회 로직 추가🔗 관련 이슈
#156