Helper class that allows for writing JPA data access code in the same style as with Spring's well-known JdoTemplate and HibernateTemplate classes. Automatically converts PersistenceExceptions into Spring DataAccessExceptions, following the
org.springframework.dao
exception hierarchy.
The central method is of this template is "execute", supporting JPA access code implementing the {@link JpaCallback} interface. It provides JPA EntityManagerhandling such that neither the JpaCallback implementation nor the calling code needs to explicitly care about retrieving/closing EntityManagers, or handling JPA lifecycle exceptions.
Can be used within a service implementation via direct instantiation with a EntityManagerFactory reference, or get prepared in an application context and given to services as bean reference. Note: The EntityManagerFactory should always be configured as bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
NOTE: JpaTemplate mainly exists as a sibling of JdoTemplate and HibernateTemplate, offering the same style for people used to it. For newly started projects, consider adopting the standard JPA style of coding data access objects instead, based on a "shared EntityManager" reference injected via a Spring bean definition or the JPA PersistenceContext annotation. (Using Spring's SharedEntityManagerBean / PersistenceAnnotationBeanPostProcessor, or using a direct JNDI lookup for an EntityManager on a Java EE 5 server.)
JpaTemplate can be considered as direct alternative to working with the native JPA EntityManager API (through a shared EntityManager reference, as outlined above). The major advantage is its automatic conversion to DataAccessExceptions; the major disadvantage is that it introduces another thin layer on top of the native JPA API. Note that exception translation can also be achieved through AOP advice; check out {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor}.
{@link LocalContainerEntityManagerFactoryBean} is the preferred way ofobtaining a reference to an EntityManagerFactory, at least outside of a full Java EE 5 environment. The Spring application context will manage its lifecycle, initializing and shutting down the factory as part of the application. Within a Java EE 5 environment, you will typically work with a server-managed EntityManagerFactory that is exposed via JNDI, obtained through Spring's {@link org.springframework.jndi.JndiObjectFactoryBean}.
@author Juergen Hoeller
@since 2.0
@see #setEntityManagerFactory
@see JpaCallback
@see javax.persistence.EntityManager
@see LocalEntityManagerFactoryBean
@see LocalContainerEntityManagerFactoryBean
@see JpaTransactionManager
@see org.springframework.transaction.jta.JtaTransactionManager
@see org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
@see org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor
@deprecated as of Spring 3.1, in favor of native EntityManager usage(typically obtained through @PersistenceContext
) Note that this class did not get upgraded to JPA 2.0 and never will.