Rupeal
RSSLinkedInTwitterFacebook

Java Persistence API, Hibernate Annotations and Maven2

Setting up a project with Hibernate Annotations using Maven2 is quite simple… or at least it should be. You should just have to set up your pom.xml file with the following dependencies:


<project>

...
<dependencies>
<dependency>
<groupid>hibernate</groupid>
<artifactid>hibernate-annotations</artifactid>
<version>3.2.1ga</version>
</dependency>
</dependencies>
...
</project>

And this is what I expected to start using Hibernate Annotations on my project. But if you try to compile your project you’ll get the following:


...
Missing:
----------
1) hibernate:hibernate-annotations:jar:3.2.1ga


Try downloading the file manually from the project website.

Then, install it using the command:


mvn install:install-file -DgroupId=hibernate -DartifactId=hibernate-annotations

-Dversion=3.2.1ga -Dpackaging=jar -Dfile=/path/to/file


Path to dependency:
1) ...
2) hibernate:hibernate-annotations:jar:3.2.1ga

So, Maven couldn’t resolve our dependency but at least gave us a hint on how to solve the problem. After downloading the Hibernate Annotations 3.2.1GA file and unzip it, we used the suggested command to solve the problem:

mvn install:install-file -DgroupId=hibernate -DartifactId=hibernate-annotations -


Dversion=3.2.1ga -Dpackaging=jar -Dfile=hibernate-annotations.jar

After successfully installing the package, the project successfully compiled!

The problem is that usually we don’t want to use Hibernate Annotations by itself, we want to use the Java Persistence API (JPA) which is included on the javax.persistence package. So by including another dependency on our pom.xml like this:

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>ejb</artifactId>
<version>3.0-public_review</version>
</dependency>

we get a similar error as we did when we tried to use Hibernate Annotations. Maven can’t resolve the dependency, but this time it even give us a hint where to dowloand the missing library:

Missing:
----------
1) javax.persistence:ejb:jar:3.0-public_review

Try downloading the file manually from:

http://prdownloads.sourceforge.net/hibernate/hibernate-annotations-3.1beta5.tar.gz?download

Then, install it using the command:
mvn install:install-file -DgroupId=javax.persistence -DartifactId=ejb
-Dversion=3.0-public_review -Dpackaging=jar -Dfile=/path/to/file

If you notice, the URL where we can download the missing library is pointing to Hibernate Annotations! Since we already have downloaded Hibernate Annotations and it’s an earlier version than the suggested we just have to find out where the jar containing the javax.persistence is. And it’s on the ejb3-persistence.jar under the lib directory.

Issuing the installation command on the file, and you’re ready to go! Just type the following to install the jar on the local Maven repository:

mvn install:install-file -DgroupId=javax.persistence -DartifactId=ejb
-Dversion=3.0-public_review -Dpackaging=jar -Dfile=ejb3-persistence.jar

That’s it! Not so fun using Maven when the dependencies aren’t resolved “automagically” like the most, but it still is a fantastic tool for managing your Java projects!

Rui Alves on February 14th, 2007

Software Development

3
 

3 comentários

  • Benito d’ALMEIDA

    14 December 2007

    Hi, i thinks that you should use sensitive key for worlds.
    write groupId at groupid place and try it.
    Make the better configuration to your setting.xml and and some repository maven (where ther is more jars source to the repository maven url config

    yours sincerely

     
  • AndrewBoldman

    4 June 2009

    da best. Keep it going! Thank you

     
  • Summer Camps

    30 October 2010

    You you should edit the page subject Java Persistence API, Hibernate Annotations and Maven2 to something more better for your subject you make. I enjoyed the post even sononetheless.

     

Deixe um Comentário