Spring JPA 和 Couchbase - com.couchbase.client.java.error.ViewDoesNotExistException:视图 cat/all 不存在

Spring JPA & Couchbase - com.couchbase.client.java.error.ViewDoesNotExistException: View cat/all does not exist

我的 Spring boot 应用正在尝试从 Couchbase 存储桶中获取类型 Cat 的所有文档。

有一个索引:

CREATE INDEX cats_idx ON `cats`(_class) WHERE _class = 'com.example.Cat'

还有一个Repository class:

public interface CatRepository extends CouchbaseRepository<Cat, String>

从代码中调用时

Iterable<Cat> all = catRepository.findAll();

我得到这个异常:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request 
processing failed; nested exception is 
org.springframework.dao.InvalidDataAccessResourceUsageException: View cat/all does not exist.; 
nested exception is com.couchbase.client.java.error.ViewDoesNotExistException: View cat/all does not 
exist.] with root cause 
rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: com.couchbase.client.java.document.json.JsonObject.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
at rx.internal.producers.SingleProducer.request(SingleProducer.java:65)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:113)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:281)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:216)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

Spring Data SDK 的当前实现仍然在内部为 findAll 和 removeAll 等方法使用视图(SDK 3.0 版不再使用视图)。因此,您可以为此文档类型创建一个视图,也可以自己实现一个新的 findAll 方法:

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} ")
List<Cat> all();

该方法应该使用您的 cats_idx

PS: cats_idx 不是最优的,你不应该在索引中存储 _class 属性。

我在使用沙发底座时也遇到了同样的问题,如果有 .findAll() 方法正在执行。所以我在存储库 class.

中给出了以下注释
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "product",viewName = "all")
public interface ProductRepository extends CouchbaseRepository<Product,Integer> {
}

以上方法对我有用。