| java.lang.Object org.springframework.orm.jdo.JdoAccessor org.springframework.orm.jdo.JdoTemplate
JdoTemplate | public class JdoTemplate extends JdoAccessor implements JdoOperations(Code) | | 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.
author: Juergen Hoeller since: 03.06.2003 See Also: JdoTemplate.setPersistenceManagerFactory See Also: JdoCallback See Also: javax.jdo.PersistenceManager See Also: LocalPersistenceManagerFactoryBean See Also: JdoTransactionManager See Also: org.springframework.transaction.jta.JtaTransactionManager See Also: org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter See Also: org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor |
Constructor Summary | |
public | JdoTemplate() Create a new JdoTemplate instance. | public | JdoTemplate(PersistenceManagerFactory pmf) Create a new JdoTemplate instance. | public | JdoTemplate(PersistenceManagerFactory pmf, boolean allowCreate) Create a new JdoTemplate instance. |
Method Summary | |
public Object | attachCopy(Object detachedEntity) | public Collection | attachCopyAll(Collection detachedEntities) | protected PersistenceManager | createPersistenceManagerProxy(PersistenceManager pm) Create a close-suppressing proxy for the given JDO PersistenceManager. | public void | deletePersistent(Object entity) | public void | deletePersistentAll(Collection entities) | public Object | detachCopy(Object entity) | public Collection | detachCopyAll(Collection entities) | public void | evict(Object entity) | public void | evictAll(Collection entities) | public void | evictAll() | public Object | execute(JdoCallback action) | public Object | execute(JdoCallback action, boolean exposeNativePersistenceManager) Execute the action specified by the given action object within a
PersistenceManager. | public Collection | executeFind(JdoCallback action) | public Collection | find(Class entityClass) | public Collection | find(Class entityClass, String filter) | public Collection | find(Class entityClass, String filter, String ordering) | public Collection | find(Class entityClass, String filter, String parameters, Object[] values) | public Collection | find(Class entityClass, String filter, String parameters, Object[] values, String ordering) | public Collection | find(Class entityClass, String filter, String parameters, Map values) | public Collection | find(Class entityClass, String filter, String parameters, Map values, String ordering) | public Collection | find(String language, Object queryObject) | public Collection | find(String queryString) | public Collection | find(String queryString, Object[] values) | public Collection | find(String queryString, Map values) | public Collection | findByNamedQuery(Class entityClass, String queryName) | public Collection | findByNamedQuery(Class entityClass, String queryName, Object[] values) | public Collection | findByNamedQuery(Class entityClass, String queryName, Map values) | public void | flush() | public Object | getObjectById(Object objectId) | public Object | getObjectById(Class entityClass, Object idValue) | public boolean | isAllowCreate() Return if a new PersistenceManager should be created if no thread-bound found. | public boolean | isExposeNativePersistenceManager() Return whether to expose the native JDO PersistenceManager to JdoCallback
code, or rather a PersistenceManager proxy. | public Object | makePersistent(Object entity) | public Collection | makePersistentAll(Collection entities) | protected Object | postProcessResult(Object result, PersistenceManager pm, boolean existingTransaction) Post-process the given result object, which might be a Collection. | public void | prepareQuery(Query query) Prepare the given JDO query object. | public void | refresh(Object entity) | public void | refreshAll(Collection entities) | public void | refreshAll() | public void | setAllowCreate(boolean allowCreate) Set if a new PersistenceManager should be created when no transactional
PersistenceManager can be found for the current thread. | public void | setExposeNativePersistenceManager(boolean exposeNativePersistenceManager) Set whether to expose the native JDO PersistenceManager to JdoCallback
code. |
JdoTemplate | public JdoTemplate()(Code) | | Create a new JdoTemplate instance.
|
JdoTemplate | public JdoTemplate(PersistenceManagerFactory pmf)(Code) | | Create a new JdoTemplate instance.
Parameters: pmf - PersistenceManagerFactory to create PersistenceManagers |
JdoTemplate | public JdoTemplate(PersistenceManagerFactory pmf, boolean allowCreate)(Code) | | Create a new JdoTemplate instance.
Parameters: pmf - PersistenceManagerFactory to create PersistenceManagers Parameters: allowCreate - if a non-transactional PersistenceManager should be createdwhen no transactional PersistenceManager can be found for the current thread |
createPersistenceManagerProxy | protected PersistenceManager createPersistenceManagerProxy(PersistenceManager pm)(Code) | | Create a close-suppressing proxy for the given JDO PersistenceManager.
Called by the execute method.
The proxy also prepares returned JDO Query objects.
Parameters: pm - the JDO PersistenceManager to create a proxy for the PersistenceManager proxy, implementing all interfacesimplemented by the passed-in PersistenceManager object (that is,also implementing all provider-specific extension interfaces) See Also: javax.jdo.PersistenceManager.close See Also: JdoTemplate.execute(JdoCallback,boolean) See Also: JdoTemplate.prepareQuery |
execute | public Object execute(JdoCallback action, boolean exposeNativePersistenceManager) throws DataAccessException(Code) | | Execute the action specified by the given action object within a
PersistenceManager.
Parameters: action - callback object that specifies the JDO action Parameters: exposeNativePersistenceManager - whether to expose the nativeJDO persistence manager to callback code a result object returned by the action, or null throws: org.springframework.dao.DataAccessException - in case of JDO errors |
isAllowCreate | public boolean isAllowCreate()(Code) | | Return if a new PersistenceManager should be created if no thread-bound found.
|
isExposeNativePersistenceManager | public boolean isExposeNativePersistenceManager()(Code) | | Return whether to expose the native JDO PersistenceManager to JdoCallback
code, or rather a PersistenceManager proxy.
|
postProcessResult | protected Object postProcessResult(Object result, PersistenceManager pm, boolean existingTransaction)(Code) | | Post-process the given result object, which might be a Collection.
Called by the 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.
Parameters: pm - the current JDO PersistenceManager Parameters: result - the result object (might be a Collection) Parameters: existingTransaction - if executing within an existing transaction(within an existing JDO PersistenceManager that won't be closed immediately) the post-processed result object (can be simply be the passed-in object) See Also: JdoTemplate.execute(JdoCallback,boolean) |
prepareQuery | public void prepareQuery(Query query) throws JDOException(Code) | | Prepare the given JDO query object. To be used within a JdoCallback.
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.
Parameters: query - the JDO query object throws: JDOException - if the query could not be properly prepared See Also: JdoCallback.doInJdo See Also: PersistenceManagerFactoryUtils.applyTransactionTimeout See Also: JdoTemplate.setExposeNativePersistenceManager |
setAllowCreate | public void setAllowCreate(boolean allowCreate)(Code) | | Set if a new PersistenceManager should be created when no transactional
PersistenceManager can be found for the current thread.
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.
See Also: PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactoryboolean) |
setExposeNativePersistenceManager | public void setExposeNativePersistenceManager(boolean exposeNativePersistenceManager)(Code) | | Set whether to expose the native JDO PersistenceManager to JdoCallback
code. Default is "false": a PersistenceManager proxy will be returned,
suppressing 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".
See Also: JdoCallback See Also: javax.jdo.PersistenceManager See Also: JdoTemplate.prepareQuery |
Fields inherited from org.springframework.orm.jdo.JdoAccessor | final protected Log logger(Code)(Java Doc)
|
|
|