Helper class that simplifies JDO data access code, and converts
JDOExceptions into Spring DataAccessExceptions, following the
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.
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.
| Method from org.springframework.orm.jdo.JdoTemplate Detail: |
public Object attachCopy(Object detachedEntity) {
return makePersistent(detachedEntity);
} Deprecated! in - favor of #makePersistent(Object) .
To be removed in Spring 3.0.
|
public Collection attachCopyAll(Collection detachedEntities) {
return makePersistentAll(detachedEntities);
} Deprecated! in - favor of #makePersistentAll(java.util.Collection) .
To be removed in Spring 3.0.
|
protected PersistenceManager createPersistenceManagerProxy(PersistenceManager pm) {
Class[] ifcs = ClassUtils.getAllInterfacesForClass(pm.getClass(), getClass().getClassLoader());
return (PersistenceManager) Proxy.newProxyInstance(
pm.getClass().getClassLoader(), ifcs, new CloseSuppressingInvocationHandler(pm));
}
|
public void deletePersistent(Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.deletePersistent(entity);
return null;
}
}, true);
}
|
public void deletePersistentAll(Collection entities) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.deletePersistentAll(entities);
return null;
}
}, true);
}
|
public Object detachCopy(Object entity) {
return execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.detachCopy(entity);
}
}, true);
}
|
public Collection detachCopyAll(Collection entities) {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.detachCopyAll(entities);
}
}, true);
}
|
public void evict(Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evict(entity);
return null;
}
}, true);
}
|
public void evictAll() throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evictAll();
return null;
}
}, true);
}
|
public void evictAll(Collection entities) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evictAll(entities);
return null;
}
}, true);
}
|
public Object execute(JdoCallback action) throws DataAccessException {
return execute(action, isExposeNativePersistenceManager());
}
|
public Object execute(JdoCallback action,
boolean exposeNativePersistenceManager) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(
getPersistenceManagerFactory(), isAllowCreate());
boolean existingTransaction =
TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory());
try {
PersistenceManager pmToExpose = (exposeNativePersistenceManager ? pm : createPersistenceManagerProxy(pm));
Object result = action.doInJdo(pmToExpose);
flushIfNecessary(pm, existingTransaction);
return postProcessResult(result, pm, existingTransaction);
}
catch (JDOException ex) {
throw convertJdoAccessException(ex);
}
catch (RuntimeException ex) {
// callback code threw application exception
throw ex;
}
finally {
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
}
}
Execute the action specified by the given action object within a
PersistenceManager. |
public Collection executeFind(JdoCallback action) throws DataAccessException {
Object result = execute(action, isExposeNativePersistenceManager());
if (result != null && !(result instanceof Collection)) {
throw new InvalidDataAccessApiUsageException(
"Result object returned from JdoCallback isn't a Collection: [" + result + "]");
}
return (Collection) result;
}
|
public Collection find(Class entityClass) throws DataAccessException {
return find(entityClass, null, null);
}
|
public Collection find(String queryString) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(queryString);
prepareQuery(query);
return query.execute();
}
}, true);
}
|
public Collection find(Class entityClass,
String filter) throws DataAccessException {
return find(entityClass, filter, null);
}
|
public Collection find(String language,
Object queryObject) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(language, queryObject);
prepareQuery(query);
return query.execute();
}
}, true);
}
|
public Collection find(String queryString,
Object[] values) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(queryString);
prepareQuery(query);
return query.executeWithArray(values);
}
}, true);
}
|
public Collection find(String queryString,
Map values) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(queryString);
prepareQuery(query);
return query.executeWithMap(values);
}
}, true);
}
|
public Collection find(Class entityClass,
String filter,
String ordering) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = (filter != null ? pm.newQuery(entityClass, filter) : pm.newQuery(entityClass));
prepareQuery(query);
if (ordering != null) {
query.setOrdering(ordering);
}
return query.execute();
}
}, true);
}
|
public Collection find(Class entityClass,
String filter,
String parameters,
Object[] values) throws DataAccessException {
return find(entityClass, filter, parameters, values, null);
}
|
public Collection find(Class entityClass,
String filter,
String parameters,
Map values) throws DataAccessException {
return find(entityClass, filter, parameters, values, null);
}
|
public Collection find(Class entityClass,
String filter,
String parameters,
Object[] values,
String ordering) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(entityClass, filter);
prepareQuery(query);
query.declareParameters(parameters);
if (ordering != null) {
query.setOrdering(ordering);
}
return query.executeWithArray(values);
}
}, true);
}
|
public Collection find(Class entityClass,
String filter,
String parameters,
Map values,
String ordering) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(entityClass, filter);
prepareQuery(query);
query.declareParameters(parameters);
if (ordering != null) {
query.setOrdering(ordering);
}
return query.executeWithMap(values);
}
}, true);
}
|
public Collection findByNamedQuery(Class entityClass,
String queryName) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newNamedQuery(entityClass, queryName);
prepareQuery(query);
return query.execute();
}
}, true);
}
|
public Collection findByNamedQuery(Class entityClass,
String queryName,
Object[] values) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newNamedQuery(entityClass, queryName);
prepareQuery(query);
return query.executeWithArray(values);
}
}, true);
}
|
public Collection findByNamedQuery(Class entityClass,
String queryName,
Map values) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newNamedQuery(entityClass, queryName);
prepareQuery(query);
return query.executeWithMap(values);
}
}, true);
}
|
public void flush() throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
getJdoDialect().flush(pm);
return null;
}
}, true);
}
|
public Object getObjectById(Object objectId) throws DataAccessException {
return execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.getObjectById(objectId, true);
}
}, true);
}
|
public Object getObjectById(Class entityClass,
Object idValue) throws DataAccessException {
return execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.getObjectById(entityClass, idValue);
}
}, true);
}
|
public boolean isAllowCreate() {
return this.allowCreate;
}
Return if a new PersistenceManager should be created if no thread-bound found. |
public boolean isExposeNativePersistenceManager() {
return this.exposeNativePersistenceManager;
}
Return whether to expose the native JDO PersistenceManager to JdoCallback
code, or rather a PersistenceManager proxy. |
public Object makePersistent(Object entity) throws DataAccessException {
return execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.makePersistent(entity);
}
}, true);
}
|
public Collection makePersistentAll(Collection entities) throws DataAccessException {
return (Collection) execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.makePersistentAll(entities);
}
}, true);
}
|
protected Object postProcessResult(Object result,
PersistenceManager pm,
boolean existingTransaction) {
return result;
}
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. |
public void prepareQuery(Query query) throws JDOException {
PersistenceManagerFactoryUtils.applyTransactionTimeout(
query, getPersistenceManagerFactory(), getJdoDialect());
}
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. |
public void refresh(Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refresh(entity);
return null;
}
}, true);
}
|
public void refreshAll() throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refreshAll();
return null;
}
}, true);
}
|
public void refreshAll(Collection entities) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refreshAll(entities);
return null;
}
}, true);
}
|
public void setAllowCreate(boolean allowCreate) {
this.allowCreate = allowCreate;
}
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. |
public void setExposeNativePersistenceManager(boolean exposeNativePersistenceManager) {
this.exposeNativePersistenceManager = exposeNativePersistenceManager;
}
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". |