执行findAll()

时间:2018-10-28 15:29:17

标签: java spring-data couchbase

我想从Couchbase存储桶中获取某种类型的所有文档。

我创建了一个扩展了CatRepository的{​​{1}},所以我继承了CouchbaseRepository方法。

运行程序并到达以下行:

findAll()

我收到以下错误:

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) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.producers.SingleProducer.request(SingleProducer.java:65) ~[rxjava-1.2.7.jar:1.2.7]
at rx.Subscriber.setProducer(Subscriber.java:211) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:113) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:281) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:216) ~[rxjava-1.2.7.jar:1.2.7]
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) ~[rxjava-1.2.7.jar:1.2.7]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_161]
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_161]
at java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_161]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_161]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_161]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]

2 个答案:

答案 0 :(得分:0)

findAll需要在Couchbase本身上创建视图。

您可以通过Couchbase UI执行此操作:

  • 登录到Couchbase用户界面
  • 转到索引(左侧菜单)
  • 视图(中上)
  • 选择您的存储桶(右上角下拉列表)
  • 添加视图(右上方)
  • 使用与您的对象(foo)相匹配的名称,并使用视图名称all,因此文档称为_design/[bucket_name]/foo并视图all
  • (可选)编辑视图,替换地图代码:
    function (doc, meta) {
        if (doc._class == "[your_package].Foo") {
            emit(meta.id, null);
        }
    }
  • 发布视图(然后应出现在“生产视图”列表中)

注意:此答案与Couchbase 6有关。此过程在旧版本/新版本上可能会有所不同

答案 1 :(得分:0)

您还可以按如下方式使用注释@ViewIndexed:

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "area")
public interface AreaRepository extends CouchbaseRepository<Area, String> {

     List<Area> findByBusinessUnityIdAndRemoved(String businessId, boolean removed);
}