2017-04-14 109 views
1

如何实现一个方法,使用JpaRepository而不是PagingAndSortingRepository返回一页对象?分页与弹簧启动usiing jpa

我的仓库

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

我的服务实现:

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

我休息控制器的方法:

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

最后我得到这个错误:

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)!

+1

尝试在选择查询“组”后加上“G”:“选择克来自G组” – Cepr0

+0

是由于这是一个正确的答案 – Elouafi

+0

不要忘记我的+1评论;) – Cepr0

回答

1

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.

你应该使用Spring Data Jpa方法。 Reference

Page<T> findAll(Pageable pageable); 

请更改存储库类。

考试:

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

感谢您的回复,但我得到了同样的错误我应该怎么做请帮忙 – Elouafi

+0

请写出完整的堆栈跟踪。 – ozgur

+0

你是什么意思全堆栈跟踪。 #jackk? – Elouafi