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:
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 set to

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 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 :)


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.

Friday, May 31, 2013

MongoDB as a cache store for Infinispan ?!

For 1 year now, I'm working as a contributor on Hibernate OGM. Few months ago, Sanne asked me to create a MongDB cache store for Infinispan. Now thanks to Sanne and Mircea I have made it possible and it will be available with the 5.3 release of Infinispan.
The next question you may have, is "Awesome how can I use it ?". Well it's pretty simple, there are actually 2 ways, the xml file and the programmatic api.

Here is a snippet of the XML configuration:
Followed by a snippet of the programmatic API:
Of course, you need to have an external MongoDB instance running somewhere :) If you want to have some MongoDB features available in Infinispan, don't hesitate to contact me or the Infinispan team. And of course, feedbacks are more than welcome.

Wednesday, March 20, 2013

Get started with Raspberry PI

Since I bought a Raspberry Pi, I'm looking for a sample project but as Java developer I needed to install some stuff to first.
I figure out few issues and then decided to share it first at Devoxx France where I'm going to give a 30 minutes speech.

So let's move on and begin with the Raspberry Pi. For those who don't know about it, it's a credit card sized computer created by the Raspberry Pi foundation in order to help organization (like schools) to teach computer programming to kids.

What's inside ?
The idea is to keep it cheap so in the first version (called model A) there are:

  • 700MHz CPU (ARM11)
  • 256Mo for the RAM
  • 1 USB 2.0 port 
  • 1 RCA video port
  • 1 3.5 jack audio connector
  • 1 GPIO pin
  • 1 SD card slot 
  • 1 HDMI connector
In the second version (model B), the ram has been enhanced to 256Mo, one USB port has been added so as Ethernet (10/100) connector.

Let's install the OS
Because I'm not a linux expert, I didn't want to go far from the trenches, I choose to install Raspbian - a Debian modified to fit to the Raspberry Pi. The installation is really simple, insert the SD card into your computer, format it in FAT32 and with an image writer (Win32DiskImager on Windows, PiWriter on OSX and ImageWriter on Linux).

I first decided to use the Soft float point version - to use the OpenJDK JVM version later - but I don't know why, it didn't reboot (after having unplugged it from the power supply). I don't have this issue with the Hard Float point version.

Next step ? Insert the SD card into the Raspberry Pi, plug a keyboard, a screen (both are mandatory at least for the first boot) and the power supply.
The very first thing you'll see is the raspi-config utility tool

This tool lets you configure the keyboard, the locale the default user, etc. To be able to use the complete capacity for your SD card, you have to run the "expand_rootfs" tool. I also suggest to enable the SSH server  so you won't need the screen and the keyboard.
If you need to change some configuration later, you can at anytime use the following command:

sudo raspi-config

Once you have finished this part you'll have to reboot the Raspberry Pi and you are ready to rock and roll.
Last thing that can be useful, the default user is called "pi" and his password is "raspberry".