Adding Hibernate dependency into Maven Project

February 9th, 2009  | Tags: , ,

Searching in the internet, I didn’t find an useful page explaining how to add hibernate depency into Maven. Although it seems an easy setup, I faced some problems that I’ll describe below.

First of all, I’d like to say that I’m using M2ECLIPSE plugin for Eclipse. There’s a simple instruction of how to install and use it here.

Even if you aren’t using M2ECLIPSE, you can follow this topic. I’ll show up the pom.xml file generated by the plugin.

Creating the PROJECT through Maven 2

The first thing to do is to create the project. You can use the Maven-archetypes to do that. There’re archetypes for desktop project, web projects, struts 2 projects and so on. Usually when you use the archetypes, it brings for you by default, the JUnit dependency already installed. Anything like this:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8</version>
      <scope>test</scope>
    </dependency>

Note: You can edit this entry and change the JUnit’s version to 4.5 (the latest).

Adding Hibernate dependency

You can add the hibernate dependency either editing the pom.xml file or adding it through M2ECLIPSE. In POM.xml, simple add the following:

    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate</artifactId>
    	<version>3.2.6.ga</version>
    </dependency>

Through M2ECLIPSE, right click on POM.xml file, go to Maven -> Add Dependency. Type org.hibernate into input file and double click into org.hibernate result. That process will add the same code above into POM.xml

Note: When I’ve done it, I had problem with the jta 1.0.1B jar file. To fix it, I had to download it and save it into jta folder manually.

Adding Hibernate-Annotations

If you’re a normal person you won’t waste time editing XML, because of this you’re going to use Annotations instead, am I right? Hence, you must add the Hibernate-annotation dependency in your project as well.

To do that, edit the pom.xml file and add the following:

    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-annotations</artifactId>
    	<version>3.4.0.GA</version>
    </dependency>

You can also add it through M2ECLIPSE. Repeat the process above and choose the hibernate-annotations rather than hibernate.

Adding slf4j-log4j12 dependency

Here is the trick. Although the two steps above are correct, you still need to add another dependency. org.hibernate adds the API slf4j, but it adds the wrong version. You must add the slf4j-log4j12 that is required by hibernate. To do that, as usual, add the following code into pom.xml:

    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-log4j12</artifactId>
    	<version>1.5.2</version>
    </dependency>

or you can do the same through M2ECLIPSE.

Full POM.XML file

After all changes, you should have a POM.xml file like below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TestHibernate</groupId>
  <artifactId>Testhibernate</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Testhibernate</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate</artifactId>
    	<version>3.2.6.ga</version>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-annotations</artifactId>
    	<version>3.4.0.GA</version>
    </dependency>
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-log4j12</artifactId>
    	<version>1.5.2</version>
    </dependency>
   </dependencies>
</project>

If you’re using the M2ECLIPSE, you can see all dependencies through its editor. Double click into POM.XML file and go to Dependency Hierarchy tab. You should see a screen like below:

Conclusion

As described above, the trick in this integration with the org.slf4j dependency. After you solve that problem, the integration works perfect. Both frameworks are awesome and this post teachs you in the integration of them.

I hope this topic be useful for anyone. If you have any question or comment about it, fell free to leave your message below.

No comments yet.
TOP