19

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.

EDIT:

Posted the wrong link. Now corrected.

Community
  • 1
  • 1
beginner_
  • 7,230
  • 18
  • 70
  • 127

5 Answers5

22

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<Object> orderByExpression = new PathBuilder<Object>(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())));
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Frank
  • 698
  • 8
  • 16
  • marked as answer as my question was answered and I came to the same solution except using MyClass.class instead of Object.class. However how do i do this in a generic way, eg MyClass is actually ? – beginner_ Oct 27 '12 at 14:49
  • 1
    You should reuse the orderByExpression, since it is a common parent for the order by children. Also the name is misleading, since the children are the actual "order by expressions". – Timo Westkämper Oct 29 '12 at 07:12
  • 1
    This kind of works for my case, but not completely. The problem is that I need to sort calculated fields (sum, count). I have them as aliases but the problem is the PathBuilder always appends to the query "alias." + property. In the example it always returns "ORDER BY object.{alias}" which is invalid. How can I get QueryDSL to just append "ORDER BY {alias}" ? – Cenobyte321 Sep 06 '16 at 17:48
  • This worked for me after some fiddling. Anyone know if it's secure? – wmakley Jul 01 '20 at 22:05
9

I don't know if it is still relevant but there is an implementation in the spring data jpa for doing the conversion between a data.domain.Sort (Spring JPA) Object to an OrderSpecifier (QueryDSL).

GIT Source of Querydsl Support in Spring JPA

It is really ugly implementation but you could still reuse it for your own purpose as the method is private:

public JPQLQuery applySorting(Sort sort, JPQLQuery query)

But if you use Spring data JPA, in your custom Repository implementation, you just need to do:

public Page<MyObject> findAll(Predicate predicate, Pageable pageable) {

    QMyObject myObject = QMyObject.myObject;
    JPQLQuery jPQLQuery = from(myObject)
            .join(myObject.user)
            .where(predicate);
    jPQLQuery = getQuerydsl().applyPagination(pageable, jPQLQuery);
    List<MyObject> myObjectList = jPQLQuery.list(myObject);
    long count =  jPQLQuery.count();
    Page<MyObject> myObjectPage = new PageImpl<MyObject>(myObjectList, pageable, count);
    return myObjectPage;  
}

Hope it could help!

existenz31
  • 382
  • 2
  • 5
  • 14
5
private OrderSpecifier<?>[] getSortedColumn(Sort sorts){   
    return sorts.toList().stream().map(x ->{
        Order order = x.getDirection().name() == "ASC"? Order.ASC : Order.DESC;
        SimplePath<Object> filedPath = Expressions.path(Object.class, Qobject, x.getProperty());
        return new OrderSpecifier(order, filedPath);
    }).toArray(OrderSpecifier[]::new);
}

then you can use like this:

  .where(...), 
  .orderBy(getSortedColumn(pageable.getSort()))
Junyeong Yeo
  • 53
  • 1
  • 3
3

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<Entity> entityPath = new PathBuilder<>(Entity.class, "entity");
for (Order order : pageable.getSort()) {
    PathBuilder<Object> path = entityPath.get(order.getProperty());
    query.orderBy(new OrderSpecifier(com.querydsl.core.types.Order.valueOf(order.getDirection().name()), path));
}
Dario
  • 43
  • 4
0

Alternatively, if you want to directly apply the sort into your base query without having to convert to OrderSpecifier<?>, you can leverage org.springframework.data.jpa.repository.support.Querydsl.

JPQLQuery<?> query = new JPAQuery<>(entityManager);
//prepare your base query
query.select(<YourQEntity>).from(<YourQEntity>).where(<whereClause>);

Querydsl querydsl = new Querydsl(entityManager, (new PathBuilderFactory()).create(<YourEntityClass>.class));

//apply org.springframework.data.domain.Sort
querydsl.applySorting(pageable.getSort(), query);
sha-mik
  • 191
  • 1
  • 4
  • The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Feb 10 '21 at 09:57
  • Best answear! It just works (may 2023) – liy May 26 '23 at 10:17