rinuthomaz
rinuthomaz

Reputation: 1413

How to use findAll method in spring boot with cruderepository

My UserRepository:

public interface UserRepository extends CrudRepository<User, Integer> {
    List<User> findAll(List<Integer> ids);
}

Error:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type User

Refer - http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html?is-external=true#findAll-java.lang.Iterable-

Can some one tell me how to get list of User objects based on List of Id's.

This is working

@Query(" select new User(id,x,y,z) from User b where b.id in ?1 ")
List<User> findById(List<Integer> id);

Upvotes: 1

Views: 23283

Answers (1)

Mico
Mico

Reputation: 2009

Firstly, I would rename the repository to UserRepository, because having 2 User classes is confusing.

findAll(), by definition, is meant to get all the models with no criteria. You should add a method named findByIdIn(Collection<Integer> ids)

Use List<User> findAll(Iterable<Integer> ids) or List<User> findByIdIn(List<Integer> ids)

Upvotes: 6

Related Questions