What i want : skip the count query that spring performs with findAll(Pageable)
.
I found that there is Slice
datatype for such UIs (next based) where there is no need for total records/pages hence no need for count query.
I can use Slice
return type for methods like findByName(Pageable)
etc
but if i use Slice for findAll(Pageable) then it still performs the count query.
Is there a workaround where i am able to use slice for findAll
avoiding the count query?
P.S. : i want the features that slice provide like - hasNext, size etc and i don't need to use Specification as well.
Any help is appreciated.
The List<Todo> findAll() method returns all Todo objects that are found from the database. The Optional<Todo> findOne(Long id) method finds the todo entry whose id is given as a method parameter.
No, there is no difference between them, they will execute exactly the same query, the All part is ignored by Spring Data when deriving the query from the method name.
findAll. Returns all entities matching the given Specification and Sort . Parameters: spec - can be null.
It is possible to use Slice
with findAll()
- you just need to extend the correct Spring JPA Repository.
It won't work if you extend JpaRepository
, because it extends PagingAndSortingRepository
and there's already findAll(Pageable pageable)
method defined in the Paging repository.
If you extend CrudRepository
then you will be able to define method Slice<T> findAll(Pageable pageable)
in your repository.
You can use query derivation for this. Based on a Spring Data JPA examples:
/**
* Returns a {@link Slice} counting a maximum number of {@link Pageable#getPageSize()} users matching given criteria
* starting at {@link Pageable#getOffset()} without prior count of the total number of elements available.
*
* @param lastname
* @param page
* @return
*/
Slice<User> findByLastnameOrderByUsernameAsc(String lastname, Pageable page);
You can modify this to your needs by adding the following to your repository:
Slice<User> findBy(Pageable page);
Since it doesn't have any other condition it performs a findAll
limited to a single page, but without a count query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With