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 .
Loading…
| Fields inherited from org.springframework.orm.jpaEntityManagerFactoryAccessor |
|---|
| Constructor Summary |
|---|
|
Create a new JpaTemplate instance. |
|
Create a new JpaTemplate instance. |
|
Create a new JpaTemplate instance. |
| Method Summary | |
|---|---|
| boolean |
No description provided. |
| protected EntityManager |
Create a close-suppressing proxy for the given JPA EntityManager. |
| Object |
No description provided. |
| Object |
Execute the action specified by the given action object within a EntityManager. |
| List |
No description provided. |
| T |
No description provided. |
| List |
No description provided. |
| List |
No description provided. |
| List |
No description provided. |
| List |
No description provided. |
| List |
No description provided. |
| List |
No description provided. |
| void |
No description provided. |
| T |
No description provided. |
| boolean |
Return whether to expose the native JPA EntityManager to JpaCallback code, or rather an EntityManager proxy. |
| T |
No description provided. |
| void |
No description provided. |
| void |
No description provided. |
| void |
No description provided. |
| void |
Set whether to expose the native JPA EntityManager to JpaCallback code. |
| Methods inherited from org.springframework.orm.jpaEntityManagerFactoryAccessor |
|---|
| Methods inherited from org.springframework.beans.factoryInitializingBean |
|---|
| Methods inherited from org.springframework.orm.jpaJpaAccessor |
|---|
| Methods inherited from java.langObject |
|---|
public <T>
T
find
(
Class<T>
entityClass,
Object
id
)
public <T>
T
getReference
(
Class<T>
entityClass,
Object
id
)
public
boolean
isExposeNativeEntityManager
(
)
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".