|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.orm.jpa.EntityManagerFactoryAccessor
org.springframework.orm.jpa.JpaAccessor
org.springframework.orm.jpa.JpaTemplate
@PersistenceContext
)
Note that this class did not get upgraded to JPA 2.0 and never will.
@Deprecated public class JpaTemplate
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
.
EntityManagerFactoryAccessor.setEntityManagerFactory(EntityManagerFactory)
,
JpaCallback
,
javax.persistence.EntityManager
,
LocalEntityManagerFactoryBean
,
LocalContainerEntityManagerFactoryBean
,
JpaTransactionManager
,
JtaTransactionManager
,
OpenEntityManagerInViewFilter
,
OpenEntityManagerInViewInterceptor
Nested Class Summary | |
---|---|
private class |
JpaTemplate.CloseSuppressingInvocationHandler
Deprecated. Invocation handler that suppresses close calls on JPA EntityManagers. |
Field Summary | |
---|---|
private boolean |
exposeNativeEntityManager
Deprecated. |
Fields inherited from class org.springframework.orm.jpa.EntityManagerFactoryAccessor |
---|
logger |
Constructor Summary | |
---|---|
JpaTemplate()
Deprecated. Create a new JpaTemplate instance. |
|
JpaTemplate(EntityManager em)
Deprecated. Create a new JpaTemplate instance. |
|
JpaTemplate(EntityManagerFactory emf)
Deprecated. Create a new JpaTemplate instance. |
Method Summary | ||
---|---|---|
boolean |
contains(java.lang.Object entity)
Deprecated. |
|
protected EntityManager |
createEntityManagerProxy(EntityManager em)
Deprecated. Create a close-suppressing proxy for the given JPA EntityManager. |
|
|
execute(JpaCallback<T> action)
Deprecated. |
|
|
execute(JpaCallback<T> action,
boolean exposeNativeEntityManager)
Deprecated. Execute the action specified by the given action object within a EntityManager. |
|
java.util.List |
executeFind(JpaCallback<?> action)
Deprecated. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.Object id)
Deprecated. |
|
java.util.List |
find(java.lang.String queryString)
Deprecated. |
|
java.util.List |
find(java.lang.String queryString,
java.lang.Object... values)
Deprecated. |
|
java.util.List |
findByNamedParams(java.lang.String queryString,
java.util.Map<java.lang.String,?> params)
Deprecated. |
|
java.util.List |
findByNamedQuery(java.lang.String queryName)
Deprecated. |
|
java.util.List |
findByNamedQuery(java.lang.String queryName,
java.lang.Object... values)
Deprecated. |
|
java.util.List |
findByNamedQueryAndNamedParams(java.lang.String queryName,
java.util.Map<java.lang.String,?> params)
Deprecated. |
|
void |
flush()
Deprecated. |
|
|
getReference(java.lang.Class<T> entityClass,
java.lang.Object id)
Deprecated. |
|
boolean |
isExposeNativeEntityManager()
Deprecated. Return whether to expose the native JPA EntityManager to JpaCallback code, or rather an EntityManager proxy. |
|
|
merge(T entity)
Deprecated. |
|
void |
persist(java.lang.Object entity)
Deprecated. |
|
void |
prepareQuery(Query query)
Deprecated. Prepare the given JPA query object. |
|
void |
refresh(java.lang.Object entity)
Deprecated. |
|
void |
remove(java.lang.Object entity)
Deprecated. |
|
void |
setExposeNativeEntityManager(boolean exposeNativeEntityManager)
Deprecated. Set whether to expose the native JPA EntityManager to JpaCallback code. |
Methods inherited from class org.springframework.orm.jpa.JpaAccessor |
---|
afterPropertiesSet, flushIfNecessary, getEntityManager, getJpaDialect, isFlushEager, setEntityManager, setFlushEager, setJpaDialect, translateIfNecessary |
Methods inherited from class org.springframework.orm.jpa.EntityManagerFactoryAccessor |
---|
createEntityManager, getEntityManagerFactory, getJpaPropertyMap, getPersistenceUnitName, getTransactionalEntityManager, setBeanFactory, setEntityManagerFactory, setJpaProperties, setJpaPropertyMap, setPersistenceUnitName |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private boolean exposeNativeEntityManager
Constructor Detail |
---|
public JpaTemplate()
public JpaTemplate(EntityManagerFactory emf)
emf
- EntityManagerFactory to create EntityManagerspublic JpaTemplate(EntityManager em)
em
- EntityManager to useMethod Detail |
---|
public void setExposeNativeEntityManager(boolean exposeNativeEntityManager)
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".
JpaCallback
,
javax.persistence.EntityManager
public boolean isExposeNativeEntityManager()
public <T> T execute(JpaCallback<T> action) throws DataAccessException
execute
in interface JpaOperations
DataAccessException
public java.util.List executeFind(JpaCallback<?> action) throws DataAccessException
executeFind
in interface JpaOperations
DataAccessException
public <T> T execute(JpaCallback<T> action, boolean exposeNativeEntityManager) throws DataAccessException
action
- callback object that specifies the JPA actionexposeNativeEntityManager
- whether to expose the native
JPA entity manager to callback code
null
DataAccessException
- in case of JPA errorsprotected EntityManager createEntityManagerProxy(EntityManager em)
em
- the JPA EntityManager to create a proxy for
javax.persistence.EntityManager#close
public <T> T find(java.lang.Class<T> entityClass, java.lang.Object id) throws DataAccessException
find
in interface JpaOperations
DataAccessException
public <T> T getReference(java.lang.Class<T> entityClass, java.lang.Object id) throws DataAccessException
getReference
in interface JpaOperations
DataAccessException
public boolean contains(java.lang.Object entity) throws DataAccessException
contains
in interface JpaOperations
DataAccessException
public void refresh(java.lang.Object entity) throws DataAccessException
refresh
in interface JpaOperations
DataAccessException
public void persist(java.lang.Object entity) throws DataAccessException
persist
in interface JpaOperations
DataAccessException
public <T> T merge(T entity) throws DataAccessException
merge
in interface JpaOperations
DataAccessException
public void remove(java.lang.Object entity) throws DataAccessException
remove
in interface JpaOperations
DataAccessException
public void flush() throws DataAccessException
flush
in interface JpaOperations
DataAccessException
public java.util.List find(java.lang.String queryString) throws DataAccessException
find
in interface JpaOperations
DataAccessException
public java.util.List find(java.lang.String queryString, java.lang.Object... values) throws DataAccessException
find
in interface JpaOperations
DataAccessException
public java.util.List findByNamedParams(java.lang.String queryString, java.util.Map<java.lang.String,?> params) throws DataAccessException
findByNamedParams
in interface JpaOperations
DataAccessException
public java.util.List findByNamedQuery(java.lang.String queryName) throws DataAccessException
findByNamedQuery
in interface JpaOperations
DataAccessException
public java.util.List findByNamedQuery(java.lang.String queryName, java.lang.Object... values) throws DataAccessException
findByNamedQuery
in interface JpaOperations
DataAccessException
public java.util.List findByNamedQueryAndNamedParams(java.lang.String queryName, java.util.Map<java.lang.String,?> params) throws DataAccessException
findByNamedQueryAndNamedParams
in interface JpaOperations
DataAccessException
public void prepareQuery(Query query)
Applies a transaction timeout, if any. If you don't use such timeouts, the call is a no-op.
In general, prefer a proxied EntityManager instead, which will automatically apply the transaction timeout (through the use of a special EntityManager proxy). You need to set the "exposeNativeEntityManager" property to "false" to activate this. Note that you won't be able to cast to a provider-specific JPA EntityManager class anymore then.
query
- the JPA query objectJpaCallback.doInJpa(EntityManager)
,
EntityManagerFactoryUtils.applyTransactionTimeout(Query, EntityManagerFactory)
,
setExposeNativeEntityManager(boolean)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |