The new / interesting stuff is the contactMethods property. If you are familiar with Hibernate / JPA then most of this should look pretty familiar to you. class ) public class Contact Integer id (nullable = false ) public String name (type = "hstore" (columnDefinition = "hstore" ) public Map contactMethods = new HashMap ( ) } Package import import import import import import import import import (name = "hstore", typeClass = HstoreUserType. So lets take a look at the simple Contact Entity: The “Contact Methods” are a good use of a schema-less, key-value pair column because it avoids the cumbersome alternatives: putting that information into a separate table or trying to create a model object that has all of the possible “Contact Methods”. This application stores “Contacts” that have a name but also can have many “Contact Methods” (e.g. Via jQuery / Ajax the client communicates to JSON services exposed via a Spring MVC Controller. The whole client-side is in a plain ‘ole HTML file. The client technologies will be jQuery and Bootstrap and there is a strict seperation between the client and server via RESTful JSON services. Spring is configured via Java Config for the main stuff, the web stuff, and the database stuff. Embedded Jetty is started via a plain ‘ole Java application that sets up Spring MVC. This demo app uses Maven to define the dependencies.
I won’t go through that code here but you can find everything in the GitHub repo for my demo project. (The same thing can also be done with Rails, Django, and many other technologies.) To add Hibernate support for hstore I found a fantastic blog about “ Storing sets of key/value pairs in a single db column with Hibernate using PostgreSQL hstore type“. In this example the server technologies will be Java, Spring, and Hibernate. One benefit of this approach is that the same datastore can be used for both the SQL and the NoSQL data. This method is a mix of NoSQL inside SQL. This article covers the pieces of a simple web application which uses regular SQL and PostgreSQL’s hstore for key value pairs. Lets walk through how you can combine a regular SQL model with a key-value NoSQL model using Java, Spring, Hibernate, and PostgreSQL. The general term to describe this is Polyglot Persistence and there are many ways to accomplish it.
Now the challenge is in deciding which persistence model fits best with each domain in a system and then combining those models in a cohesive way. With NoSQL we no longer must try to shoehorn everything into a relational model. The primary gift the NoSQL movement has given us is the variety of options we now have for data persistence. There are many benefits to schema-less NoSQL datastores, but there are always trade-offs.