• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

OnToMany 관계의 복잡한 동적쿼리는 어떻게 해결하나요?

23.01.05 23:16 작성 조회수 1.62k

0

 @Override
    public Page<?> findAllPostWithCategory3(Pageable pageable, List<Tech> techList, Category category, Place place) {


            JPAQuery<PostResponseDto> query = queryFactory.
                    select(new QPostResponseDto(post))
                    .from(post)
                    .leftJoin(post.account, account).fetchJoin()
                    .where(checkCategory(category), checkPlace(place))
                    .leftJoin(post.techs, techs)
                    .where(checkTechList(techList))
                    .offset(pageable.getOffset())
                    .limit(pageable.getPageSize());

            // sorting
            for (Sort.Order o : pageable.getSort()) {
                PathBuilder pathBuilder = new PathBuilder(post.getType(), post.getMetadata());
                query.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC,
                        pathBuilder.get(o.getProperty())));
            }
            List<PostResponseDto> list = query.fetch();
            JPAQuery<Long> countQuery = queryFactory
                    .select(post.count())
                    .from(post)
                    .where(checkTechList(techList));
            return PageableExecutionUtils.getPage(list, pageable, countQuery::fetchOne);
    }
//동적 관리
 /*techList in query where*/
private BooleanExpression checkTechList (List < Tech > techList) {
     return techList == null ? null : techs.tech.in(techList);
}


 /*category in query where*/
 private BooleanExpression checkCategory (Category category){
     return category == null ? null : post.category.eq(category);
 }

 /*place in query where*/
 private BooleanExpression checkPlace (Place place){
     return place == null ? null : post.place.eq(place);
 }

Post를 기준으로 account는 manyToOne이고

지금 문제가 되는것은 Tech입니다. Post와 oneToMany관계인데, 동적쿼리를 tech의 값으로 해야되서 문제가 발생합니다..
페이지네이션을 유지해야하기에, 어떻게 짜야할지 모르겠습니다. 제발 도움이나 힌트 부탁드립니다

답변 1

답변을 작성해보세요.

0

안녕하세요. S-J L님

해당 부분은 활용2편에서 자세히 설명드렸습니다^^ 활용2편을 다시 복습해보시면 답을 찾으실 수 있을거에요.

감사합니다.