Search code examples
micronautmicronaut-data

Example count query for Micronaut data @Query


Can someone give an example countQuery to implement pagination while doing explicit query (@Query) using Micronaut data? There is no examples at https://micronaut-projects.github.io/micronaut-data/latest/guide/#explicitQueries

I have to make a query like the one below

@Query("From UserRelation where userId = :userId and itemId=:itemId", countQuery = <count query here>)

Solution

  • You can do it in this way:

    @Query(
      value = "select relation_ from UserRelation relation_ where relation_.userId = :userId and relation_.itemId = :itemId", 
      countQuery = "select count(relation_) from UserRelation relation_ where relation_.userId = :userId and relation_.itemId = :itemId"
    )
    

    The count query will be the same only with one change and it is the count() function in the SELECT clause.