couchdb - Simple CRUD with POJOs with CouchDB - couchdb tutorial - futon couch - nosql

Simple CRUD with POJOs

  • The below example explained for creating a simple POJO and doing standard CRUD operation on it.

Creating a simple POJO

we define a POJO as follows

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {
    @JsonProperty("_id") private String id;
    @JsonProperty("_rev") private String revision;
    private String name;

    public String getId() {
        return id;

    public String getRevision() {
        return revision;

    public String getName() {
        return name;

    public void setId(String id) { = id;

    public void setRevision(String revision) {
        this.revision = revision;

    public void setName(String name) { = name;
  • The annotation @JsonInclude(JsonInclude.Include.NON_NULL) tells jackson not to serialize null fields into JSON. So, for example, if the id property is null you wont have the id property in the resulting JSON at all.
  • Additionally, the @JsonProperty("_id") and @JsonProperty("_rev") annotations are directives, informing the serializer/unserializer what JSON properties to map these values to. Documents in CouchDB must have both a _id and a _rev field, thus all POJOs which you intent to persist in CouchDB, must include a id and revision properties as above. You are free to name your properties differently in the POJO, as long as you don't change the annotations.

For example,

@JsonProperty("_id") private String identity;
@JsonProperty("_rev") private String version;

Persisting new instances to CouchDB

  • Now, creating a brand new document, in the database is done as follows, presuming you have a valid CouchDbInstance , and that you wish to persist the document in a database named person
CouchDbConnector connector = dbInstance.createConnector("person", true);

Person person = new Person();
person.setName("John Doe");
  • Now, in this scenario, CouchDB will automatically create a new ID and Revision for you. If you dont want this, you can use
connector.create("MyID", person);   

Loading, updating and deleting documents

Presuming you have a CouchDBConnector instance ready, we can load instances of our POJO as follows

Person person = connector.get(Person.class, "id");

then we can manipulate it, and update it as follows

person.setName("Mr Bean");
  • Notice, if the revision of the document in couch, doesn't match the revision of the document you are sending, the update will fail, and you need to load the latest version from the database and merge your instances accordingly.
  • And finally, we should delete the instance, its as simple as

Related Searches to Simple CRUD with POJOs with CouchDB

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add to your ad blocking whitelist or disable your adblocking software.