org.ektorp.DbAccessException if POJO not extended from CouchDbDocument

415 Views Asked by At

My java application connects to CouchDB using the Ektorp persistence api http://ektorp.org/reference_documentation.html to store and retrieve objects. I'm trying to store a simple bean with few String properties and using CouchDbConnector.create(mybean) to create the document and CouchDbConnector.get(mybean.class,doumentid) to retrieve the document. When I extend my bean with CouchDBDocument as documented here http://ektorp.org/reference_documentation.html#d100e355, it works well while creating and retrieving the document but if I don't extend it with CouchDBDocument and annotate the "id" and the "revision" attributes as listed here http://ektorp.org/reference_documentation.html#d100e31, then create document works but retrieve document throws this exception.

nested exception is org.ektorp.DbAccessException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "_id"

I tried various options by changing the name "id" to "_id" but it do not work. Also, I notice that when I don't extend my bean with CouchDbDocument then in the couchdb, an "id" and "revision" fields are created in the document along with "_id" and "_rev". These fields are not created when I extend CouchDbDocument. Any idea what's wrong here?

2

There are 2 best solutions below

2
On

The id field must serialize to "_id". Write a unit test and verfify that you class can be written to json and read from json with the fields named _id and _rev.

0
On

It's been a while the question was asked but I'm answering in case someone has the same problem.

The right Maven dependency:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.10.2</version>
</dependency>

And in your POJO replace import org.codehaus.jackson.annotate.*; by import com.fasterxml.jackson.annotation.*;