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

前端 未结 4 761
有刺的猬
有刺的猬 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:17

    You can do somethings like this: But make sure to trim the o.getProperty() so you only pass the property and not "alias."+property

    
        if (pageable != null) {
                    query.offset(pageable.getOffset());
                    query.limit(pageable.getPageSize());
                    for (Sort.Order o : pageable.getSort()) {
                        PathBuilder orderByExpression = new PathBuilder(Object.class, "object");
    
                        query.orderBy(new OrderSpecifier(o.isAscending() ? com.mysema.query.types.Order.ASC
                                : com.mysema.query.types.Order.DESC, orderByExpression.get(o.getProperty())));
                    }
                }
    
    

提交回复
热议问题