Monday, June 17, 2013

How to use Hibernate OGM in Glassfish 3 ?

After having posted "How to Hibernate OGM in JBoss AS 7 ?", I continue with the reference Java EE 6 applications server, Glassfish in its third version. For this post I use the same code base as for the previous post.

Setting up the application server

You probably know that Glassfish is shipped with Eclipse Link for the JPA2.0 implementation, so the first part is to make Hibernate ORM 4 available. The easiest way is to copy into the %GLASSFISH_HOME%/glassfish/domains/domain1/lib/ folder the required JARs to run Hibernate ORM.
Those JARs are:
  • hibernate-core
  • hibernate-entitymanager
  • hibernate-commons-annotations
  • hibernate-validator
Because OGM is depending on ORM, to avoid any version conflicts I choose to use (as far as possible) the same version of Hibernate JARs that OGM has been built with: 4.2.0 (just hibernate-commons-annotations is in 4.0.2). And that's all, Glassfish is ready.

The application

To use Hibernate ORM, you just have to specify the provider is:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
For the OGM part, we just have to copy OGM JARs into the lib folder (mentioned above), those JARs are:

  • hibernate-ogm-core
  • hibernate-ogm-ehcache
  • hibernate-ogm-mongodb
  • ehcache-core
  • mongo-java-driver
For more information about the libraries version, I suggest you to refer to the OGM pom file. If you want to use Infinispan rather than EhCache or MongoDB, just copy both hibernate-ogm-infinispan and infinispan-core .

To switch the application on "OGM mode", it's always the same steps:
  1. use the HibernateOgmPersistence provider
  2. declare the data store provider you want to work with (hibernate.ogm.datastore.provider)
  3. if needed, configure the right properties (host, port, etc. )
At this point, you can run the demo and if it works you'll find in the logs:

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

Tuesday, June 4, 2013

Trucs et astuces avec Java et MongoDB sur Raspberry Pi @ Devoxx FR 2013

En mars dernier, j'ai eu la chance que dis-je l'honneur d'être sélectionné pour un Tools in action lors de session 2013 de Devoxx France. Si vous ne connaissez pas Devoxx et/ou Devoxx France, il s'agit des plus grandes conférences indépendantes sur l'écosystème Java pour respectivement l'Europe et la France. Lors de cette session, j'ai présenté quelques trucs à savoir pour faire du Java et utiliser MongoDB sur un Raspberry Pi.

Si vous n'avez pas pu venir me voir, voici mes slides.