Search code examples
javaspringcouchbasespring-data-couchbase

Counting query in spring-data-couchbase (N1QL)


I'm writing couchbase repository using Spring module and I'm trying to add my own implementation of count method using N1QL query:

public interface MyRepository extends CouchbaseRepository<Entity, Long> {
    @Query("SELECT count(*) FROM default")
    long myCount();
}

But it doesn't work:

org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Unable to retrieve enough metadata for N1QL to entity mapping, have you selected _ID and _CAS?

So my question is: how can I write counting query using spring-data-couchbase?

I cannot find anything about this in spring documentation. link


Solution

  • This exception happens because the @Query annotation was designed with the use-case of retrieving entities in mind. Projections to a scalar like count are uncovered corner cases as of RC1. Maybe I can think of some way of adding support for it through explicit boolean flag in the annotation?

    Unfortunately I was unable to find a workaround. I was trying to come up with a custom repository method implementation but it appears support for it is broken in 2.0.0-RC1 :(

    edit: The use case of simple return types like long, with a SELECT that only uses a single aggregation, should work so this is a bug/improvement. I've opened ticket DATACOUCH-187 in the Spring Data JIRA.