I'd like to use org.springframework.data.domain.Pageable
with com.querydsl.jpa.impl.JPAQuery
is there a way or is there a solid workaround how to make Pageable work with JPAQuery?
Asked
Active
Viewed 3,167 times
5

Mihkel L.
- 1,543
- 1
- 27
- 42
1 Answers
3
So I found nice why to make them work together posting full example:
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.persistence.EntityManager;
public class Example {
public void example(Pageable pageable, EntityManager em){
QCustomer qCustomer = QCustomer.customer;
JPAQuery<?> query = new JPAQuery<>(em);
query.from(qCustomer);
BooleanBuilder builder = new BooleanBuilder();
builder.and(qCustomer.email.likeIgnoreCase("c@m.c"));
JPAQuery<?> where = query.where(builder);
// This for loop is making it work with Pagable
query.offset(pageable.getOffset());
query.limit(pageable.getPageSize());
PathBuilder<Customer> entityPath = new PathBuilder<>(Customer.class, "customer");
for (Sort.Order order : pageable.getSort()) {
PathBuilder<Object> path = entityPath.get(order.getProperty());
query.orderBy(new OrderSpecifier(Order.valueOf(order.getDirection().name()), path));
}
List<Customer> resultList = query.createQuery().getResultList();
}
}

Mihkel L.
- 1,543
- 1
- 27
- 42
-
where do you get that Pageble Object ? – Rookie007 Jul 29 '19 at 06:51
-
Comes in from RestController – Mihkel L. Aug 24 '19 at 06:09