Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (31.9m points)

spring - How to pass pageable for native query?

I was doing a project and there i had a requirement of using pageable object and recieved page object from JPA.

Does anyone have any idea on how to use this?

@Repository
public interface CustomerRepository extends JpaRepository<Customer,Long>{

    @Query("SELECT * FROM WHERE name=?1 AND surname=?2 ", nativeQuery = true)
    List<Customer> findAllByNameAndSurname(String name,String surname);
}

I want a page List for result fetch from this query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (31.9m points)

Spring Data JPA and native queries with pagination is not supported in Spring. According to documentation , Spring Data JPA repositories can be used with native queries and pagination. But, in real life Spring Data JPA (1.10.2.RELEASE) requires some additional fix.

You have to use this if you want pagination support.

List<Customer> customers = customerRepository.findAllByNameAndSurname(name,username);
PagedListHolder<Customer> pages = new PagedListHolder(customers);
pages.setPage(currentPageNumber); //set current page number
pages.setPageSize(pageSize); // set the size of page

pages.getPageList(); // return the list of items(or in your case List<Customer> ) of current page 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...