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.
The code works but the design violates the Law of Demeter. Take a look at this line in the test.
1 |
order.getItems().add(new Item()); |
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:
1 |
order.add(new Item()); |
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.
1 2 3 4 |
<repository> <id>JBoss Repo</id> <url>https://repository.jboss.org/nexus/content/repositories/releases/</url> </repository> |
Then find EclipseLink under dependencies. Comment it out and add this:
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>org.hibernate.java-persistence</groupId> <artifactId>jpa-api</artifactId> <version>2.0-cr-1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.5.0-Beta-2</version> </dependency> |
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.
1 |
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> |
and insert the Hibernate version.
1 |
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>1.1.1.Final</version> </dependency> ... <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> <compilerArguments> <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> </compilerArguments> </configuration> </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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.8.1</version> <configuration> <!--forkMode>pertest</forkMode --> <includes> <include>**/*Tests.java</include> </includes> <excludes> <exclude>**/Abstract*.java</exclude> </excludes> <argLine>-javaagent:spring-instrument-3.0.0.RELEASE.jar</argLine> </configuration> </plugin> |
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