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 4d18351a99..96438997d3 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 @@ -38,6 +38,7 @@ * @author Christoph Strobl * @author Diego Krupitza * @author Mark Paluch + * @author Joshua Chen */ public interface JpaSpecificationExecutor { @@ -71,6 +72,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 7ba13651e6..0e3bf0c115 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 @@ -100,6 +100,7 @@ * @author Ernst-Jan van der Laan * @author Diego Krupitza * @author Seol-JY + * @author Joshua Chen */ @Repository @Transactional(readOnly = true) @@ -456,10 +457,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