Do I really need countQuery in a native @Query for pagination?

Viewed 183

According to the reference, countQuery is required for native queries pagination (at least there is an example with it). But according to my tests (Spring Boot 2.4.5), @Query pagination works without countQuery for such queries:

public interface ARepository extends JpaRepository<A, Long>{
  @Query(value = "select * from a", nativeQuery = true)
  Page<A> findAllANative(Pageable pageable);

  @Query(value = "select count(a.id) as aCount, category.name as 
        categoryName " +
        "from a " +
        "join category on a.category_id=category.id " +
        "group by category.name",
        nativeQuery = true)
  List<CountView> findACountByCategoryNative(Pageable pageable);
}

As you can see, there is no countQuery and they work.

Does pagination work only for these specific queries or I can omit countQuery in all cases?

0 Answers
Related