org.kuali.rice.krad.data.jpa
Class JpaPersistenceProvider

java.lang.Object
  extended by org.kuali.rice.krad.data.jpa.JpaPersistenceProvider
All Implemented Interfaces:
PersistenceProvider, Provider, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware

@Transactional
public class JpaPersistenceProvider
extends Object
implements PersistenceProvider, org.springframework.beans.factory.BeanFactoryAware

Java Persistence API (JPA) implementation of 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 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:
org.springframework.orm.jpa.support.SharedEntityManagerBean, PersistenceExceptionTranslator

Field Summary
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 Summary
JpaPersistenceProvider()
           
 
Method Summary
<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 PersistenceExceptionTranslators 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTO_FLUSH

public static final 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.

This is recommended for testing only since the change is global and would affect all persistence units.

See Also:
Constant Field Values
Constructor Detail

JpaPersistenceProvider

public JpaPersistenceProvider()
Method Detail

getSharedEntityManager

public javax.persistence.EntityManager getSharedEntityManager()
Gets the shared EntityManager.

Returns:
The shared EntityManager.

setSharedEntityManager

public void setSharedEntityManager(javax.persistence.EntityManager sharedEntityManager)
Setter for the shared EntityManager.

Parameters:
sharedEntityManager - The shared EntityManager to set.

setDataObjectService

public void setDataObjectService(DataObjectService dataObjectService)
Setter for the DataObjectService.

Parameters:
dataObjectService - The DataObjectService to set.

getDataObjectService

public DataObjectService getDataObjectService()
Returns the DataObjectService.

Returns:
a DataObjectService

setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
                    throws org.springframework.beans.BeansException

Specified by:
setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
Throws:
org.springframework.beans.BeansException

detectPersistenceExceptionTranslators

protected org.springframework.dao.support.PersistenceExceptionTranslator detectPersistenceExceptionTranslators(org.springframework.beans.factory.ListableBeanFactory beanFactory)
Gets any PersistenceExceptionTranslators from the BeanFactory.

Parameters:
beanFactory - The BeanFactory to use.
Returns:
A PersistenceExceptionTranslator from the BeanFactory.

save

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

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:
save in interface PersistenceProvider
Type Parameters:
T - the data object class type
Parameters:
dataObject - the data object to save
options - 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

public <T> T find(Class<T> type,
                  Object id)
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:
find in interface PersistenceProvider
Type Parameters:
T - the data object class type
Parameters:
type - the type of the data object to find
id - 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

public <T> QueryResults<T> findMatching(Class<T> type,
                                        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:
findMatching in interface PersistenceProvider
Type Parameters:
T - the data object class type
Parameters:
type - the type of the data objects to query
queryByCriteria - query object, can contain sorting and page request configuration
Returns:
the results of the query, will never return null but may return empty results

delete

public void delete(Object dataObject)
Deletes a given data object.

Specified by:
delete in interface PersistenceProvider
Parameters:
dataObject - the data object to delete

copyInstance

public <T> T copyInstance(T dataObject)
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:
copyInstance in interface PersistenceProvider
Type Parameters:
T - the type of the data object
Parameters:
dataObject - the data object to copy
Returns:
a copy of the given data object

handles

public boolean handles(Class<?> type)
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:
handles in interface PersistenceProvider
Parameters:
type - the data object type to check
Returns:
true if this provider can handle the given type, false otherwise

flush

public void flush(Class<?> type)
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.

Specified by:
flush in interface PersistenceProvider
Parameters:
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.

verifyDataObjectWritable

protected void verifyDataObjectWritable(Object dataObject)
Verifies that the data object can be written to.

Parameters:
dataObject - The data object to check.

doWithExceptionTranslation

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.

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.


Copyright © 2005–2014 The Kuali Foundation. All rights reserved.