-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from K-Hackathon-Fledge/42-bug-fetch-join-pagi…
…ng-error BUG : Fetch join + Paging 에러 처리 -> QueryDSL 적용 및 Count 쿼리 추가
- Loading branch information
Showing
6 changed files
with
156 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/fledge/fledgeserver/config/QueryDslConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.fledge.fledgeserver.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class QueryDslConfig { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.fledge.fledgeserver.support.repository; | ||
|
||
import com.fledge.fledgeserver.support.entity.SupportCategory; | ||
import com.fledge.fledgeserver.support.entity.SupportPost; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
public interface SupportPostRepositoryCustom { | ||
Page<SupportPost> findByCategoryAndSearchAndSupportPostStatusWithImages( | ||
List<SupportCategory> category, String q, String status, Pageable pageable); | ||
} | ||
|
80 changes: 80 additions & 0 deletions
80
src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.fledge.fledgeserver.support.repository; | ||
|
||
import com.fledge.fledgeserver.support.entity.QSupportPost; | ||
import com.fledge.fledgeserver.support.entity.SupportCategory; | ||
import com.fledge.fledgeserver.support.entity.SupportPost; | ||
import com.fledge.fledgeserver.support.entity.SupportPostStatus; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
|
||
import java.util.List; | ||
|
||
public class SupportPostRepositoryImpl extends QuerydslRepositorySupport implements SupportPostRepositoryCustom { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Autowired | ||
public SupportPostRepositoryImpl(JPAQueryFactory queryFactory) { | ||
super(SupportPost.class); | ||
this.queryFactory = queryFactory; | ||
} | ||
|
||
@Override | ||
public Page<SupportPost> findByCategoryAndSearchAndSupportPostStatusWithImages( | ||
List<SupportCategory> category, String q, String status, Pageable pageable) { | ||
|
||
QSupportPost supportPost = QSupportPost.supportPost; | ||
|
||
// 카운트 쿼리: 중복 없이 실제 SupportPost 엔티티 개수만 카운트 | ||
long total = queryFactory.selectFrom(supportPost) | ||
.where( | ||
categoryIsNullOrInCategory(supportPost, category), | ||
titleOrReasonContains(supportPost, q), | ||
statusIsNullOrMatches(supportPost, status) | ||
).fetchCount(); | ||
|
||
List<SupportPost> results = queryFactory.selectFrom(supportPost) | ||
.leftJoin(supportPost.images).fetchJoin() | ||
.where( | ||
categoryIsNullOrInCategory(supportPost, category), | ||
titleOrReasonContains(supportPost, q), | ||
statusIsNullOrMatches(supportPost, status) | ||
) | ||
.orderBy(supportPost.createdDate.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
return new PageImpl<>(results, pageable, total); | ||
} | ||
|
||
private BooleanExpression categoryIsNullOrInCategory(QSupportPost supportPost, List<SupportCategory> category) { | ||
return category == null || category.isEmpty() ? null : supportPost.supportCategory.in(category); | ||
} | ||
|
||
private BooleanExpression titleOrReasonContains(QSupportPost supportPost, String q) { | ||
if (q == null || q.isEmpty()) { | ||
return null; | ||
} | ||
return supportPost.title.containsIgnoreCase(q) | ||
.or(supportPost.reason.containsIgnoreCase(q)); | ||
} | ||
|
||
private BooleanExpression statusIsNullOrMatches(QSupportPost supportPost, String status) { | ||
if (status == null || status.isEmpty()) { | ||
return null; | ||
} | ||
if ("ing".equals(status)) { | ||
return supportPost.supportPostStatus.in(SupportPostStatus.PENDING, SupportPostStatus.IN_PROGRESS); | ||
} else if ("end".equals(status)) { | ||
return supportPost.supportPostStatus.in(SupportPostStatus.COMPLETED, SupportPostStatus.TERMINATED); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters