From 5315848cb766778477c25babce6cbfa966311feb Mon Sep 17 00:00:00 2001 From: Joshua Chen <27291761@qq.com> Date: Sat, 28 Dec 2024 15:02:46 +0800 Subject: [PATCH] =?UTF-8?q?Support=20custom=20countSpec=20in=20SimpleJpaRe?= =?UTF-8?q?pository.findAll(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3727 --- .../jpa/repository/JpaSpecificationExecutor.java | 16 ++++++++++++++++ .../repository/support/SimpleJpaRepository.java | 8 +++++++- 2 files changed, 23 insertions(+), 1 deletion(-) 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 4d18351a9..96438997d 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 @@ import org.springframework.lang.Nullable; * @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 7ba13651e..0e3bf0c11 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 @@ import org.springframework.util.Assert; * @author Ernst-Jan van der Laan * @author Diego Krupitza * @author Seol-JY + * @author Joshua Chen */ @Repository @Transactional(readOnly = true) @@ -456,10 +457,15 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation 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