JPA

[JPA] countQuery

MY_STUDY 2024. 11. 10. 15:35
    @Query(value = "select id, name from product where name = :productName", nativeQuery = true)
    Page<Product> findAllByNameWithPagination(String productName, Pageable pageable);

 

페이징으로 조회 할 때, 다음과 같이 기본적인 쿼리 형식으로 조회할 경우 에러가 발생할 확률이 높아진다.

 

 

 

    @Query(value = "select id,name from product a where name = :productName", nativeQuery = true
            ,countQuery = "select count(*) from product where name = :productName ")
    Page<Product> findAllByNameWithPagination(String productName, Pageable pageable);

 

countQuery 를 이용하여 총 갯수를 구한 후, 조회한다면 에러가 발생하지 않는다.