Search code examples
javajpacouchbasespring-data-couchbase

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


My Spring boot app is trying to get all documents of type Cat from the Couchbasebucket.

There is an index for that:

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

And there is a Repository class:

public interface CatRepository extends CouchbaseRepository<Cat, String>

When calling this from the code

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

I get this exception:

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.access$201(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)

Solution

  • The current implementation of the Spring Data SDK still uses views internally for methods like findAll and removeAll (views are no longer used on the SDK version 3.0). So you could either create a view for this document type or implement a new findAll method yourself:

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

    The method should use your cats_idx

    PS: The cats_idx is not optimal, you should not store the _class attribute in the index.