• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

Sort 질문있습니다.

22.03.27 13:29 작성 조회수 369

0

Querydsl 4RepositorySupport에 Sort를 처리해주는 로직이 없는데 이건 따로 구현해야하는건가요?

 

Querydsl4RepositorySupport에서

@Autowired
public void setEntityManager(EntityManager entityManager) {
Assert.notNull(entityManager, "EntityManager must not be null!");
JpaEntityInformation entityInformation = JpaEntityInformationSupport.getEntityInformation(domainClass, entityManager);
SimpleEntityPathResolver resolver = SimpleEntityPathResolver.INSTANCE;
EntityPath path = resolver.createPath(entityInformation.getJavaType());
this.entityManager = entityManager;
this.querydsl = new Querydsl(entityManager, new PathBuilder<>(path.getType(), path.getMetadata()));
this.queryFactory = new JPAQueryFactory(entityManager);
}

이 부분이 스프링 데이터에서 제공하는 Pageable안에 있는 Sort를 사용할 수 잇도록 설정한 것이라고 하셨는데 그럼 

 

MemberTestRepository에서

public Page<Member> applyPaginationWithSort(MemberSearchCondition condition, Pageable pageable) {
return applyPagination(pageable, contentQuery -> contentQuery
.selectFrom(member)
.leftJoin(member.team, team)
.where(usernameEq(condition.getUsername()),
teamNameEq(condition.getTeamName()),
ageGoe(condition.getAgeGoe()),
ageLoe(condition.getAgeLoe())))
.orderBy(pageable.getSort());
}

 

와 같이 pageable.getSort()로 Sort값을 가져와 orderBy절에서 사용하면 되는건지 궁금합니다

 

 

답변 1

답변을 작성해보세요.

1

안녕하세요. 조윤호님

다음과 같이 Sort 조건을 pageable에 포함해서 던지면 해당 조건이 함께 적용됩니다. 별도로 orderBy에 적용하지 않아도 됩니다.

PageRequest pageable = PageRequest.of(0, 10, Sort.by("age"));

감사합니다.