Post

OneToMany 관계의 entity를 Querydsl로 조회할 때 fetchjoin을 사용하면 데이터가 중복되어 조회될 수 있다.

OneToMany 관계의 entityQuerydsl로 조회할 때 fetchjoin을 사용하면 데이터가 중복되어 조회될 수 있다.

해결 방법

1
2
3
4
5
6
7
8
9
10
11
12
public SurveyDocument findSurveyById(Long surveyDocumentId) {
    SurveyDocument survey = queryFactory
            .selectFrom(surveyDocument)
            .leftJoin(surveyDocument.design, design).fetchJoin()
            .leftJoin(surveyDocument.date, dateManagement).fetchJoin()
            .leftJoin(surveyDocument.questionDocumentList, questionDocument).fetchJoin()
            .where(surveyDocument.id.eq(surveyDocumentId))
            .distinct()
            .fetchOne();

    return survey;
}
  • distinct를 추가하여 중복된 row를 제거할 수 있다.
  • 하지만 db에 날아간 쿼리의 결과에는 중복이 있으며
  • 쿼리를 통해 db에서 가져온 데이터를 Entity 차원에서 중복을 제거하는 것이다.
This post is licensed under CC BY 4.0 by the author.