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

Don't use findAll(Pageable pageable) #103

Open
simasch opened this issue Dec 29, 2022 · 6 comments
Open

Don't use findAll(Pageable pageable) #103

simasch opened this issue Dec 29, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@simasch
Copy link

simasch commented Dec 29, 2022

When using Page<T> findAll(Pageable pageable) Spring Data will execute the select plus the count query for every call.

This can be very inefficient and in the case of a DataProvider with paging and sorting simply superfluous.

It would be better to add a custom method to the Repository like:

List<T> findBy(Pageable pageable)

to avoid the unnecessary count query.

@simasch simasch added the enhancement New feature or request label Dec 29, 2022
@Artur-
Copy link
Member

Artur- commented Jan 3, 2023

Sounds like a good idea but although

    List<SamplePerson> findBy(Pageable pageable);

works fine in a repository it seems like

    List<SamplePerson> findBy(Specification<SamplePerson> filter, Pageable pageable);

does not: "org.springframework.dao.InvalidDataAccessApiUsageException: At least 1 parameter(s) provided but only 0 parameter(s) present in query"

@simasch
Copy link
Author

simasch commented Jan 3, 2023

The problem maybe because the JpaSpecificationExecutor does only define these methods:

public interface JpaSpecificationExecutor<T> {
    Optional<T> findOne(@Nullable Specification<T> spec);

    List<T> findAll(@Nullable Specification<T> spec);

    Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);

    List<T> findAll(@Nullable Specification<T> spec, Sort sort);

    long count(@Nullable Specification<T> spec);

    boolean exists(Specification<T> spec);

    long delete(Specification<T> spec);

    <S extends T, R> R findBy(Specification<T> spec, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
}

@Artur-
Copy link
Member

Artur- commented Jan 3, 2023

I think the problem is that you cannot mix By and Specification so the hack of using findBy cannot be used. It is also not possible to use findAll with a different return type.. So I'm not sure how create a method that would accept both Specification and Pageable but not return Page

@simasch
Copy link
Author

simasch commented Jan 3, 2023

Yeah, that's indeed a problem!

@simasch
Copy link
Author

simasch commented Jan 3, 2023

I don't understand why PagingAndSortingRepository does not support a findAll that returns a Slice or just a List

@Artur-
Copy link
Member

Artur- commented Jan 13, 2023

Let's see if there are any comments in spring-projects/spring-data-jpa#2762

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants