How to teach a Java EE app new NoSQL tricks

Sun dumped JavaEE Pet Store, but a bright developer modernized it, and we ported it to NoSQL with Couchbase

1 2 3 4 Page 4
Page 4 of 4

Using Couchbase views for data access
Couchbase uses views to simplify access to the data. They are generally a cross between a view and a stored procedures in the relational database world. In our application, we added a type field to the entity objects and set it to the type of object. For example, in the Order entity type="order". Then we created a view to pull back any document where the type was whatever we were looking for.

Figure 3: Our view code in Couchbase
Figure 3: Our view code in Couchbase

You can also drill into a document to have the view return anything in the document. An example of this is an item, which is embedded in the order line in the order document. We set up a view that query for the item for an ID. That is how the application pulls back an item relating to a specific product, which is in a specific category. This JavaScript code is very flexible.

One important note on views: When you first put a document into the database, it does not immediately become available to the view. It must first be saved to disk.

What we changed in the code
To change this application to work in Couchbase, a few files needed to change, primarily around the services. This is where, in the RDBMS version, there was a reference to the EntityManager. We decided to use the DbPopulator class to initialize the Couchbase connection because it is a singleton that starts at application start. We then use this connection similarly to the Entity Manager. The following is an example of some of the code changes that were necessary.

Derby repository calls
EntityManager em;
find : TypedQuery<Category> typedQuery = em.createNamedQuery(Category.FIND_BY_NAME, Category.class);
typedQuery.setParameter("pname", categoryName);
save : em.persist(category);
update : em.merge(category);
delete : em.remove(em.merge(category));

Couchbase repository calls
find : client.get(categoryName);
save : client.set(category.getName(), EXP_TIME, mapper.writeValueAsString(category));
update : client.replace(category.getName(), EXP_TIME, mapper.writeValueAsString(category));
delete : client.delete(category.getName());

Code changes were required for each of the CRUD operations for Customers, Orders, and Categories. Below is an example of the changes made to create a new customer.

The original code for customer creation
public Customer createCustomer(final Customer customer) {
if (customer == null) throw new ValidationException("Customer object is null");
return customer;

The Couchbase code for customer creation
public Customer createCustomer(final Customer customer) {

if (customer == null) throw new ValidationException("Customer object is null");

// Sets the customerId to the current time in millis
customer.setId(String.valueOf(new Date().getTime()));
try {
// Puts the document into the database.
// Uses customer login as the key since this should be unique
// EXP_TIME, Expiry time in seconds is set to 0, which means store forever.
// Uses the Jackson mapper to conver the customer to json
client.set(customer.getLogin(), EXP_TIME, mapper.writeValueAsString(customer));
} catch (Exception ex) {
return customer;

Our experience in switching this J2EE application from Apache Derby, JPA, and Hibernate to Couchbase demonstrated that the switch was fairly simple. The most difficult part was designing the schema, which is an issue when switching any application from an RDBMS to a NoSQL database. Still, designing a document database schema is generally easier than designing a relational database schema.

With the design behind us, it was simply a matter of switching the service classes to use the Couchbase client. Although this is a small application, implementing the Couchbase version was relatively easy and quick, which bodes well for other existing Java EE applications' ability to take advantage of NoSQL.

Michael Brush, Elizabeth Edmiston, and Deep Mistry, who are all developers at Open Software Integrators, contributed to this article.

This article, "How to teach a Java EE app new NoSQL tricks," was originally published at Keep up on the latest developments in application development and read more of Andrew Oliver's Strategic Developer blog at For the latest business technology news, follow on Twitter.

Copyright © 2013 IDG Communications, Inc.

1 2 3 4 Page 4
Page 4 of 4