Elouafi
Elouafi

Reputation: 301

Pagination with spring boot usiing jpa

How can I implement a method that return a page of objects using JpaRepository and not PagingAndSortingRepository ?

My repository

public interface GroupRepository extends JpaRepository<Group, Long> {   
    @Query(value = "SELECT g FROM Group")
    Page<Group> listAllByPage(Pageable pageable);
}

My service implementation:

@Override
public 
Page<Group> findGroupesByPagination(Pageable pageable) {
    return groupeRepository.listAllByPage(pageable);
}

My rest Controller method:

@RequestMapping(value="/groups", method = RequestMethod.GET)
public @ResponseBody Page<Group> list( Pageable pageable){
    Page<Group> groupes = groupeServiceImpl.findGroupesByPagination(pageable);
    return groupes;
}

Finally I got this error:

Error creating bean with name 'groupServiceImpl': Unsatisfied dependency expressed through field 'groupeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'groupRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page rimtrack.org.repository.GroupRepository.listAllByPage(org.springframework.data.domain.Pageable)!

Upvotes: 1

Views: 4170

Answers (1)

ooozguuur
ooozguuur

Reputation: 3466

The query can be defined by an annotation somewhere or declared by other means. Consult the documentation of the specific store to find available options for that store. If the repository infrastructure does not find a declared query for the method at bootstrap time, it fails.

You should using Spring Data Jpa method. Reference

  Page<T> findAll(Pageable pageable);

Please change repository class.

Exam:

public interface GroupRepository extends JpaRepository<Group, Long> {   
  Page<Group> findAlll(Pageable pageable);
}

Upvotes: 3

Related Questions