spring data jpa @query and pageable

You can use pagination with a native query. It is documented here: Spring Data JPA – Reference Documentation

You can however use native queries for pagination by specifying the count query yourself:
Example 59. Declare native count queries for pagination at the query method using @Query

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
    countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
    nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);

Leave a Comment