From b0b1e5ee9746c2064a8dfedd48d64d53e1cb2d78 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 --- .../repository/JpaSpecificationExecutor.java | 17 +++++++++++++++++ .../repository/support/SimpleJpaRepository.java | 8 +++++++- 2 files changed, 24 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 3aa6afbb1b..a252e70fab 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 @@ -28,6 +28,7 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.UpdateSpecification; import org.springframework.data.repository.query.FluentQuery; +import org.springframework.lang.Nullable; /** * Interface to allow execution of {@link Specification}s based on the JPA criteria API. @@ -36,6 +37,7 @@ * @author Christoph Strobl * @author Diego Krupitza * @author Mark Paluch + * @author Joshua Chen * @see Specification * @see org.springframework.data.jpa.domain.UpdateSpecification * @see DeleteSpecification @@ -96,6 +98,21 @@ default List findAll(PredicateSpecification spec) { */ Page findAll(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 8ff602227e..1034cb1734 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) @@ -454,10 +455,15 @@ public List findAll(Specification spec) { @Override public Page findAll(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