Rajeev Ranjan
Rajeev Ranjan

Reputation: 4076

Get count of rows for a repository query before the actual call

I have a Repository class implementing PagingAndSortingRepository. There is a method as below which returns paginated results.

@Query("SELECT dt FROM Data dt JOIN dt.categories dc WHERE dt.service in (:services) AND dc.id = :cid")
    public List<Data> findForCategoryAndServices(@Param("cid")String cid, @Param("services")Set<Service> services, Pageable page);

I need a cumulative count of all the data which can help depict pagination at UI. Is there some method out of the box for this?

Or should I make an explicit count query for the above query which is but obvious?

Upvotes: 0

Views: 615

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

Return a Page<Data> rather than a List<Data>, and Spring-data will execute the count query for you, by deriving it from the actual query.

If it can't, or if you have a better count query, you can specify it using the countQuery annotation parameter.

Upvotes: 3

Related Questions