The very first data store that has been implemented was Infinispan, then came Ehcache followed by MongoDB that I have launched approximately 1 year ago (already 1 year).
As a Java developper, one thing that I like about Hibernate OGM, is that it allows me to reuse what I know about JPA 2 (mapping, Entity Manager, etc) in a NoSQL context.
Therefore, OGM is definitely not a panacea but at least in a Java EE environment it's very useful to have CRUD capabilities out of the box without having to reinvent the wheel. And beside OGM you can use powerful tools for searching like Hibernate Search or ElasticSearch. But that's not the point today.
JBoss AS 7 is using a system called modules on which are based most of the subsystems like, EJBs, Web Services, JPA, etc. For more information about modules, here is a link to the Developer Guide.
Because Hibernate OGM is dealing with persistence stuff, you may have figured out that we first need to customize the JPA module.
The modulesThe easiest way to make OGM a JPA module is to use the files we build for you. To do that, first get the source files (by cloning / forking the Github repository). You have to build at least the hibernate-ogm-core and hibernate-ogm-modules (using mvn clean install -pl hibernate-ogm-core,hibernate-ogm-modules -s settings-example.xml). After that, in the target folder (of the hibernate-ogm-modules module) you'll find a zip archive that contains everything. You just have to copy it (and replace if needed) into the module folder %JBOSS_HOME% /modules/.
So the %JBOSS_HOME% / modules/org/hibernate/ should contain a new "ogm" folder. Inside of it, you have the module.xml file which declares OGM as a JPA module, the hibernate-ogm-core jar, the hibernate-ogm-infinispan dependency and 2 folders (one for the Ehcache implementation and one for the MongoDB implementation both declared as module).
That's it ! Now you can use OGM in your Java EE application. So let's see how to do that.
The applicationYou can find a whole application I use for JUG presentations, on my Github repository.
When you write an application that uses Hibernate OGM there are some specific configurations you have to know.
Let's start with this classic persistence.xml file
To transform it into an OGM application
- Get rid of the jta-data-source
tag (or all the javax.persistence.jdbc.* properties)
- Get rid of all the SQL properties (we don't need them anymore)
- Switch from the standard provider ( org.hibernate.ejb.HibernatePersistence ) to the OGM provider ( org.hibernate.ogm.jpa.HibernateOgmPersistence )
- Tell OGM which data store provider to use by adding a property called: hibernate.ogm.datastore.provider and set it to either INFINISPAN, either EHCACHE or MONGODB
- Tell AS to use Hibernate 4 integration classes (because OGM is depending on ORM 4.x), use the property jboss.as.jpa.adapterModule set to org.jboss.as.jpa.hibernate:4
Finally the persistence.xml could look like:
The very last thing is to inform the container that our application is depending on OGM (in that case, the application will use ogm-core and ogm-ehcache module) by adding a dependencies line into the manifest.mf file:
This is it ! you can now use Hibernate OGM in your application with the good old @Entity, entityManager.persist() and so on.
If you want to use MongoDB instead of Ehcache, here is an example of persistence.xml file:
If you want to have more information about the specific MongoDB configuration property you can refer to the official documentation.
As always, feedback and comments are more than welcome :)