From 5ec2efa6bbfb5cb708ddc27f5a7ec2cbab82eed2 Mon Sep 17 00:00:00 2001 From: Jose Luis Rodriguez Alonso Date: Fri, 21 Jun 2013 19:02:48 +0100 Subject: [PATCH] DATAJPA-361 - Improve QueryDslJpaRepository.findAll(Predicate, Pageable). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The paginating ….findAll(…) of QueryDslJpaRepository now only executes the query if we really have items to fetch, i.e. if the page requested really exists. --- .../data/jpa/repository/support/QueryDslJpaRepository.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java index 0c36f096a..cf2d4996b 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java @@ -16,6 +16,7 @@ package org.springframework.data.jpa.repository.support; import java.io.Serializable; +import java.util.Collections; import java.util.List; import javax.persistence.EntityManager; @@ -110,7 +111,10 @@ public class QueryDslJpaRepository extends SimpleJpa JPQLQuery countQuery = createQuery(predicate); JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate)); - return new PageImpl(query.list(path), pageable, countQuery.count()); + Long total = countQuery.count(); + List content = total > pageable.getOffset() ? query.list(path) : Collections. emptyList(); + + return new PageImpl(content, pageable, total); } /*