Powered By Blogger

Monday, September 12, 2011

Integrating Hibernate with Maven


I was trying to integrate Hibernate with maven for quite a long time but gave up everytime due to long list of dependency that had to be configured while working with Hibernate. Moreover, no website or blog was clearly able to tell where to find the latest repository that could download all the dependencies. I would have expected Hibernate guys to maintain the dependencies and allowed any user to just use a single <dependency> element to be added to POM file and that would do the magic coz that's what maven is for -- resolving transitive dependency.
Lately JBoss guys have starting maintaing hibernate dependency on https://repository.jboss.org/nexus/content/groups/public. Though using this repository you don't have to add multiple dependencies like hibernate-core, hibernate-commons, hibernate-annotations... and so on.
Adding the following dependency does most of the magic.
<dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-entitymanager</artifactId>
       <scope>compile</scope>
       <version>3.4.0.GA</version>
</dependency>
But still you have to configure the logging dependency. In this case it requires sl4j-log4j12. So you might just add a dependency of slf4j-log4j12 with current GA version as below:
<dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12r</artifactId>
       <type>jar</type>
       <scope>runtime</scope>
       <version>1.5.2</version>
</dependency>

But as soon as you compile and there's a boooom....#*%^^%#Exception occured.
So what went wrong? The slf4j-log4j12 version that you just used is not compatible with the Hibernate version you are using. Why can't the JBoss guys maintain this in the repo and do away with it?

If you get this, you can find out the dependency using the command mvn dependency:tree

In this case, I had to use 1.4.2 version of slg4j-log4j12 API.
Pre-requisites: Maven is downloaded and configured on your m/c.