Skip to content

Commit

Permalink
fix: left join 시 중복 문제 해결 -> 일부 서브 쿼리로 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjaez committed Sep 19, 2024
1 parent 398a6e9 commit d060ef4
Showing 1 changed file with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ public GatheringRepositoryImpl() {

@Override
public List<GatheringBriefResponse> getGatheringRecommend(Long gatheringId, Long gatheringCategoryId, Long userId) {

NumberExpression<Integer> likeCount = countGreatGatheringByGatheringById();
NumberExpression<Integer> applicantsCount = applicantsCountNowConsent();
return from(gathering)
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
.leftJoin(bookMarkGathering)
.on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId)))
.leftJoin(category).on(category.id.eq(gathering.gatheringCategory.id))
.leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id).and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT)))
.where(gathering.isFinish.eq(Boolean.FALSE)
.and(gathering.gatheringCategory.id.eq(gatheringCategoryId))
.and(gathering.id.ne(gatheringId))
Expand All @@ -78,9 +75,9 @@ public List<GatheringBriefResponse> getGatheringRecommend(Long gatheringId, Long
zoneCategoryParent.name,
zoneCategoryChild.name,
gathering.viewCount,
bookMarkGathering.user.id.isNotNull(),
countGreatGatheringByGatheringById(),
category.name,
isGatheringBookmark(userId),
likeCount,
gathering.gatheringCategory.name,
gathering.user.name,
gathering.scheduleStartDate,
gathering.scheduleEndDate,
Expand All @@ -89,7 +86,7 @@ public List<GatheringBriefResponse> getGatheringRecommend(Long gatheringId, Long
gathering.startAge,
gathering.endAge,
gathering.personCount,
gatheringApplicants.count().coalesce(0L).intValue(),
applicantsCount,
isUserGreatGathering(userId)
)).limit(3L).fetch();
}
Expand All @@ -104,23 +101,15 @@ public Page<GatheringBriefResponse> getGatheringPageFilterAndOrder(Pageable page
OrderSpecifier<?> orderSpecifier = getOrderSpecifier(gatheringPageRequest.getSort());

NumberExpression<Integer> countGreatGathering = countGreatGatheringByGatheringById();
NumberExpression<Integer> applicantsCount = applicantsCountNowConsent();

long total = from(gathering)
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
.leftJoin(bookMarkGathering)
.on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId)))
.leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id))
.where(booleanBuilder)
.select(gathering.id.countDistinct()).fetchCount();
.select(gathering.id).fetchCount();

List<GatheringBriefResponse> content = from(gathering)
.distinct()
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
.leftJoin(bookMarkGathering)
.on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId)))
.leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id).and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT)))
.where(booleanBuilder)
.groupBy(gathering.id, zoneCategoryChild.id, zoneCategoryParent.id, category.id,
gathering.title, gathering.viewCount, gathering.user.name,
Expand All @@ -135,7 +124,7 @@ public Page<GatheringBriefResponse> getGatheringPageFilterAndOrder(Pageable page
zoneCategoryParent.name,
zoneCategoryChild.name,
gathering.viewCount,
bookMarkGathering.user.id.isNotNull(),
isGatheringBookmark(userId),
countGreatGathering,
gathering.gatheringCategory.name,
gathering.user.name,
Expand All @@ -146,7 +135,7 @@ public Page<GatheringBriefResponse> getGatheringPageFilterAndOrder(Pageable page
gathering.startAge,
gathering.endAge,
gathering.personCount,
gatheringApplicants.count().coalesce(0L).intValue(),
applicantsCount,
isUserGreatGathering(userId)
))
.offset(pageable.getOffset())
Expand Down Expand Up @@ -236,13 +225,10 @@ public List<GatheringRankResponse> getGatheringRankList() {
@Override
public List<GatheringBriefResponse> getGatheringLikeCountFromCreatedIn3(Long userId) {
NumberExpression<Integer> likeCount = countGreatGatheringByGatheringById();
NumberExpression<Integer> applicantsCount = applicantsCountNowConsent();
return from(gathering)
.distinct()
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
.leftJoin(bookMarkGathering).on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId)))
.leftJoin(category).on(category.id.eq(gathering.gatheringCategory.id))
.leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id).and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT)))
.where(gathering.isFinish.eq(Boolean.FALSE)
.and(gathering.isDeleted.eq(Boolean.FALSE))
.and(gathering.createdAt.after(LocalDateTime.now().minusMonths(3)))
Expand All @@ -260,7 +246,7 @@ public List<GatheringBriefResponse> getGatheringLikeCountFromCreatedIn3(Long use
zoneCategoryParent.name,
zoneCategoryChild.name,
gathering.viewCount,
bookMarkGathering.user.id.isNotNull(),
isGatheringBookmark(userId),
likeCount,
gathering.gatheringCategory.name,
gathering.user.name,
Expand All @@ -271,7 +257,7 @@ public List<GatheringBriefResponse> getGatheringLikeCountFromCreatedIn3(Long use
gathering.startAge,
gathering.endAge,
gathering.personCount,
gatheringApplicants.count().coalesce(0L).intValue(),
applicantsCount,
isUserGreatGathering(userId)
)).limit(6L).fetch();
}
Expand Down Expand Up @@ -358,4 +344,26 @@ private BooleanExpression isUserGreatGathering(Long userId) {
.otherwise(false);
}

private BooleanExpression isGatheringBookmark(Long userId) {
return new CaseBuilder()
.when(JPAExpressions.selectOne()
.from(bookMarkGathering)
.where(bookMarkGathering.gathering.id.eq(gathering.id)
.and(bookMarkGathering.user.id.eq(userId)))
.exists())
.then(true)
.otherwise(false);
}

private NumberExpression<Integer> applicantsCountNowConsent() {
JPQLQuery<Integer> applicantsCountSubQuery = JPAExpressions
.select(gatheringApplicants.count().intValue())
.from(gatheringApplicants)
.where(gatheringApplicants.gathering.id.eq(gathering.id)
.and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT)));

return Expressions.numberTemplate(Integer.class, "{0}", applicantsCountSubQuery)
.coalesce(0);
}

}

0 comments on commit d060ef4

Please sign in to comment.