@Transactional public class JpaPersistenceProvider extends Object implements PersistenceProvider, org.springframework.beans.factory.BeanFactoryAware
PersistenceProvider
.
When creating a new instance of this provider, a reference to a "shared" entity manager (like that created by
Spring's org.springframework.orm.jpa.support.SharedEntityManagerBean
must be injected. Additionally, a
reference to the DataObjectService
must be injected as well.
This class will perform persistence exception translation (converting JPA exceptions to
DataAccessException
s. It will scan the
BeanFactory
in which it was created to find beans which implement
PersistenceExceptionTranslator
and use those translators for translation.
org.springframework.orm.jpa.support.SharedEntityManagerBean
,
PersistenceExceptionTranslator
Modifier and Type | Field and Description |
---|---|
static String |
AUTO_FLUSH
Indicates if a JPA
EntityManager flush should be automatically executed when calling
DataObjectService.save(Object, org.kuali.rice.krad.data.PersistenceOption...)
using a JPA provider. |
Constructor and Description |
---|
JpaPersistenceProvider() |
Modifier and Type | Method and Description |
---|---|
<T> T |
copyInstance(T dataObject)
Returns a copy of the given data object instance.
|
void |
delete(Object dataObject)
Deletes a given data object.
|
protected org.springframework.dao.support.PersistenceExceptionTranslator |
detectPersistenceExceptionTranslators(org.springframework.beans.factory.ListableBeanFactory beanFactory)
Gets any
PersistenceExceptionTranslator s from the BeanFactory . |
protected <T> T |
doWithExceptionTranslation(Callable<T> callable)
Surrounds the transaction with a try/catch block that can use the
PersistenceExceptionTranslator to
translate the exception if necessary. |
<T> T |
find(Class<T> type,
Object id)
Invoked to retrieve a data object instance by a single primary key field or id object.
|
<T> QueryResults<T> |
findMatching(Class<T> type,
QueryByCriteria queryByCriteria)
Executes a query for the given data object.
|
void |
flush(Class<?> type)
Flush any outstanding changes within the current context for the provider pertaining to the given data object
Class type.
|
DataObjectService |
getDataObjectService()
Returns the
DataObjectService . |
javax.persistence.EntityManager |
getSharedEntityManager()
Gets the shared
EntityManager . |
boolean |
handles(Class<?> type)
Indicates whether or not this provider handles persistence for the given data object type.
|
<T> T |
save(T dataObject,
PersistenceOption... options)
Saves the given data object, determining whether or not this is a new data object which is being created, or an
existing one which should be updated.
|
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) |
void |
setDataObjectService(DataObjectService dataObjectService)
Setter for the
DataObjectService . |
void |
setSharedEntityManager(javax.persistence.EntityManager sharedEntityManager)
Setter for the shared
EntityManager . |
protected void |
verifyDataObjectWritable(Object dataObject)
Verifies that the data object can be written to.
|
public static final String AUTO_FLUSH
EntityManager
flush should be automatically executed when calling
DataObjectService.save(Object, org.kuali.rice.krad.data.PersistenceOption...)
using a JPA provider.
This is recommended for testing only since the change is global and would affect all persistence units.
public JpaPersistenceProvider()
public javax.persistence.EntityManager getSharedEntityManager()
EntityManager
.EntityManager
.public void setSharedEntityManager(javax.persistence.EntityManager sharedEntityManager)
EntityManager
.sharedEntityManager
- The shared EntityManager
to set.public void setDataObjectService(DataObjectService dataObjectService)
DataObjectService
.dataObjectService
- The DataObjectService
to set.public DataObjectService getDataObjectService()
DataObjectService
.DataObjectService
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
org.springframework.beans.BeansException
protected org.springframework.dao.support.PersistenceExceptionTranslator detectPersistenceExceptionTranslators(org.springframework.beans.factory.ListableBeanFactory beanFactory)
PersistenceExceptionTranslator
s from the BeanFactory
.beanFactory
- The BeanFactory
to use.PersistenceExceptionTranslator
from the BeanFactory
.public <T> T save(T dataObject, PersistenceOption... options)
Optional persistence options can be passed to indicate whether or not linking should be performed prior to persistence and whether or not validation should be performed. By default, linking is performed as well as validation.
save
in interface PersistenceProvider
T
- the data object class typedataObject
- the data object to saveoptions
- the options to use when saving the data objectpublic <T> T find(Class<T> type, Object id)
In the case of a compound primary key consisting of multiple attributes on the data object, a CompoundKey can be passed in order to encapsulate these into a single argument.
find
in interface PersistenceProvider
T
- the data object class typetype
- the type of the data object to findid
- the id representing the primary key of the data object to findpublic <T> QueryResults<T> findMatching(Class<T> type, QueryByCriteria queryByCriteria)
If the given QueryByCriteria is empty or null, then all data objects for the given type will be returned. Depending on the given criteria and the implementation for the query execution, not all matching results may be returned. The QueryResults will contain information on whether or not there are additional results which can be used for paging and similar functionality.
findMatching
in interface PersistenceProvider
T
- the data object class typetype
- the type of the data objects to queryqueryByCriteria
- query object, can contain sorting and page request configurationpublic void delete(Object dataObject)
delete
in interface PersistenceProvider
dataObject
- the data object to deletepublic <T> T copyInstance(T dataObject)
The method of copying is provider dependent, and will handle instances (including nested) using whatever measures might be required to deal with the quirks of said provider (e.g. fetching lazy loaded relations).
copyInstance
in interface PersistenceProvider
T
- the type of the data objectdataObject
- the data object to copypublic boolean handles(Class<?> type)
Responsibility on with the caller to call prior to invocation of any other PersistenceProvider methods to ensure the data objects of the right type are passed.
handles
in interface PersistenceProvider
type
- the data object type to checkpublic void flush(Class<?> type)
If an implementation of this interface does not support or require the concept of "flushing", this method can be ignored.
flush
in interface PersistenceProvider
type
- the type of the data object for which to perform the flush. This shoul be used to identify the
context in which to perform the flush.protected void verifyDataObjectWritable(Object dataObject)
dataObject
- The data object to check.protected <T> T doWithExceptionTranslation(Callable<T> callable)
PersistenceExceptionTranslator
to
translate the exception if necessary.T
- The type of the data operation.callable
- The data operation to invoke.Copyright © 2005–2014 The Kuali Foundation. All rights reserved.