Support custom countSpec in SimpleJpaRepository.findAll(…).

Closes #3727
This commit is contained in:
Joshua Chen
2024-12-28 15:02:46 +08:00
committed by Mark Paluch
parent 96968e91eb
commit 5315848cb7
2 changed files with 23 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ import org.springframework.lang.Nullable;
* @author Christoph Strobl
* @author Diego Krupitza
* @author Mark Paluch
* @author Joshua Chen
*/
public interface JpaSpecificationExecutor<T> {
@@ -71,6 +72,21 @@ public interface JpaSpecificationExecutor<T> {
*/
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
/**
* Returns a {@link Page} of entities matching the given {@link Specification}.
* <p>
* Supports counting the total number of entities matching the {@link Specification}.
* <p>
*
* @param spec can be {@literal null}, if no {@link Specification} is given all entities matching {@code <T>} will be
* selected.
* @param countSpec can be {@literal null}if no {@link Specification} is given all entities matching {@code <T>} will
* be counted.
* @param pageable must not be {@literal null}.
* @return never {@literal null}.
*/
Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable);
/**
* Returns all entities matching the given {@link Specification} and {@link Sort}.
* <p>

View File

@@ -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<T, ID> implements JpaRepositoryImplementation<T
@Override
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
return findAll(spec, spec, pageable);
}
@Override
public Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) {
TypedQuery<T> query = getQuery(spec, pageable);
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList())
: readPage(query, getDomainClass(), pageable, spec);
: readPage(query, getDomainClass(), pageable, countSpec);
}
@Override