2017-04-14 133 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