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

RELATED TOPICS
Page 3 of 4

Couchbase has B-tree-based indexes. You can index anything from entire views to embedded documents. However, it lacks in geospatial indexes (currently available in experimental mode only), although this becomes an issue solely if you are working with location data. Couchbase also does not have in-place updates. This is not a huge sticking point because the working set remains in memory all the time, so the updates are superfast.

Couchbase does not have any concept of capped collections. This is only an issue if you are working mainly with log data analysis. Couchbase maintains the working set in memory, but you can have much more data than the amount of memory. Although Couchbase 2.0 has a relatively higher cache miss rate, its developers are working to optimize this in the next release.

The data models for the Pet Store in NoSQL

The Java Pet Store application was originally deployed in Apache Derby using Hibernate and JPA. Because Derby is an embedded implementation, we switched the configuration to use MySQL. This enabled us to have an in-depth look at the relational schema design.

Figure 2: The relational scheme design for the JavaEE Pet Store
Figure 2: The relational scheme design for the JavaEE Pet Store

The application is being driven primarily by two events:

  1. When a new customer registers
  2. When a new order is created by a customer

We built the Couchbase documents around these two events: Customer and Order. These documents were designed to contain related entities as embedded documents. We also created a third document type, Category, to store inventory information: categories, products, and items. This design decision enabled the creation of separate indexes (or views) so that they can be fetched quickly. This also provides examples of both linked and embedded documents.

JSON example for
Customer
JSON example for
Order
JSON example for Category
{
"id":"customer_marc",
"type":"customer",
"login":"marc",
"password":"marc",
"firstname":"Marc",
"lastname":"Fleury",
"telephone":null,
"email":"marc@jboss.org",
"homeAddress":{
"street1":"65 Ritherdon Road",
"street2":null,
"city":"Los Angeles",
"state":null,
"zipcode":"56421",
"country":"USA"
},
"dateOfBirth":1363794557891,
"age":null
}
{
"id":"Marc",
"type":"order",
"orderDate":null,
"customer":{
"id":1,
"login":"marc",
"password":"marc",
"firstname":"Marc",
"lastname":"Fleury",
"telephone":null,
"email":"marc@jboss.org",
"homeAddress":{
"street1":"65 Ritherdon Road",
"street2":"",
"city":"Los Angeles",
"state":"",
"zipcode":"56421",
"country":"USA"
},
"dateOfBirth":1363722361660,
"age":0
},
"orderLines":[
{
"id":null,
"quantity":1,
"item":{
"id":"item_Goldfish_Male Puppy",
"type":"item",
"name":"Male Puppy",
"description":"Lorem ...",
"unitCost":12,
"imagePath":"fish2.jpg"
}
},
{
"id":null,
"quantity":1,
"item":{
"id":"item_Angelfish_Large",
"type":"item",
"name":"Large",
"description":"Lorem ...",
"unitCost":10,
"imagePath":"fish1.jpg"
}
}
],
"deliveryAddress":{
"street1":"65 Ritherdon Road",
"street2":"",
"city":"Los Angeles",
"state":"",
"zipcode":"56421",
"country":"USA"
},
"creditCard":{
"creditCardNumber":"1234",
"creditCardType":"VISA",
"creditCardExpDate":"03/15"
},
}
{
"id":"category_Birds",
"type":"category",
"name":"Birds",
"description":"Any of ...",
"products":[
{
"id":"product_Amazon Parrot",
"type":"product",
"name":"Amazon Parrot",
"description":"Great companion for up to 75 years",
"items":[
{
"id":"item_Male Adult",
"type":"item",
"name":"Male Adult",
"description":"Lorem ...",
"unitCost":120,
"imagePath":"bird2.jpg"
},
{
"id":"item_Female Adult",
"type":"item",
"name":"Female Adult",
"description":"Lorem ...",
"unitCost":120,
"imagePath":"bird2.jpg"
}
]
},
{
"id":"product_Finch",
"type":"product",
"name":"Finch",
"description":"Great stress reliever",
"items":[
{
"id":"item_Male Adult",
"type":"item",
"name":"Male Adult",
"description":"Lorem...",
"unitCost":75,
"imagePath":"bird1.jpg"
},
{
"id":"item_Female Adult",
"type":"item",
"name":"Female Adult",
"description":"Lorem ...",
"unitCost":80,
"imagePath":"bird1.jpg"
}
]
}
]
}

As the application is deployed, the Categories, Products, and Items are generated in the database by the database populator class. When a new customer goes to the home page, he or she has the option to sign in or register. At that point, a new customer document is created. The customer can then browse the existing categories, create an order, and save it. When the order is saved, a new order document is created with order details. The associated items are added as embedded documents in the order.

RELATED TOPICS
| 1 2 3 4 Page 3
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies