Recommended Way To Structure A Spring 3, JPA 2, and Maven Web Application


I've created an example application that uses Struts 2, Spring 3, JPA 2, Hibernate 3.5, and Maven. I need some advice from Java web application developers who are using Spring 3, JPA 2, and Maven. The programming team I'm part of is moving to JPA. We plan to use Hibernate as the JPA provider but we want to keep the dependency on Hibernate to a minimum. To reduce our direct dependency on Hibernate we are using the Spring - JPA configuration described in chapter 4 of Spring Persistence with Hibernate and in section 13.5 of the Spring 3 documentation (see references below). I've run into an issue with configuring my applications to follow this structure and I'd appreciate any advice from Java developers more experienced with Spring, JPA, and Maven.

Example Application

Please download the example application. It is a standard Maven project, created using Eclipse 3.6. The application uses an in-memory database (HSQL) that is setup by the Spring applicationContext.xml class. See files schema.sql and test-data.sql in src/main/resources for the schema created during the application's startup and the initial records inserted into the table's database. Using the in-memory database means there is no need to setup and run a separate database server for the example application.

You can import this project into Eclipse by using Eclipse's File - Import feature. In the project's root folder is a README.txt file that explains how to run the application. If you're not using Eclipse with the Maven 2 plugin, you can unzip the archived download and view the source code in any text editor. If you are using a Java IDE that enables importing of Maven projects you should be able to import the unzipped project.

You can also run the application using Maven directly if you have downloaded and installed Maven on your computer. Open a terminal (command) window and navigate to the project's root folder (the folder where pom.xml resides). Then in the terminal window issue these commands:

  1. mvn clean
  2. mvn test (All tests should pass successfully)
  3. mvn jetty:run

When you see [INFO] Started Jetty Server open a web browser and navigate to http://localhost:8080/strutsspringjpaexample/index.jsp. Enter 1, 2, or 3 in the employee id text field and click submit.

To stop the Jetty web server type CTRL-C in the terminal window.

Application Structure

Based on the goal of reducing the direct dependency on Hibernate, the application uses the configuration described in section 13.5 of the Spring 3.0 documentation (reference 2) to setup Spring integration with JPA. The Spring configuration file creates an LocalContainerEntityManagerFactoryBean object and provides it with the data source, location of the persistence.xml file, and the name of the persistence unit to use.

The persistence.xml file defines the persistence unit including what classes are to be JPA entities, what is the JPA provider (Hibernate), and the properties for the JPA provider.

The model classes use JPA 2 annotations to identify how a model class is related to a database table.

The data access classes use Spring's @Repository annotation to mark the class as one Spring should automatically create an instance of in the Inversion of Control container. In the Spring configuration file are these nodes which are used with @Repository:

<!-- scans the classpath for annotated components (including @Repository) that will be
auto-registered as Spring beans -->

<context:component-scan base-package="" />

<!-- This will ensure that hibernate or jpa exceptions are automatically translated into
Spring's generic DataAccessException hierarchy for those classes annotated with Repository
For example see PersonDaoJpa-->

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

The data access classes also use the JPA @PersistenceContext annotation to mark which method Spring should use to inject the EntityManager object into the data access class. The data access classes then use the EntityManager object to create queries that will run against the database and return model objects.

Note that the data access classes do not extend any other class and are not tied to Hibernate. The model and data access classes only use JPA 2 annotations. No Hibernate configuration files are needed. The Hibernate properties are set in the pesistence.xml file. As I understand it as long as our classes use JPA 2 annotations, I should be able to change JPA providers (say from Hibernate to OpenJPA) by just changing the persistence.xml file.

The service classes use Spring's @Service annotation to mark the class as one Spring should automatically create an instance of in the Inversion of Control container. Using the Struts 2 Spring plugin (see the pom.xml) will cause Spring to inject the services classes into the Struts 2 Action Support classes.

In the service classes any data access classes needed are annotated with Spring's @Autowired annotation.

Tomcat Issues With Spring - JPA Project

My company uses Tomcat 6 as its Servlet container. When deploying the war file created by my example application I get this warning:

WARN org.hibernate.ejb.packaging.InputStreamZippedJarVisitor.doProcessElements:61 -
Unable to find file (ignored): jndi:/localhost/strutsspringjpaexample/

I think this is from a bug in the Hibernate code, as the application works fine. I researched the error on the internet and only found one reference that might be applicable (see reference 3). But if anyone has more insight on why this warning is generated and if there is something I need to configure differently please comment.

Request For Advice

My programming team is exploring the best way to structure our Spring - JPA web applications. I've created an example application that demonstrates one way to structure the application. Before recommending this structure to my team I'd like to get input from more experienced Java developers.

I'd appreciate any advice and comments from other Java developers who have experience with Spring - JPA - Maven web applications on the best way to structure the application.


  1. Spring Persistence with Hibernate; Paul Tepper Fisher and Brian D. Murphy; Apress Publishing, 2010
  2. Section 13.5 JPA, Spring Framework Reference Documentation 3.0
  3. Thread Discussion On Hibernate Application Startup

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
hi Bruce
your Struts2-spring-jpa sample works fine and I have the same warning
when it runs on Tomcat. But it doesn't work any longer when I have 3 tables
with manyToMany : any hint ?
# Posted By danielle | 2/22/11 5:59 AM
Danielle - I've used many-to-many relationships with JPA 2/Hibernate without a problem. Can you modify my example application to show what you're doing that is not working? Then I can look over the code.
# Posted By Bruce | 2/22/11 6:07 AM
Tomcat Issues With Spring - JPA Project - for tomcat:
inside your persistence.xml file do the following: <persistence-unit name="mylPU" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="USERNAME"/>
<property name="hibernate.connection.password" value="PASSWORD"/>
<property name="hibernate.connection.url" value="jdbc:mysql://ADDERSS"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="" value="update"/>
this is my only reference to hibernate. other than thay make sure that hibernate 3+ persistence and annotation jars are there. inside of spring appContext.xml i DO have to name a entityManagerFactory, repeat a "dataSource" with the username and password, and then name a transactionManager. it works. the advantage is that i am easily with the javax.persistence and can stay there without having to sweat about hibernate...
# Posted By ejf | 3/3/11 11:36 AM
Hi Bruce,
In action class u declared service class as transient!! Why we should do that ?


# Posted By Arun.R.U | 4/6/11 4:20 AM

The Struts 2 class ActionSupport implements Serializable (see So any instance members of the class must also implement Serializable or be marked as transient (

Since the Service classes do not maintain state there is no need for them to implement Serializable.
# Posted By Bruce | 4/6/11 6:33 AM
Thanks for your example.
In the action package, I find one class per action, such as PersonDeleter, PersonFinder, PersonSaver. As the project grows bigger, won't there be too many action classes? Why not put the Person operation related methods in one action class?
Chen Li
# Posted By heylichen | 6/14/11 8:29 AM
heylichen - I usually like to keep my classes small and focused (SRP). A class that does one thing is easier to maintain then a class that does numerous things. The cost of that approach is I have more classes and also more nodes in my struts.xml.

Your suggestion is one other developers I know do follow.
# Posted By Bruce | 6/14/11 9:05 AM
I tried the application on tomcat7 with java7 and got the results as you documented. I think due to lack of jndi look up it seems
the application server is unable to locate the strutsspringjpaexample.war. thats why the warning message probably. Not only that at the time of App Server shutdown I found the below message on server.

Nov 19, 2011 1:01:44 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/strutsspringjpaexample] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@c6c033]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@749130]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

the trace is as follows:

2011-11-19 13:15:30,125 WARN org.hibernate.ejb.packaging.InputStreamZippedJarVi
sitor.doProcessElements:61 - Unable to find file (ignored): jndi:/localhost/stru
tsspringjpaexample/ jndi:/localhost/strutsspringjpaexample/
at org.apache.naming.resources.DirContextURLConnection.getInputStream(Di
at org.hibernate.ejb.packaging.InputStreamZippedJarVisitor.doProcessElem
at org.hibernate.ejb.packaging.AbstractJarVisitor.getMatchingEntries(Abs
at org.hibernate.ejb.packaging.NativeScanner.getClassesInJar(NativeScann
at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configurati
at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.
at org.hibernate.ejb.Ejb3Configuration.configure(
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFa
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPro
at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludi
at org.springframework.dao.annotation.PersistenceExceptionTranslationAdv
at org.springframework.dao.annotation.PersistenceExceptionTranslationPos
at org.springframework.web.context.ContextLoader.createWebApplicationCon
at org.springframework.web.context.ContextLoader.initWebApplicationConte
at org.springframework.web.context.ContextLoaderListener.contextInitiali
at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
at org.apache.catalina.core.StandardContext$
at org.apache.catalina.core.StandardContext$
at java.util.concurrent.FutureTask$Sync.innerRun(
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
at java.util.concurrent.ThreadPoolExecutor$
# Posted By Srinivas | 11/19/11 3:27 AM
using your application can i inject service object in non struts action ..may be in filter class or another servlet
# Posted By Mahendra | 11/24/11 4:46 AM
Mahendra - For the Servlet class google Spring inject Servlet and you should find some information on that.

I'm not sure about injecting a dependency into a Servlet filter.

You may also want ask your question in the Spring user forum.
# Posted By Bruce | 11/24/11 6:39 AM
Thanks Bruce. I got those things resolved.

I have another question. I dont why with thissetup I am not able to get result for native or jpql query having cos() acos() functions ... any jar dependecy missing ?
# Posted By Mahendra | 4/24/12 10:46 AM
Its working fine now small copy paste mistake from side
# Posted By Mahendra | 4/27/12 5:21 AM

This article is a bit old now. Do yo have any most up-to-date integration structure and code now?

# Posted By Yoichi | 5/3/12 1:24 AM
Yoichi - check out /blog/index.cfm/2011/...
# Posted By Bruce | 5/10/12 4:24 PM
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner