diff --git a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java index bb53f58..b2ff304 100644 --- a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java +++ b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java @@ -2,6 +2,7 @@ import com.moneymong.domain.agency.entity.Agency; import com.moneymong.domain.agency.entity.enums.AgencyType; +import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.function.Supplier; import static com.moneymong.domain.agency.entity.QAgency.agency; @@ -38,7 +40,7 @@ public Page findByUniversityNameByPaging(String universityName, Pageable public Page findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type, Pageable pageable) { JPAQuery query = queryFactory.selectFrom(agency) - .where(eqUniversityName(universityName).or(agency.agencyType.eq(type))) + .where(agency.agencyType.eq(type).or(eqUniversityName(universityName))) .limit(pageable.getPageSize()) .offset(pageable.getOffset()); @@ -58,7 +60,16 @@ private JPAQuery getCountQuery(String universityName, AgencyType type) { return queryFactory.selectFrom(agency) .where(eqUniversityName(universityName).or(agency.agencyType.eq(type))); } - private BooleanExpression eqUniversityName(String universityName) { - return universityName != null ? agency.universityName.eq(universityName) : null; + + private BooleanBuilder eqUniversityName(String universityName) { + return nullSafeBooleanBuilder(() -> agency.universityName.eq(universityName)); + } + + private BooleanBuilder nullSafeBooleanBuilder(Supplier supplier) { + try { + return new BooleanBuilder(supplier.get()); + } catch (IllegalArgumentException e) { + return new BooleanBuilder(); + } } }