Couchbase & Spring query error: “Query returning a primitive type are expected to return exactly 1 result, got X”

对着背影说爱祢 提交于 2019-12-11 02:46:49

问题


In our spring-boot 2.0.4.RELEASE application, we are using Couchbase version 5.5.1 build 3511 with Spring.

We added a new delete method to the Repository interface:

public interface CatRepository extends CouchbaseRepository<Cat, String> {

  long deleteAllByName(String name);

When calling the method, documents are deleted from the bucket, but we get the following error:

Query returning a primitive type are expected to return exactly 1 result, got X

The X value is changed according to the number of items that were deleted. In the example below, 27 items were deleted.

Here is the full stack trace:

    Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Query returning a primitive type are expected to return exactly 1 result, got 27] with root cause

org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Query returning a primitive type are expected to return exactly 1 result, got 27
    at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeSingleProjection(AbstractN1qlBasedQuery.java:203) ~[spring-data-couchbase-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeDependingOnType(AbstractN1qlBasedQuery.java:143) ~[spring-data-couchbase-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.execute(AbstractN1qlBasedQuery.java:106) ~[spring-data-couchbase-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87) ~[spring-data-couchbase-3.0.9.RELEASE.jar:3.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at com.sun.proxy.$Proxy130.deleteAllByName(Unknown Source)

We also tried to change the return type of the deleteAllByName(String name); to be void, but it has the same results.

In the pom, these are the client versions:

        <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-couchbase</artifactId>
        <version>3.0.9.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>com.couchbase.client</groupId>
        <artifactId>couchbase-client</artifactId>
        <version>1.4.13</version>
    </dependency>

回答1:


As far as I know, you must return the object that you are removing:

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "maintenanceSchedule")
public interface MaintenanceScheduleRepository extends CouchbasePagingAndSortingRepository<MaintenanceSchedule, String> {

    List<MaintenanceSchedule> deleteByCompanyIdAndMaintenancePlanId(String companyId, String maintenancePlanId);

    List<MaintenanceSchedule> deleteByCompanyIdAndResourceIdAndMaintenancePlanIdIn(String companyId, String resourceId,
                                                                               List<String> maintenancePlanIds);

}

If you don't need the removed objects, the best approach is to execute a N1QL query directly.



来源:https://stackoverflow.com/questions/52537525/couchbase-spring-query-error-query-returning-a-primitive-type-are-expected-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!