简体   繁体   English

在spring-data-couchbase(N1QL)中计数查询

[英]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: 我正在使用Spring模块编写couchbase存储库,并尝试使用N1QL查询添加自己的count方法实现:

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? 所以我的问题是:如何使用spring-data-couchbase编写计数查询?

I cannot find anything about this in spring documentation. 我在春季文档中找不到关于此的任何信息。 link 链接

This exception happens because the @Query annotation was designed with the use-case of retrieving entities in mind. 发生此异常是因为@Query注释在设计时就考虑了检索实体的用例。 Projections to a scalar like count are uncovered corner cases as of RC1. 从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 :( 我试图提出一个自定义存储库方法实现,但似乎在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. 编辑:像long这样的简单返回类型的用例以及仅使用单个聚合的SELECT应该可以起作用,因此这是一个错误/改进。 I've opened ticket DATACOUCH-187 in the Spring Data JIRA. 我已经在Spring Data JIRA中打开票证DATACOUCH-187

@Query("SELECT count(*) , META(default).id as _ID, META(default).cas as _CAS FROM default")

将您的查询更改为此。

Use this query : 使用此查询:

 @Query("SELECT count(*) as count FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} ")
 long myCount();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM