org.springframework.orm.jpa
Class JpaTemplate

public class JpaTemplate
extends JpaAccessor
implements JpaOperations
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 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.

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 PersistenceExceptionTranslationPostProcessor .

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 JndiObjectFactoryBean .

Since2.0
VersionNot specified.
AuthorJuergen Hoeller
Wiki javadoc Use textile entry format.

Loading…

Fields inherited from org.springframework.orm.jpaEntityManagerFactoryAccessor
Constructor Summary
JpaTemplate()
Create a new JpaTemplate instance.
JpaTemplate( EntityManagerFactory emf )
Create a new JpaTemplate instance.
JpaTemplate( EntityManager em )
Create a new JpaTemplate instance.
Method Summary
boolean contains( Object entity )
No description provided.
protected EntityManager createEntityManagerProxy( EntityManager em )
Create a close-suppressing proxy for the given JPA EntityManager.
Object execute( JpaCallback action )
No description provided.
Object execute( JpaCallback action, boolean exposeNativeEntityManager )
Execute the action specified by the given action object within a EntityManager.
List executeFind( JpaCallback action )
No description provided.
T find( Class<T> entityClass, Object id )
No description provided.
List find( String queryString )
No description provided.
List find( String queryString, Object ... [] values )
No description provided.
List findByNamedParams( String queryString, Map<String, ?> params )
No description provided.
List findByNamedQuery( String queryName )
No description provided.
List findByNamedQuery( String queryName, Object ... [] values )
No description provided.
List findByNamedQueryAndNamedParams( String queryName, Map<String, ?> params )
No description provided.
void flush()
No description provided.
T getReference( Class<T> entityClass, Object id )
No description provided.
boolean isExposeNativeEntityManager()
Return whether to expose the native JPA EntityManager to JpaCallback code, or rather an EntityManager proxy.
T merge( T entity )
No description provided.
void persist( Object entity )
No description provided.
void refresh( Object entity )
No description provided.
void remove( Object entity )
No description provided.
void setExposeNativeEntityManager( boolean exposeNativeEntityManager )
Set whether to expose the native JPA EntityManager to JpaCallback code.
Methods inherited from org.springframework.beans.factoryInitializingBean
JpaTemplate
public JpaTemplate ( )
Create a new JpaTemplate instance.
Wiki javadoc Use textile entry format.
Add your comments here.
JpaTemplate
public JpaTemplate ( EntityManagerFactory emf )
Create a new JpaTemplate instance.
Parameters
TypeNameDescription
EntityManagerFactory emf EntityManagerFactory to create EntityManagers
Wiki javadoc Use textile entry format.
Add your comments here.
JpaTemplate
public JpaTemplate ( EntityManager em )
Create a new JpaTemplate instance.
Parameters
TypeNameDescription
EntityManager em EntityManager to use
Wiki javadoc Use textile entry format.
Add your comments here.
contains
public boolean contains ( Object entity )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
Object entity No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
createEntityManagerProxy
protected EntityManager createEntityManagerProxy ( EntityManager em )
Create a close-suppressing proxy for the given JPA EntityManager. The proxy also prepares returned JPA Query objects.
Parameters
TypeNameDescription
EntityManager em the JPA EntityManager to create a proxy for
See also
Wiki javadoc Use textile entry format.
Add your comments here.
execute
public Object execute ( JpaCallback action )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
JpaCallback action No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
execute
public Object execute ( JpaCallback action, boolean exposeNativeEntityManager )
Execute the action specified by the given action object within a EntityManager.
Parameters
TypeNameDescription
JpaCallback action callback object that specifies the JPA action
boolean exposeNativeEntityManager whether to expose the native JPA entity manager to callback code
Exceptions
DataAccessException in case of JPA errors
Wiki javadoc Use textile entry format.
Add your comments here.
executeFind
public List executeFind ( JpaCallback action )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
JpaCallback action No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
find
public <T> T find ( Class<T> entityClass, Object id )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
Class<T> entityClass No description provided.
Object id No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
find
public List find ( String queryString )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
String queryString No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
find
public List find ( String queryString, Object ... [] values )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
String queryString No description provided.
Object ...[] values No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
findByNamedParams
public List findByNamedParams ( String queryString, Map<String, ?> params )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
String queryString No description provided.
Map<String, ?> params No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
findByNamedQuery
public List findByNamedQuery ( String queryName )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
String queryName No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
findByNamedQuery
public List findByNamedQuery ( String queryName, Object ... [] values )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
String queryName No description provided.
Object ...[] values No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
findByNamedQueryAndNamedParams
public List findByNamedQueryAndNamedParams ( String queryName, Map<String, ?> params )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
String queryName No description provided.
Map<String, ?> params No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
flush
public void flush ( )
No description provided.
Implements method in JpaOperations
Returns void No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
getReference
public <T> T getReference ( Class<T> entityClass, Object id )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
Class<T> entityClass No description provided.
Object id No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
isExposeNativeEntityManager
public boolean isExposeNativeEntityManager ( )
Return whether to expose the native JPA EntityManager to JpaCallback code, or rather an EntityManager proxy.
Wiki javadoc Use textile entry format.
Add your comments here.
merge
public <T> T merge ( T entity )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
T entity No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
persist
public void persist ( Object entity )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
Object entity No description provided.
Returns void No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
refresh
public void refresh ( Object entity )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
Object entity No description provided.
Returns void No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
remove
public void remove ( Object entity )
No description provided.
Implements method in JpaOperations
Parameters
TypeNameDescription
Object entity No description provided.
Returns void No description provided.
Exceptions
DataAccessException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setExposeNativeEntityManager
public void setExposeNativeEntityManager ( boolean exposeNativeEntityManager )
Set whether to expose the native JPA EntityManager to JpaCallback code. Default is "false": a EntityManager proxy will be returned, suppressing close calls and automatically applying transaction timeouts (if any).

As there is often a need to cast to a provider-specific EntityManager class in DAOs that use the JPA 1.0 API, for JPA 2.0 previews and other provider-specific functionality, the exposed proxy implements all interfaces implemented by the original EntityManager. If this is not sufficient, turn this flag to "true".

Parameters
TypeNameDescription
boolean exposeNativeEntityManager No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.