|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.orm.jdo.JdoAccessor
org.springframework.orm.jdo.JdoTemplate
public class JdoTemplate
Helper class that simplifies JDO data access code, and converts
JDOExceptions into Spring DataAccessExceptions, following the
org.springframework.dao
exception hierarchy.
The central method is execute
, supporting JDO access code
implementing the JdoCallback
interface. It provides JDO PersistenceManager
handling such that neither the JdoCallback implementation nor the calling
code needs to explicitly care about retrieving/closing PersistenceManagers,
or handling JDO lifecycle exceptions.
Typically used to implement data access or business logic services that
use JDO within their implementation but are JDO-agnostic in their interface.
The latter or code calling the latter only have to deal with business
objects, query objects, and org.springframework.dao
exceptions.
Can be used within a service implementation via direct instantiation with a PersistenceManagerFactory reference, or get prepared in an application context and given to services as bean reference. Note: The PersistenceManagerFactory 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.
This class can be considered as direct alternative to working with the
raw JDO PersistenceManager API (through
PersistenceManagerFactoryUtils.getPersistenceManager()
).
The major advantage is its automatic conversion to DataAccessExceptions, the
major disadvantage that no checked application exceptions can get thrown from
within data access code. Corresponding checks and the actual throwing of such
exceptions can often be deferred to after callback execution, though.
LocalPersistenceManagerFactoryBean
is the preferred way of obtaining
a reference to a specific PersistenceManagerFactory, at least in a non-EJB
environment. The Spring application context will manage its lifecycle,
initializing and shutting down the factory as part of the application.
Note that lazy loading will just work with an open JDO PersistenceManager,
either within a Spring-driven transaction (with JdoTransactionManager or
JtaTransactionManager) or within OpenPersistenceManagerInViewFilter/Interceptor.
Furthermore, some operations just make sense within transactions,
for example: evict
, evictAll
, flush
.
NOTE: This class requires JDO 2.0 or higher, as of Spring 2.5. As of Spring 3.0, it follows JDO 2.1 conventions in terms of generic parameter and return types, which still remaining compatible with JDO 2.0.
JdoAccessor.setPersistenceManagerFactory(PersistenceManagerFactory)
,
JdoCallback
,
javax.jdo.PersistenceManager
,
LocalPersistenceManagerFactoryBean
,
JdoTransactionManager
,
JtaTransactionManager
,
OpenPersistenceManagerInViewFilter
,
OpenPersistenceManagerInViewInterceptor
Nested Class Summary | |
---|---|
private class |
JdoTemplate.CloseSuppressingInvocationHandler
Invocation handler that suppresses close calls on JDO PersistenceManagers. |
Field Summary | |
---|---|
private boolean |
allowCreate
|
private boolean |
exposeNativePersistenceManager
|
Fields inherited from class org.springframework.orm.jdo.JdoAccessor |
---|
logger |
Constructor Summary | |
---|---|
JdoTemplate()
Create a new JdoTemplate instance. |
|
JdoTemplate(PersistenceManagerFactory pmf)
Create a new JdoTemplate instance. |
|
JdoTemplate(PersistenceManagerFactory pmf,
boolean allowCreate)
Create a new JdoTemplate instance. |
Method Summary | ||
---|---|---|
protected PersistenceManager |
createPersistenceManagerProxy(PersistenceManager pm)
Create a close-suppressing proxy for the given JDO PersistenceManager. |
|
void |
deletePersistent(java.lang.Object entity)
Delete the given persistent instance. |
|
void |
deletePersistentAll(java.util.Collection entities)
Delete all given persistent instances. |
|
|
detachCopy(T entity)
Detach a copy of the given persistent instance from the current JDO transaction, for use outside a JDO transaction (for example, as web form object). |
|
|
detachCopyAll(java.util.Collection<T> entities)
Detach copies of the given persistent instances from the current JDO transaction, for use outside a JDO transaction (for example, as web form objects). |
|
void |
evict(java.lang.Object entity)
Remove the given object from the PersistenceManager cache. |
|
void |
evictAll()
Remove all objects from the PersistenceManager cache. |
|
void |
evictAll(java.util.Collection entities)
Remove all given objects from the PersistenceManager cache. |
|
|
execute(JdoCallback<T> action)
Execute the action specified by the given action object within a PersistenceManager. |
|
|
execute(JdoCallback<T> action,
boolean exposeNativePersistenceManager)
Execute the action specified by the given action object within a PersistenceManager. |
|
java.util.Collection |
executeFind(JdoCallback<?> action)
Execute the specified action assuming that the result object is a Collection. |
|
|
find(java.lang.Class<T> entityClass)
Find all persistent instances of the given class. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.String filter)
Find all persistent instances of the given class that match the given JDOQL filter. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.String filter,
java.lang.String ordering)
Find all persistent instances of the given class that match the given JDOQL filter, with the given result ordering. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.String filter,
java.lang.String parameters,
java.util.Map<java.lang.String,?> values)
Find all persistent instances of the given class that match the given JDOQL filter, using the given parameter declarations and parameter values. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.String filter,
java.lang.String parameters,
java.util.Map<java.lang.String,?> values,
java.lang.String ordering)
Find all persistent instances of the given class that match the given JDOQL filter, using the given parameter declarations and parameter values, with the given result ordering. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.String filter,
java.lang.String parameters,
java.lang.Object... values)
Find all persistent instances of the given class that match the given JDOQL filter, using the given parameter declarations and parameter values. |
|
|
find(java.lang.Class<T> entityClass,
java.lang.String filter,
java.lang.String parameters,
java.lang.Object[] values,
java.lang.String ordering)
Find all persistent instances of the given class that match the given JDOQL filter, using the given parameter declarations and parameter values, with the given result ordering. |
|
java.util.Collection |
find(java.lang.String queryString)
Find persistent instances through the given single-string JDOQL query. |
|
java.util.Collection |
find(java.lang.String queryString,
java.util.Map<java.lang.String,?> values)
Find persistent instances through the given single-string JDOQL query. |
|
java.util.Collection |
find(java.lang.String queryString,
java.lang.Object... values)
Find persistent instances through the given single-string JDOQL query. |
|
java.util.Collection |
find(java.lang.String language,
java.lang.Object queryObject)
Find persistent instances through the given query object in the specified query language. |
|
|
findByNamedQuery(java.lang.Class<T> entityClass,
java.lang.String queryName)
Find persistent instances through the given named query. |
|
|
findByNamedQuery(java.lang.Class<T> entityClass,
java.lang.String queryName,
java.util.Map<java.lang.String,?> values)
Find persistent instances through the given named query. |
|
|
findByNamedQuery(java.lang.Class<T> entityClass,
java.lang.String queryName,
java.lang.Object... values)
Find persistent instances through the given named query. |
|
void |
flush()
Flush all transactional modifications to the database. |
|
|
getObjectById(java.lang.Class<T> entityClass,
java.lang.Object idValue)
Return the persistent instance of the given entity class with the given id value, throwing an exception if not found. |
|
java.lang.Object |
getObjectById(java.lang.Object objectId)
Return the persistent instance with the given JDO object id, throwing an exception if not found. |
|
boolean |
isAllowCreate()
Return if a new PersistenceManager should be created if no thread-bound found. |
|
boolean |
isExposeNativePersistenceManager()
Return whether to expose the native JDO PersistenceManager to JdoCallback code, or rather a PersistenceManager proxy. |
|
|
makePersistent(T entity)
Make the given transient instance persistent. |
|
|
makePersistentAll(java.util.Collection<T> entities)
Make the given transient instances persistent. |
|
protected
|
postProcessResult(T result,
PersistenceManager pm,
boolean existingTransaction)
Post-process the given result object, which might be a Collection. |
|
void |
prepareQuery(Query query)
Prepare the given JDO query object. |
|
void |
refresh(java.lang.Object entity)
Re-read the state of the given persistent instance. |
|
void |
refreshAll()
Re-read the state of all persistent instances. |
|
void |
refreshAll(java.util.Collection entities)
Re-read the state of all given persistent instances. |
|
void |
setAllowCreate(boolean allowCreate)
Set if a new PersistenceManager should be created when no transactional PersistenceManager can be found for the current thread. |
|
void |
setExposeNativePersistenceManager(boolean exposeNativePersistenceManager)
Set whether to expose the native JDO PersistenceManager to JdoCallback code. |
Methods inherited from class org.springframework.orm.jdo.JdoAccessor |
---|
afterPropertiesSet, convertJdoAccessException, flushIfNecessary, getJdoDialect, getPersistenceManagerFactory, isFlushEager, setFlushEager, setJdoDialect, setPersistenceManagerFactory |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private boolean allowCreate
private boolean exposeNativePersistenceManager
Constructor Detail |
---|
public JdoTemplate()
public JdoTemplate(PersistenceManagerFactory pmf)
pmf
- PersistenceManagerFactory to create PersistenceManagerspublic JdoTemplate(PersistenceManagerFactory pmf, boolean allowCreate)
pmf
- PersistenceManagerFactory to create PersistenceManagersallowCreate
- if a non-transactional PersistenceManager should be created
when no transactional PersistenceManager can be found for the current threadMethod Detail |
---|
public void setAllowCreate(boolean allowCreate)
JdoTemplate is aware of a corresponding PersistenceManager bound to the current thread, for example when using JdoTransactionManager. If allowCreate is true, a new non-transactional PersistenceManager will be created if none found, which needs to be closed at the end of the operation. If false, an IllegalStateException will get thrown in this case.
PersistenceManagerFactoryUtils#getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean)
public boolean isAllowCreate()
public void setExposeNativePersistenceManager(boolean exposeNativePersistenceManager)
close
calls and automatically applying transaction
timeouts (if any).
As there is often a need to cast to a provider-specific PersistenceManager class in DAOs that use provider-specific functionality, the exposed proxy implements all interfaces implemented by the original PersistenceManager. If this is not sufficient, turn this flag to "true".
JdoCallback
,
javax.jdo.PersistenceManager
,
prepareQuery(Query)
public boolean isExposeNativePersistenceManager()
public <T> T execute(JdoCallback<T> action) throws DataAccessException
JdoOperations
Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager like JdoTransactionManager.
execute
in interface JdoOperations
action
- callback object that specifies the JDO action
null
DataAccessException
- in case of JDO errorsJdoTransactionManager
,
org.springframework.dao
,
org.springframework.transaction
,
javax.jdo.PersistenceManager
public java.util.Collection executeFind(JdoCallback<?> action) throws DataAccessException
JdoOperations
executeFind
in interface JdoOperations
action
- callback object that specifies the JDO action
null
DataAccessException
- in case of JDO errorspublic <T> T execute(JdoCallback<T> action, boolean exposeNativePersistenceManager) throws DataAccessException
action
- callback object that specifies the JDO actionexposeNativePersistenceManager
- whether to expose the native
JDO persistence manager to callback code
null
DataAccessException
- in case of JDO errorsprotected PersistenceManager createPersistenceManagerProxy(PersistenceManager pm)
execute
method.
The proxy also prepares returned JDO Query objects.
pm
- the JDO PersistenceManager to create a proxy for
javax.jdo.PersistenceManager#close()
,
execute(JdoCallback, boolean)
,
prepareQuery(Query)
protected <T> T postProcessResult(T result, PersistenceManager pm, boolean existingTransaction)
execute
method.
Default implementation always returns the passed-in Object as-is. Subclasses might override this to automatically detach result collections or even single result objects.
pm
- the current JDO PersistenceManagerresult
- the result object (might be a Collection)existingTransaction
- if executing within an existing transaction
(within an existing JDO PersistenceManager that won't be closed immediately)
execute(JdoCallback, boolean)
public java.lang.Object getObjectById(java.lang.Object objectId) throws DataAccessException
JdoOperations
A JDO object id identifies both the persistent class and the id within the namespace of that class.
getObjectById
in interface JdoOperations
objectId
- a JDO object id of the persistent instance
ObjectRetrievalFailureException
- if not found
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#getObjectById(Object, boolean)
public <T> T getObjectById(java.lang.Class<T> entityClass, java.lang.Object idValue) throws DataAccessException
JdoOperations
The given id value is typically just unique within the namespace of the persistent class. Its toString value must correspond to the toString value of the corresponding JDO object id.
Usually, the passed-in value will have originated from the primary key field of a persistent object that uses JDO's application identity.
getObjectById
in interface JdoOperations
entityClass
- a persistent classidValue
- an id value of the persistent instance
ObjectRetrievalFailureException
- if not found
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#getObjectById(Object, boolean)
,
javax.jdo.PersistenceManager#getObjectById(Class, Object)
public void evict(java.lang.Object entity) throws DataAccessException
JdoOperations
evict
in interface JdoOperations
entity
- the persistent instance to evict
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#evict(Object)
public void evictAll(java.util.Collection entities) throws DataAccessException
JdoOperations
evictAll
in interface JdoOperations
entities
- the persistent instances to evict
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#evictAll(java.util.Collection)
public void evictAll() throws DataAccessException
JdoOperations
evictAll
in interface JdoOperations
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#evictAll()
public void refresh(java.lang.Object entity) throws DataAccessException
JdoOperations
refresh
in interface JdoOperations
entity
- the persistent instance to re-read
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#refresh(Object)
public void refreshAll(java.util.Collection entities) throws DataAccessException
JdoOperations
refreshAll
in interface JdoOperations
entities
- the persistent instances to re-read
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#refreshAll(java.util.Collection)
public void refreshAll() throws DataAccessException
JdoOperations
refreshAll
in interface JdoOperations
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#refreshAll()
public <T> T makePersistent(T entity) throws DataAccessException
JdoOperations
makePersistent
in interface JdoOperations
entity
- the transient instance to make persistent
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#makePersistent(Object)
public <T> java.util.Collection<T> makePersistentAll(java.util.Collection<T> entities) throws DataAccessException
JdoOperations
makePersistentAll
in interface JdoOperations
entities
- the transient instances to make persistent
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#makePersistentAll(java.util.Collection)
public void deletePersistent(java.lang.Object entity) throws DataAccessException
JdoOperations
deletePersistent
in interface JdoOperations
entity
- the persistent instance to delete
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#deletePersistent(Object)
public void deletePersistentAll(java.util.Collection entities) throws DataAccessException
JdoOperations
This can be combined with any of the find methods to delete by query in two lines of code.
deletePersistentAll
in interface JdoOperations
entities
- the persistent instances to delete
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#deletePersistentAll(java.util.Collection)
public <T> T detachCopy(T entity)
JdoOperations
detachCopy
in interface JdoOperations
entity
- the persistent instance to detach
javax.jdo.PersistenceManager#detachCopy(Object)
public <T> java.util.Collection<T> detachCopyAll(java.util.Collection<T> entities)
JdoOperations
detachCopyAll
in interface JdoOperations
entities
- the persistent instances to detach
javax.jdo.PersistenceManager#detachCopyAll(Collection)
public void flush() throws DataAccessException
JdoOperations
Only invoke this for selective eager flushing, for example when JDBC code needs to see certain changes within the same transaction. Else, it's preferable to rely on auto-flushing at transaction completion.
flush
in interface JdoOperations
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#flush()
,
JdoDialect#flush(javax.jdo.PersistenceManager)
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent class
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class)
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass, java.lang.String filter) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent classfilter
- the JDOQL filter to match (or null
if none)
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class, String)
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass, java.lang.String filter, java.lang.String ordering) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent classfilter
- the JDOQL filter to match (or null
if none)ordering
- the ordering of the result (or null
if none)
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class, String)
,
javax.jdo.Query#setOrdering
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass, java.lang.String filter, java.lang.String parameters, java.lang.Object... values) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent classfilter
- the JDOQL filter to matchparameters
- the JDOQL parameter declarationsvalues
- the corresponding parameter values
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class, String)
,
javax.jdo.Query#declareParameters
,
javax.jdo.Query#executeWithArray
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass, java.lang.String filter, java.lang.String parameters, java.lang.Object[] values, java.lang.String ordering) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent classfilter
- the JDOQL filter to matchparameters
- the JDOQL parameter declarationsvalues
- the corresponding parameter valuesordering
- the ordering of the result (or null
if none)
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class, String)
,
javax.jdo.Query#declareParameters
,
javax.jdo.Query#executeWithArray
,
javax.jdo.Query#setOrdering
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass, java.lang.String filter, java.lang.String parameters, java.util.Map<java.lang.String,?> values) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent classfilter
- the JDOQL filter to matchparameters
- the JDOQL parameter declarationsvalues
- a Map with parameter names as keys and parameter values
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class, String)
,
javax.jdo.Query#declareParameters
,
javax.jdo.Query#executeWithMap
public <T> java.util.Collection<T> find(java.lang.Class<T> entityClass, java.lang.String filter, java.lang.String parameters, java.util.Map<java.lang.String,?> values, java.lang.String ordering) throws DataAccessException
JdoOperations
find
in interface JdoOperations
entityClass
- a persistent classfilter
- the JDOQL filter to matchparameters
- the JDOQL parameter declarationsvalues
- a Map with parameter names as keys and parameter valuesordering
- the ordering of the result (or null
if none)
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(Class, String)
,
javax.jdo.Query#declareParameters
,
javax.jdo.Query#executeWithMap
,
javax.jdo.Query#setOrdering
public java.util.Collection find(java.lang.String language, java.lang.Object queryObject) throws DataAccessException
JdoOperations
find
in interface JdoOperations
language
- the query language (javax.jdo.Query#JDOQL
or javax.jdo.Query#SQL
, for example)queryObject
- the query object for the specified language
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(String, Object)
,
javax.jdo.Query#JDOQL
,
javax.jdo.Query#SQL
public java.util.Collection find(java.lang.String queryString) throws DataAccessException
JdoOperations
find
in interface JdoOperations
queryString
- the single-string JDOQL query
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(String)
public java.util.Collection find(java.lang.String queryString, java.lang.Object... values) throws DataAccessException
JdoOperations
find
in interface JdoOperations
queryString
- the single-string JDOQL queryvalues
- the corresponding parameter values
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(String)
public java.util.Collection find(java.lang.String queryString, java.util.Map<java.lang.String,?> values) throws DataAccessException
JdoOperations
find
in interface JdoOperations
queryString
- the single-string JDOQL queryvalues
- a Map with parameter names as keys and parameter values
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newQuery(String)
public <T> java.util.Collection<T> findByNamedQuery(java.lang.Class<T> entityClass, java.lang.String queryName) throws DataAccessException
JdoOperations
findByNamedQuery
in interface JdoOperations
entityClass
- a persistent classqueryName
- the name of the query
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newNamedQuery(Class, String)
public <T> java.util.Collection<T> findByNamedQuery(java.lang.Class<T> entityClass, java.lang.String queryName, java.lang.Object... values) throws DataAccessException
JdoOperations
findByNamedQuery
in interface JdoOperations
entityClass
- a persistent classqueryName
- the name of the queryvalues
- the corresponding parameter values
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newNamedQuery(Class, String)
public <T> java.util.Collection<T> findByNamedQuery(java.lang.Class<T> entityClass, java.lang.String queryName, java.util.Map<java.lang.String,?> values) throws DataAccessException
JdoOperations
findByNamedQuery
in interface JdoOperations
entityClass
- a persistent classqueryName
- the name of the queryvalues
- a Map with parameter names as keys and parameter values
DataAccessException
- in case of JDO errorsjavax.jdo.PersistenceManager#newNamedQuery(Class, String)
public void prepareQuery(Query query) throws JDOException
Applies a transaction timeout, if any. If you don't use such timeouts, the call is a no-op.
In general, prefer a proxied PersistenceManager instead, which will automatically apply the transaction timeout (through the use of a special PersistenceManager proxy). You need to set the "exposeNativePersistenceManager" property to "false" to activate this. Note that you won't be able to cast to a provider-specific JDO PersistenceManager class anymore then.
query
- the JDO query object
JDOException
- if the query could not be properly preparedJdoCallback.doInJdo(PersistenceManager)
,
PersistenceManagerFactoryUtils.applyTransactionTimeout(Query, PersistenceManagerFactory, org.springframework.orm.jdo.JdoDialect)
,
setExposeNativePersistenceManager(boolean)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |