Spring STS 2.6 JPA template project

Here are a few tips on using the JPA project template included in the Eclipse based SpringSource Tool Suite (STS) (currently at version 2.6) which is a free download from SpringSource.

Createe a JPA “utility” project (New->Spring Template Project then choose Simple Spring JPA Utility Project). This generates a lot more than just an empty project. It uses the standard Maven directory layout with fully functioning code and a complete pom.xml. The pom specifies the EclipseLink JPA implementation which is the “reference” implementation so you can try all of JPA 2.0‘s new tricks right away.

The project contains a JUnit test for the project: OrderPersistenceTests.java and an application context config (following COC (convention over configuration) it’s named OrderPersistenceTests-context.xml in the test/resources folder) which runs the test in an embedded hsqldb database.

There are two domain classes included. Order and Item (of course) to show a simple JPA @OneToMany/@ManyToOne relationship.

Nitpick
The code works but the design violates the Law of Demeter. Take a look at this line in the test. 

The domain objects shouldn’t force clients to do this. (You see this in the “official” Hibernate documents too.) Instead it should be hidden inside a method like this:

You can run the tests by right clicking on the project name and choosing Run As -> Maven test. (also available on the toolbar under Run)

Using the Hibernate JPA implementation

If you wish to use the Hibernate implementation instead of EclipseLink then edit your pom.xml. Add this repository.

Then find EclipseLink under dependencies. Comment it out and add this:

N.B. if you browse the JBoss repository (easy with the Maven repository view) you will see that the most current version for the EntityManager is 3.6.3.Final. Unfortunately the pom is invalid. The most recent valid pom in the repository is 3.5.0-Beta-2.

Then update your config file. Open src/main/resource/ then navigate to META-INF/spring to edit app-context.xml. Comment out (or delete) the EclipseLink line.

and insert the Hibernate version.

Then you need to configure the metamodel processing if you want to use Criteria Queries. The JBoss documentation has instructions.

Essentially you add a dependency and a line of configuration to the compiler plugin.

Using the Eclipse JUnit view

As an Eclipse user you may have gotten used to the JUnit view where you can easily run individual tests within a test case. (you can do the same thing in Maven too actually (mvn -Dtest=findAll MyTest); maybe you just like seeing the green bar on success). If you select the project or test case and choose Run as-> JUnit test you will have problems. The reason the Maven test execution work was because the template author was kind enough to provide this configuration.

Take a look at line 13. You need to add this to your Eclipse run configuration for the test. Go to the menu Run and choose Run Configurations. You should see the configuration for the failed test case (OrderPersistenceTests) you just tried. Choose the Arguments tag. Under VM Arguments enter -javaagent:spring-instrument-3.0.0.RELEASE.jar

Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.