Skip to content

Commit b0b1e5e

Browse files
JoshuaChenmp911de
authored andcommitted
Support custom countSpec in SimpleJpaRepository.findAll(…).
Closes #3727
1 parent ef226e9 commit b0b1e5e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.jpa.domain.Specification;
2929
import org.springframework.data.jpa.domain.UpdateSpecification;
3030
import org.springframework.data.repository.query.FluentQuery;
31+
import org.springframework.lang.Nullable;
3132

3233
/**
3334
* Interface to allow execution of {@link Specification}s based on the JPA criteria API.
@@ -36,6 +37,7 @@
3637
* @author Christoph Strobl
3738
* @author Diego Krupitza
3839
* @author Mark Paluch
40+
* @author Joshua Chen
3941
* @see Specification
4042
* @see org.springframework.data.jpa.domain.UpdateSpecification
4143
* @see DeleteSpecification
@@ -96,6 +98,21 @@ default List<T> findAll(PredicateSpecification<T> spec) {
9698
*/
9799
Page<T> findAll(Specification<T> spec, Pageable pageable);
98100

101+
/**
102+
* Returns a {@link Page} of entities matching the given {@link Specification}.
103+
* <p>
104+
* Supports counting the total number of entities matching the {@link Specification}.
105+
* <p>
106+
*
107+
* @param spec can be {@literal null}, if no {@link Specification} is given all entities matching {@code <T>} will be
108+
* selected.
109+
* @param countSpec can be {@literal null},if no {@link Specification} is given all entities matching {@code <T>} will
110+
* be counted.
111+
* @param pageable must not be {@literal null}.
112+
* @return never {@literal null}.
113+
*/
114+
Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable);
115+
99116
/**
100117
* Returns all entities matching the given {@link Specification} and {@link Sort}.
101118
*

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
* @author Ernst-Jan van der Laan
100100
* @author Diego Krupitza
101101
* @author Seol-JY
102+
* @author Joshua Chen
102103
*/
103104
@Repository
104105
@Transactional(readOnly = true)
@@ -454,10 +455,15 @@ public List<T> findAll(Specification<T> spec) {
454455

455456
@Override
456457
public Page<T> findAll(Specification<T> spec, Pageable pageable) {
458+
return findAll(spec, spec, pageable);
459+
}
460+
461+
@Override
462+
public Page<T> findAll(@Nullable Specification<T> spec, @Nullable Specification<T> countSpec, Pageable pageable) {
457463

458464
TypedQuery<T> query = getQuery(spec, pageable);
459465
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList())
460-
: readPage(query, getDomainClass(), pageable, spec);
466+
: readPage(query, getDomainClass(), pageable, countSpec);
461467
}
462468

463469
@Override

0 commit comments

Comments
 (0)