View RSS Feed

Spring Framework

Data Access with Java Persistence API

Rate this Entry
by , 11-29-2011 at 03:44 AM (1805 Views)
Spring integration with Java Persistence API (JPA) came about on the release of Spring 2.0. It is very easy to use JPA with Spring because it is all based on plain old java beans (POJOs). In this tip, we will outline the steps required for you to use JPA with Spring.

JPA-based applications are based on the implementation of EntityManagerFactory in order to get an instance of an EntityManager. There are two kinds of entity managers from the JPA Specification that both implement the same EntityManager interface. They are:
  • Application-managed—Entity managers are created when an application directly requests one from an entity manager factory.
  • Container-managed—Entity managers are created and managed by a Java EE container.

When you are using JPA with Spring, the first thing that you need to do is to configure an entity manager factory as a bean in the Spring application context. Irrespective of what type of EntityManagerFactory you use, Spring will assume responsibility for managing the EntityManagers for you. So the mapping for the EntityManagerFactory to the Spring factory is:
  • LocalEntityManagerFactoryBean produces an application managed EntityManagerFactory
  • LocalContainerEntityManagerFactoryBean produces a container managed EntityManagerFactory

Application managed entity manager factories get their configuration information from the persistence.xml configuration file that is located in the META-INF directory. It is used to define persistence units which is a grouping of one of more persistent classes corresponding to one data source. An example is presented below:

Java Code:
<persistence xmlns="" version="1.0">
<persistence-unit name="springexamples"> 
<property name="toplink.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="toplink.jdbc.url" value= "jdbc:hsqldb:hsql://localhost/springexamples/springexamples" />
<property name="toplink.jdbc.user" value="joe" />
<property name="toplink.jdbc.password" value="" />
</properties> </persistence-unit>
This leaves very little configuration required in Spring. You only need to declare the LocalManagerFactoryBean:

Java Code:
<bean id="emfBean" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="carsPU" /> 
The value of the persistenceUnitName property refers to the persistent unit name of the persistent.xml file. With an application-managed application which only minimally uses Spring, it is the application that is responsible for obtaining an EntityManagerFactory through the JPA implementation’s PersistenceProvider. Therefore all the persistent unit definitions are found in the persistence.xml file. The implementation of the Dao in Spring uses a template form that is related to the JpaDaoSupport class. Here we will show you the pure JPA approach.

Java Code:
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext;
import org.springframework.dao.DataAccessException; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Transactional;
import com.acme.springexamples.domain.Car; 

public class JpaCarDao implements CarDao {
	private static final String ALL_CARS = "SELECT c FROM CAR c";
	private static final String CAR_FOR_MODEL = "SELECT c FROM 
		CAR c WHERE c.model = :model";
	private EntityManager em;	
	public void addCar(Car Car) { 
	public Car getCarById(long id) { 
		return em.find(Car.class, id);
	public void saveCar(Car Car) { 
JpaCarDao uses a EntityManager to handle persistence. This allows for the DAO to be unencumbered by Spring specific code. Finally the last two things to be done is to configure a PersistenceAnnotationBeanPostProcessor and a PersistenceExceptionTranslationPostProcessor to enable EntityManager injection in Spring and exception translation respectively.

Java Code:
<bean class="

<bean class="org.springframework.dao.annotation.
Note that JpaCarDao has @Repository and @Transactional annotations. The @Transactional indicates that the persistence methods in this DAO will be involved in a transactional context. The @Repository allows for the PersistenceExceptionTranslationPostProcessor to know that this is one of those beans for whom exceptions should be translated into one of Spring’s unified data access exceptions.

In order to complete the use of @Repository we need to wire up the PersistenceExceptionTranslationPostProcessor as a bean in Spring:

Java Code:
<bean class="org.springframework.dao.annotation.
Now this isn’t mandatory but it is very useful in simplify and unify the number of native exceptions that you would need to handle. If in the future your company wanted to switch to another persistence framework, the use of this Spring translation class makes it alot easier.

Submit "Data Access with Java Persistence API" to Facebook Submit "Data Access with Java Persistence API" to Digg Submit "Data Access with Java Persistence API" to Submit "Data Access with Java Persistence API" to StumbleUpon Submit "Data Access with Java Persistence API" to Google

Updated 11-30-2011 at 04:23 AM by Spring Framework

JPA , Spring 3 , EntityManager , EntityManagerFactory