Wednesday, June 5, 2013

How to use Hibernate OGM in Jboss AS 7

For those who still haven't heard about Hibernate OGM (what !! That exists :) ), it means Object Grid Mapper. The basic idea is to do same job that ORM do with SQL databases but with NoSQL data stores.
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 modules

The 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 application

You 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
  1. Get rid of the jta-data-source tag (or all the javax.persistence.jdbc.* properties) 
  2. Get rid of all the SQL properties (we don't need them anymore)
  3. Switch from the standard provider ( org.hibernate.ejb.HibernatePersistence ) to the OGM provider ( org.hibernate.ogm.jpa.HibernateOgmPersistence )
  4. 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
  5. 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 :)

Cheers,
Guillaume

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Guillaume,

    I read your post and tried to do the same thing but I always get the error:

    Failed to get adapter for persistence provider 'org.hibernate.ogm.jpa.HibernateOgmPersistence'

    I don't know what I did wrong. I use wildfly-8.0.0.Final, do I need to configure something in standalone.xml?
    Thank you.

    Regards
    Loi

    ReplyDelete
  3. Hi Guillaume,

    Im having the same problem as sanitu
    Any idea where to look to solve the problem of the error Failed to get adapter for persistence provider 'org.hibernate.ogm.jpa.HibernateOgmPersistence'


    Than you

    ReplyDelete
  4. Enjoyed your approach to explaining how it works, hope to see more blog posts from you. thank you!

    Hibernate Online Training | Java Online Training


    Hibernate Training in Chennai Java Training in Chennai

    ReplyDelete