title: Converting from Couchbase document to POJO using Spring-data-couchbase
body: <p>I need to read a couchbase document and convert it to a simple pojo class object. 
  From the <a href="https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/" rel="nofollow noreferrer">docs</a>, I understand that I need to define some custom converter to convert that document into POJO object as it has a (non-primitive)field of type <code>org.apache.commons.json.JSONObject</code> as shown below</p>
  
  <pre><code>@Document
  public class MyClass{
  
      @Id
      private String id;
  
      @Field
      private JSONObject someJsonObj;
  //getters and setters
  
  }
  </code></pre>
  
  <p>below is the repository for this </p>
  
  <pre><code>@Repository
  @N1qlPrimaryIndexed
  public interface MyClassRepository extends CouchbaseRepository&lt;MyClass, String&gt;{}
  </code></pre>
  
  <p>while saving the object, below code is working fine, </p>
  
  <pre><code>     MyClass myclass = new MyClass();
       myclass.setId(someKey);
      myclass.setSomeJsonObj(jsonObject);
      myclassRepository.save(myclass);
  </code></pre>
  
  <p>and is saving the document in below format in couchbase</p>
  
  <pre><code>someKey {jsonObject}
  </code></pre>
  
  <p>now during retrieval, I am using a custom read converter as shown below</p>
  
  <pre><code>@ReadingConverter
      public static enum CouchbaseDocumentToMyClassConverter implements Converter&lt;CouchbaseDocument,MyClass&gt; {
          INSTANCE;
          @Override
          public MyClass convert(CouchbaseDocument source) {
              System.out.println(source.getPayload()); //this is returning lot of unwanted data like id, exp and payload for every nested json Object 
              //logic to instantiate MyClass obj 
              return new MyClass();
              }
          }
  </code></pre>
  
  <p>If i try to map the object directly to MyClass object instead of CouchbaseDocument  in the convert, then I am getting  <code>org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type java.lang.Object!</code>. Below is the complete stacktrace </p>
  
  <pre><code>2019-04-25 15:01:36.584 ERROR [{}] 33024 --- [http-nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type java.lang.Object!] with root cause
  rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: com.couchbase.client.java.document.RawJsonDocument.class
      at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.Subscriber.setProducer(Subscriber.java:211) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) ~[rxjava-1.3.8.jar:1.3.8]
      at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) ~[rxjava-1.3.8.jar:1.3.8]
  </code></pre>
  
  <p>Can someone please provide some insight on how this can be solved? I directly want to map the couchbase document to my POJO class object during retrieval.</p>