diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java index 0d38cc580d..5954833b3a 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java @@ -37,6 +37,7 @@ * @author Christoph Strobl * @author Diego Krupitza * @author Mark Paluch + * @author Joshua Chen */ public interface JpaSpecificationExecutor { @@ -70,6 +71,21 @@ public interface JpaSpecificationExecutor { */ Page findAll(@Nullable Specification spec, Pageable pageable); + /** + * Returns a {@link Page} of entities matching the given {@link Specification}. + *

+ * Supports counting the total number of entities matching the {@link Specification}. + *

+ * + * @param spec can be {@literal null}, if no {@link Specification} is given all entities matching {@code } will be + * selected. + * @param countSpec can be {@literal null},if no {@link Specification} is given all entities matching {@code } will + * be counted. + * @param pageable must not be {@literal null}. + * @return never {@literal null}. + */ + Page findAll(@Nullable Specification spec, @Nullable Specification countSpec, Pageable pageable); + /** * Returns all entities matching the given {@link Specification} and {@link Sort}. *

diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index bb784e2115..b31c60db58 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -99,6 +99,7 @@ * @author Ernst-Jan van der Laan * @author Diego Krupitza * @author Seol-JY + * @author Joshua Chen */ @Repository @Transactional(readOnly = true) @@ -455,10 +456,15 @@ public List findAll(Specification spec) { @Override public Page findAll(@Nullable Specification spec, Pageable pageable) { + return findAll(spec, spec, pageable); + } + + @Override + public Page findAll(@Nullable Specification spec, @Nullable Specification countSpec, Pageable pageable) { TypedQuery query = getQuery(spec, pageable); return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) - : readPage(query, getDomainClass(), pageable, spec); + : readPage(query, getDomainClass(), pageable, countSpec); } @Override