Maven, Hibernate, JPA
Posted by billburke on January 7, 2008
I’m a maven noob. Wanted to integrate Hibernate 3 and JPA into a Maven 2 project. Not really into the hibernate maven plugin (anybody have good/bad experiences with it?) because I’m actually superstitious and like to control things myself, so I decided to integrate manually. After a little bit of trial and error and updating the JBoss maven repository here’s a sample pom.xml
<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>org.resteasy</groupId>
<artifactId>titan-cruise</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>titan-cruise</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate3</artifactId>
<version>3.2.4.SP1</version>
</dependency>
<dependency>
<groupId>hibernate-annotations</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.GA</version>
</dependency>
<dependency>
<groupId>hibernate-commons-annotations</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.0.0.GA</version>
</dependency>
<dependency>
<groupId>hibernate-entitymanager</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.2</version>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jboss-common-core</artifactId>
<version>2.2.0.GA</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.0.jboss</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.6.0.GA</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Anonymous said
Well if the hibernate-entitymanager would be configured like the one on mvnrepository.com you would also get persistence-api and could leave out a lot of dependencies and only add them if you really need a specific version (annotations or in general hibernate). Furthermore I prefer either a parent pom with version extracted or at least in the same pom. Like
[3.3.1.GA,)
billburke said
Thanks for the Maven lesson. I’ll have to figure out WTF that means now…
Anonymous said
damn of course I meant (forgot the code tag)
[3.3.1.GA,)
and in your dependencies like
hibernate-entitymanager
hibernate-entitymanager
${hibernate-em.version}
Anonymous said
(Pls remove previous comment)
[3.3.1.GA,)
hibernate-entitymanager
hibernate-entitymanager
${hibernate-em.version}
(A Preview mode would be very helpful sometimes
)
The version range simply defines minimum 3.3.1.GA or better.
Anonymous said
WTF
just imagine those are xml tags
properties
hibernate-em.version
(3.3.1.GA,]
hibernate-em.version
properties
and your dependency looks like
dependency
groupId
hibernate-entitymanager
groupId
artifactId
hibernate-entitymanager
artifactId
version
${hibernate-em.version}
version
dependency
Hopefully this time it works.
alrubinger said
There’s an example of extracting the versions into properties in the JBoss EJB3 Parent Build:
http://anonsvn.jboss.org/repos/jbossas/projects/ejb3/trunk/build/pom.xml
…this way, all versions are together at the top, and will be consistent among all children of the parent (subproject POMs will not designate a version number).
S,
ALR
Cédric Vidal said
Hi Bill,
You might want to give a look at the Maven dependency mechanism documentation:
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
It mentions transitive dependencies, which are very important to get right in order for maven to work properly.
You also might want to give a look at Mavenizer, a little tool I have created which helps in mavenizing third party libraries. It handles transitive dependencies:
http://mavenizer.sourceforge.net/
Kind regards,
Cédric
Fred said
I suffered also when I tried it first until I found that a nice guy in codehaus is maintaining a POM.
So now I have only:
>dependencies<
>dependency<
>groupId<junit>/groupId<
>artifactId<junit>/artifactId<
>version<3.8.1>/version<
>scope<test>/scope<
>/dependency<
>dependency<
>groupId<org.codehaus.mojo.hibernate3>/groupId<
>artifactId<maven-hibernate3-jdk15>/artifactId<
>version<2.0-alpha-1>/version<
>/dependency<
>dependency<
>groupId<org.apache.derby>/groupId<
>artifactId<derby>/artifactId<
>version<10.2.2.0>/version<
>/dependency<
>/dependencies<
I used JavaDB and so build a JUnit test that can run without container.
The code full runnable code is here, and the blog entry here.
David Peers said
Thanks, this blog aided me somewhat in narrowing down some issues with the latest version, Why do they always seem to leave out vital documentation when they release a new version? It may be trivial to them but not to me. I’m sure we’re not alone.