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 96968e9 commit 5315848
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @author Christoph Strobl
* @author Diego Krupitza
* @author Mark Paluch
* @author Joshua Chen
*/
public interface JpaSpecificationExecutor<T> {

Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
* @author Ernst-Jan van der Laan
* @author Diego Krupitza
* @author Seol-JY
* @author Joshua Chen
*/
@Repository
@Transactional(readOnly = true)
Expand Down Expand Up @@ -456,10 +457,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

0 comments on commit 5315848

Please sign in to comment.