How to do a paged QueryDSL query with Spring JPA?

强颜欢笑 提交于 2019-12-08 19:34:02

问题


QueryDSL defines an OrderSpecifier interface and an instance for that can be easily obtained for any field by calling asc() or desc(). The QueryDslPredicateExecutor interface of Spring Data JPA even has a findAll() method which takes OrderSpecifiers as parameters.

org.springframework.data.domain.PageRequest however doesn't know anything about QueryDSL and it has its own way for defining query sort order, namely org.springframework.data.domain.Sort. It can contain a number of org.springframework.data.domain.Sort.Orders which are a lot like OrderSpecifiers, except that they are not type safe etc.

So, if I want to make paged query which uses sorting, is there really no way of using QueryDSL for defining that?


回答1:


It should work like this if you can't find another way

private Sort sortBy(Path<?> path) {
    return new Sort(Sort.Direction.ASC, path.getMetadata().getExpression().toString());
}



回答2:


I know it's been a while and I'm not sure this was available at the time of the OP but there is now a QPageRequest object introduced which allows for sorting via QueryDSL to be added to spring data jpa Query DSL...




回答3:


Here is a much simpler way to construct a Sort object using QueryDSL:

new QSort(user.manager.firstname.asc())

Then you can use it in a PageRequest like so:

new PageRequest(0, 10, new QSort(user.manager.firstname.asc()))



回答4:


The getExpression() method has been removed, and I had expressions akin to QPost.post.period.periCode which needed a traversal, and without the complete name of expression I couldn't do anything about it, so now I made a method that gathers period.periCode and works perfectly on QPost.post.

private String resolveOrderPath(Path<?> path) {
    StringBuilder stringBuffer = new StringBuilder(path.getMetadata().getName());
    path = path.getMetadata().getParent();
    while(!path.getMetadata().isRoot()) {
        stringBuffer.insert(0, path.getMetadata().getName() + ".");
        path = path.getMetadata().getParent();
    }
    return stringBuffer.toString();
}

Path<?> path = QPost.post.period.periCode;
String propertyPath = resolveOrderPath(path);
Sort sort = new Sort("asc".equals(sSortDir0) ? Sort.Direction.ASC : Sort.Direction.DESC, propertyPath);


来源:https://stackoverflow.com/questions/11673213/how-to-do-a-paged-querydsl-query-with-spring-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!