Kundera with Couchdb

chhavigangwal edited this page Jun 5, 2015 · 11 revisions
Clone this wiki locally


Couchdb is an open source database that enables easy access of data over web. It uses JavaScript Object Notation (JSON) for data storage, a lightweight format based on a subset of JavaScript syntax. Couchdb can be easily downloaded and installed with help of instructions available on its site.


Being a JPA provider, Kundera provides support for Couchdb. It allows to perform CRUD and query operation over Couchdb using JPA specifications. Let's look into how to use Kundera over Couchdb as database.

  • Entity
public class PersonCouchDB

    /** The person id. */
    @Column(name = "PERSON_ID")
    private String personId;

    /** The person name. */
    @Column(name = "PERSON_NAME")
    private String personName;

    /** The age. */
    @Column(name = "AGE")
    private Integer age;
  // setters and getters. 

Persistence unit configuration

 <persistence-unit name="couchdb_pu">
                        <property name="kundera.nodes" value="localhost" />
                        <property name="kundera.port" value="5984" />
                        <property name="kundera.keyspace" value="couchdatabase" />
                        <property name="kundera.dialect" value="couchdb" />
                        <property name="kundera.client" value="couchdb" />
                        <property name="kundera.ddl.auto.prepare" value="create" />
                        <property name="kundera.client.lookup.class"
                                value="com.impetus.client.couchdb.CouchDBClientFactory" />
                        <property name="kundera.cache.provider.class"
                                value="com.impetus.kundera.cache.ehcache.EhCacheProvider" />
                        <property name="kundera.cache.config.resource" value="/ehcache-test.xml" />


final String nodeId = "node1";
final String originalName = "vivek";
PersonCouchDB object = new PersonCouchDB();

Node node = new Node(nodeId, PersonCouchDB.class, new TransientState(), null, ROW_KEY);

PersonCouchDB result = (PersonCouchDB) client.find(PersonCouchDB.class, ROW_KEY);

// Find by key and now row key
String findByIdAndAge = "Select p from PersonCouchDB p where p.personId=:personId AND p.age=:age";
query = em.createQuery(findByIdAndAge);
query.setParameter("personId", ROW_KEY);
query.setParameter("age", 32);
  • Range
 // Find by greater than and less than clause over non row key
String findAgeByGTELTEClause = "Select p from PersonCouchDB p where p.age <=:max AND p.age>=:min";
query = em.createQuery(findAgeByGTELTEClause);
query.setParameter("min", 32);
query.setParameter("max", 35);
  • Between clause
 // find by between over non rowkey
String findAgeByBetween = "Select p from PersonCouchDB p where p.age between :min AND :max";
query = em.createQuery(findAgeByBetween);
query.setParameter("min", 32);
query.setParameter("max", 35);
  • Select specific field (without where clause)
 // select specific field AGE
String findAge = "Select p.age from PersonCouchDB p";
query = em.createQuery(findAge);
  • Perform aggregations (without where clause)

Kundera supports COUNT MAX MIN SUM and AVG aggregation queries with CouchDB. Currently the support is enabled for queries without WHERE clause only.

 // run aggregation like MIN SUM COUNT etc
String findMinAge = "Select MIN(p.age) from PersonCouchDB p";
query = em.createQuery(findMinAge);
String findSumAge = "Select SUM(p.age) from PersonCouchDB p";
query = em.createQuery(findSumAge);
String findCountAge = "Select COUNT(p.age) from PersonCouchDB p";
query = em.createQuery(findCountAge);

You may also refer junits for more examples.


  • Enable SELECT query on a particular column with WHERE clause
  • Nested AND clause support