Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom countSpec in SimpleJpaRepository.findAll #3727

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Christoph Strobl
* @author Diego Krupitza
* @author Mark Paluch
* @author Joshua Chen
*/
public interface JpaSpecificationExecutor<T> {

Expand Down Expand Up @@ -70,6 +71,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>
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 @@ -455,10 +456,15 @@ public List<T> findAll(Specification<T> spec) {

@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
Expand Down