Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JpaRepository findAll(Iterable<ID> ids) + findAll(Sort sort)

With Spring Data JpaRepository is there any capability to get select collection of given Id with some sorting. That mean I need to make enable following query. I have found some solution apply with @NamedQuery but I can't make enable it because I am using Spring-data-jap 1.4.2.RELEASE. Thanks.

public Iterable<User> findAll(Iterable<Integer> userIds) {

 Sort sort = new Sort(Direction.ASC, "name");

 Iterable<User> users = userRepository.findAll(userIds, sort); 

 return users; 
}
like image 826
Channa Avatar asked Jun 29 '15 04:06

Channa


People also ask

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is difference between PagingAndSortingRepository and JpaRepository?

PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.


1 Answers

Just declare a query method like this:

public interface UserRepository extends Repository<User, Integer> {

  Iterable<User> findByIdIn(Collection<Integer> ids, Sort sort);
}
like image 128
Oliver Drotbohm Avatar answered Sep 20 '22 20:09

Oliver Drotbohm