Skip to content

Commit

Permalink
Support custom countSpec in SimpleJpaRepository.findAll(…).
Browse files Browse the repository at this point in the history
Closes #3727
  • Loading branch information
JoshuaChen authored and mp911de committed Jan 23, 2025
1 parent ef226e9 commit b0b1e5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -96,6 +98,21 @@ default List<T> findAll(PredicateSpecification<T> spec) {
*/
Page<T> findAll(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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
* @author Ernst-Jan van der Laan
* @author Diego Krupitza
* @author Seol-JY
* @author Joshua Chen
*/
@Repository
@Transactional(readOnly = true)
Expand Down Expand Up @@ -454,10 +455,15 @@ public List<T> findAll(Specification<T> spec) {

@Override
public Page<T> findAll(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
Expand Down

0 comments on commit b0b1e5e

Please sign in to comment.