Sunday, April 17, 2011

Setting up a Spring Data JPA Project

This post is about setting up a Maven Project based on Spring Data JPA. I spent a few hours today nailing it down. Hopefully it saves someone some time to bring a project up quickly.

- Spring Framework based application
- makes use of Spring Data JPA for easy Repositories
- makes use of JPA 2.0 persistence with EcliplseLink
- JUnit4 and spring-test
- Logging using SL4J to Log4J

For the impatient, I uploaded a sample project on github: spring-data-jpa-trial

You can also download the tar ball or zip file from these links.

I started with a blank Maven Project created with STS 2.5.2.SR1 created using
the New Maven Project wizard checking the Create a simple project(skip archetype selection).

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
</project>

Added some entries to the pom file to setup 1.6
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

Setup spring-data-jpa dependency
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.0.M2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://maven.springframework.org/milestone</url>
</repository>

Setup persistence dependecies
<!-- Persistence dependencies -->
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.framework.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.2.0</version>
<scope>runtime</scope>
</dependency>
<repository>
<id>EclipseLink Repo</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>

Setup logging dependencies as discussed in the Spring Framework Reference Docs
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${sl4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${sl4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>

Setup Testing Dependencies
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>        
<argLine>-javaagent:spring-instrument-3.0.5.RELEASE.jar</argLine>
</configuration>
</plugin>

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.

3 comments:

  1. Hi, I couldn't compile your project using sts.Error:

    The import org.springframework.beans.factory.annotation.Autowired cannot be resolved. Why you don't need spring-beans in the pom.xml?

    ReplyDelete
  2. Hi

    Excellent article - very useful.

    Just one question: Why is that that the spring-instrument jar can not be picked up from the Maven dependencies?

    Thanks

    Jan

    ReplyDelete