How can I convert a spring data Sort to a querydsl OrderSpecifier?

前端 未结 4 760
有刺的猬
有刺的猬 2021-02-04 06:23

This is basically the opposite of this: How to do a paged QueryDSL query with Spring JPA?

This is for a custom query for which i can\'t use any of the findAll() methods.

4条回答
  •  感情败类
    2021-02-04 07:15

    org.springframework.data.domain.Sort.Order and com.querydsl.core.types.Order and are so similar, yet there is no straightforward conversion between the two. This is somewhat improved version of frozenfury answer:

    PathBuilder entityPath = new PathBuilder<>(Entity.class, "entity");
    for (Order order : pageable.getSort()) {
        PathBuilder path = entityPath.get(order.getProperty());
        query.orderBy(new OrderSpecifier(com.querydsl.core.types.Order.valueOf(order.getDirection().name()), path));
    }
    
        

    提交回复
    热议问题