Class JpaPersistenceProvider
- All Implemented Interfaces:
PersistenceProvider,Provider,org.springframework.beans.factory.Aware,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 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
DataAccessExceptions. It will scan the
BeanFactory in which it was created to find beans which implement
PersistenceExceptionTranslator and use those translators for translation.
- Author:
- Kuali Rice Team (rice.collab@kuali.org)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringIndicates if a JPAEntityManagerflush should be automatically executed when callingDataObjectService.save(Object, org.kuali.rice.krad.data.PersistenceOption...)using a JPA provider. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidclearObjectIdOnUpdatableObjects(Object dataObject, Set<Object> visitedObjects) For the given data object, recurse through all updatable references and clear the object ID on the basis that this is a unique column in each object's table.protected voidclearVersionNumberOnUpdatableObjects(Object dataObject, Set<Object> visitedObjects) For the given data object, recurse through all updatable references and clear the object ID on the basis that this is a unique column in each object's table.<T> TcopyInstance(T dataObject, CopyOption... options) Returns a copy of the given data object instance.voidDeletes a given data object.<T> voidDeletes all data objects based on the given type.<T> voiddeleteMatching(Class<T> type, org.kuali.rice.core.api.criteria.QueryByCriteria queryByCriteria) Deletes data objects based on the given criteriaprotected org.springframework.dao.support.PersistenceExceptionTranslatordetectPersistenceExceptionTranslators(org.springframework.beans.factory.ListableBeanFactory beanFactory) Gets anyPersistenceExceptionTranslators from theBeanFactory.protected <T> TdoWithExceptionTranslation(Callable<T> callable) Surrounds the transaction with a try/catch block that can use thePersistenceExceptionTranslatorto translate the exception if necessary.<T> TInvoked to retrieve a data object instance by a single primary key field or id object.<T> org.kuali.rice.core.api.criteria.QueryResults<T> Retrieves all data objects for the given type.<T> org.kuali.rice.core.api.criteria.QueryResults<T> findMatching(Class<T> type, org.kuali.rice.core.api.criteria.QueryByCriteria queryByCriteria) Executes a query for the given data object.voidFlush any outstanding changes within the current context for the provider pertaining to the given data object Class type.Returns theDataObjectService.javax.persistence.EntityManagerGets the sharedEntityManager.booleanIndicates whether or not this provider handles persistence for the given data object type.<T> Tsave(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.voidsetBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) voidsetDataObjectService(DataObjectService dataObjectService) Setter for theDataObjectService.voidsetSharedEntityManager(javax.persistence.EntityManager sharedEntityManager) Setter for the sharedEntityManager.protected voidverifyDataObjectWritable(Object dataObject) Verifies that the data object can be written to.
-
Field Details
-
AUTO_FLUSH
Indicates if a JPAEntityManagerflush should be automatically executed when callingDataObjectService.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.
- See Also:
-
-
Constructor Details
-
JpaPersistenceProvider
public JpaPersistenceProvider()
-
-
Method Details
-
setDataObjectService
Setter for theDataObjectService.- Parameters:
dataObjectService- TheDataObjectServiceto set.
-
getDataObjectService
Returns theDataObjectService.- Returns:
- a
DataObjectService
-
setBeanFactory
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException - Specified by:
setBeanFactoryin interfaceorg.springframework.beans.factory.BeanFactoryAware- Throws:
org.springframework.beans.BeansException
-
detectPersistenceExceptionTranslators
protected org.springframework.dao.support.PersistenceExceptionTranslator detectPersistenceExceptionTranslators(org.springframework.beans.factory.ListableBeanFactory beanFactory) Gets anyPersistenceExceptionTranslators from theBeanFactory.- Parameters:
beanFactory- TheBeanFactoryto use.- Returns:
- A
PersistenceExceptionTranslatorfrom theBeanFactory.
-
save
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.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.
- Specified by:
savein interfacePersistenceProvider- Type Parameters:
T- the data object class type- Parameters:
dataObject- the data object to saveoptions- the options to use when saving the data object- Returns:
- the saved data object, calling code should always use the reference the object returned from this method for future operations after calling the save since it could have been updated
-
find
Invoked to retrieve a data object instance by a single primary key field or id object.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.
- Specified by:
findin interfacePersistenceProvider- Type Parameters:
T- the data object class type- Parameters:
type- the type of the data object to findid- the id representing the primary key of the data object to find- Returns:
- the entity with the given primary key or null if none found
-
findMatching
@Transactional(readOnly=true) public <T> org.kuali.rice.core.api.criteria.QueryResults<T> findMatching(Class<T> type, org.kuali.rice.core.api.criteria.QueryByCriteria queryByCriteria) Executes a query for the given data object.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.
- Specified by:
findMatchingin interfacePersistenceProvider- Type Parameters:
T- the data object class type- Parameters:
type- the type of the data objects to queryqueryByCriteria- query object, can contain sorting and page request configuration- Returns:
- the results of the query, will never return null but may return empty results
-
findAll
@Transactional(readOnly=true) public <T> org.kuali.rice.core.api.criteria.QueryResults<T> findAll(Class<T> type) Retrieves all data objects for the given type.- Specified by:
findAllin interfacePersistenceProvider- Type Parameters:
T- the data object class type- Parameters:
type- the type of data objects to find- Returns:
- the results of the query, will never return null but may return empty results
-
delete
Deletes a given data object.- Specified by:
deletein interfacePersistenceProvider- Parameters:
dataObject- the data object to delete
-
deleteMatching
@Transactional public <T> void deleteMatching(Class<T> type, org.kuali.rice.core.api.criteria.QueryByCriteria queryByCriteria) Deletes data objects based on the given criteriaIf the given criteria is empty or null than an
IllegalArgumentExceptionwill be thrown. If the given type is null then anIllegalArgumentExceptionwill be thrown.- Specified by:
deleteMatchingin interfacePersistenceProvider- Parameters:
type- the type of data objectqueryByCriteria- criteria to filter by
-
deleteAll
Deletes all data objects based on the given type.- Specified by:
deleteAllin interfacePersistenceProvider- Type Parameters:
T- the data object class type- Parameters:
type- the type of data objects to delete
-
copyInstance
Returns a copy of the given data object instance.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).
- Specified by:
copyInstancein interfacePersistenceProvider- Type Parameters:
T- the type of the data object- Parameters:
dataObject- the data object to copy- Returns:
- a copy of the given data object
-
clearObjectIdOnUpdatableObjects
For the given data object, recurse through all updatable references and clear the object ID on the basis that this is a unique column in each object's table.- Parameters:
dataObject- The data object on which to clear the object ID from itself and all updatable child objects.visitedObjects- A set of objects built by the recursion process which will be checked to ensure that the code does not get into an infinite loop.
-
clearVersionNumberOnUpdatableObjects
For the given data object, recurse through all updatable references and clear the object ID on the basis that this is a unique column in each object's table.- Parameters:
dataObject- The data object on which to clear the object ID from itself and all updatable child objects.visitedObjects- A set of objects built by the recursion process which will be checked to ensure that the code does not get into an infinite loop.
-
handles
Indicates whether or not this provider handles persistence for the given data object 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.
- Specified by:
handlesin interfacePersistenceProvider- Parameters:
type- the data object type to check- Returns:
- true if this provider can handle the given type, false otherwise
-
flush
Flush any outstanding changes within the current context for the provider pertaining to the given data object Class type.If an implementation of this interface does not support or require the concept of "flushing", this method can be ignored. However, when used, this must purge *all* cache items for the given types from all caches managed by this provider.
- Specified by:
flushin interfacePersistenceProvider- Parameters:
type- the type of the data object for which to perform the flush. This should be used to identify the context in which to perform the flush.
-
verifyDataObjectWritable
Verifies that the data object can be written to.- Parameters:
dataObject- The data object to check.
-
doWithExceptionTranslation
Surrounds the transaction with a try/catch block that can use thePersistenceExceptionTranslatorto translate the exception if necessary.- Type Parameters:
T- The type of the data operation.- Parameters:
callable- The data operation to invoke.- Returns:
- The result from the data operation, if successful.