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.
NOTE: JpaTemplate mainly exists as a sibling of JdoTemplate and
HibernateTemplate, to offer 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.)
The central method is of this template is "execute", supporting JPA access code
implementing the
JpaCallback interface. It provides JPA EntityManager
handling 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.
JpaTemplate can be considered as direct alternative to working with the
raw 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 target API.
Note that even if
JpaTransactionManager is used for transaction
demarcation in higher-level services, all those services above the data
access layer don't need to be JPA-aware. Setting such a special
PlatformTransactionManager is a configuration issue: For example,
switching to JTA is just a matter of Spring configuration (use
JtaTransactionManager instead) that does not affect application code.
LocalContainerEntityManagerFactoryBean is the preferred way of
obtaining 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
org.springframework.jndi.JndiObjectFactoryBean .
author: Juergen Hoeller since: 2.0 See Also: org.springframework.orm.jdo.JdoTemplate See Also: org.springframework.orm.hibernate3.HibernateTemplate See Also: org.springframework.orm.jpa.support.SharedEntityManagerBean See Also: org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor See Also: JpaTemplate.setEntityManagerFactory See Also: JpaTemplate.execute(JpaCallback) See Also: javax.persistence.EntityManager See Also: LocalEntityManagerFactoryBean See Also: LocalContainerEntityManagerFactoryBean See Also: org.springframework.jndi.JndiObjectFactoryBean |