QueryDsl Example

Sungju Kim·2024년 10월 29일

Sparta_Coding_Camp_TIL

목록 보기
52/53

Below is an example of using QueryDsl to search for meeting entities that will be stored in DTO, then returned via pagenation.

Key Functions

  • Filters meetings that have starting time that has already passed the current time.
  • If 'ranks' is given in the parameter, filter meetings that have equal 'ranks'.

JPAExpression

JPAExpressions in QueryDSL is a utility class used to create subqueries in JPQL (Java Persistence Query Language) statements.

MeetingQueryDslRepositoryImpl.java

When query.fetch() is called, QueryDSL executes the query against the database, collecting the data that matches the specified conditions. The method then returns these results as a List of MeetingSearchResponse objects.

package com.play.hiclear.domain.meeting.repository;
imports ...

@Repository
@RequiredArgsConstructor
public class MeetingQueryDslRepositoryImpl implements MeetingQueryDslRepository {
    private final JPAQueryFactory queryFactory;

    @Override
    public Page<MeetingSearchResponse> search(SortType sortType, Ranks ranks, Pageable pageable) {
        LocalDateTime now = LocalDateTime.now();
        var query = queryFactory
                .select(
                        Projections.constructor(
                                MeetingSearchResponse.class,
                                meeting,
                                JPAExpressions
                                        .select(participant.count())
                                        .from(participant)
                                        .where(participant.meeting.eq(meeting)))
                )
                .from(meeting)
                .where(
                        matchRank(ranks),
                        meeting.startTime.gt(now)
                )
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize());

        // Adjust sorting based on sortType
        if (sortType == SortType.EARLIEST) {
            query.orderBy(meeting.startTime.asc()); // Sort by earliest startTime
        } else {
            query.orderBy(meeting.id.desc()); // LATEST sort if not EARLIEST
        }

        List<MeetingSearchResponse> results = query.fetch();

        Long totalCount = queryFactory
                .select(Wildcard.count)
                .from(meeting)
                .where(
                        matchRank(ranks),
                        meeting.startTime.gt(now)
                )
                .fetchOne();

        return new PageImpl<>(results, pageable, totalCount);
    }

    private BooleanExpression matchRank(Ranks ranks) {
        return ranks != null ? meeting.ranks.eq(ranks) : null;
    }
}

MeetingQueryDslRepository.java

package com.play.hiclear.domain.meeting.repository;

public interface MeetingQueryDslRepository {
    Page<MeetingSearchResponse> search(SortType sortType, Ranks rank, Pageable pageable);
}
profile
Fully ✨committed✨ developer, always eager to learn!

0개의 댓글