Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » orm » jdo » [javadoc | source]
org.springframework.orm.jdo
public class: JdoTemplate [javadoc | source]
java.lang.Object
   org.springframework.orm.jdo.JdoAccessor
      org.springframework.orm.jdo.JdoTemplate

All Implemented Interfaces:
    JdoOperations, InitializingBean

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.

Fields inherited from org.springframework.orm.jdo.JdoAccessor:
logger
Constructor:
 public JdoTemplate() 
 public JdoTemplate(PersistenceManagerFactory pmf) 
    Create a new JdoTemplate instance.
    Parameters:
    pmf - PersistenceManagerFactory to create PersistenceManagers
 public JdoTemplate(PersistenceManagerFactory pmf,
    boolean allowCreate) 
    Create a new JdoTemplate instance.
    Parameters:
    pmf - PersistenceManagerFactory to create PersistenceManagers
    allowCreate - if a non-transactional PersistenceManager should be created when no transactional PersistenceManager can be found for the current thread
Method from org.springframework.orm.jdo.JdoTemplate Summary:
attachCopy,   attachCopyAll,   createPersistenceManagerProxy,   deletePersistent,   deletePersistentAll,   detachCopy,   detachCopyAll,   evict,   evictAll,   evictAll,   execute,   execute,   executeFind,   find,   find,   find,   find,   find,   find,   find,   find,   find,   find,   find,   findByNamedQuery,   findByNamedQuery,   findByNamedQuery,   flush,   getObjectById,   getObjectById,   isAllowCreate,   isExposeNativePersistenceManager,   makePersistent,   makePersistentAll,   postProcessResult,   prepareQuery,   refresh,   refreshAll,   refreshAll,   setAllowCreate,   setExposeNativePersistenceManager
Methods from org.springframework.orm.jdo.JdoAccessor:
afterPropertiesSet,   convertJdoAccessException,   flushIfNecessary,   getJdoDialect,   getPersistenceManagerFactory,   isFlushEager,   setFlushEager,   setJdoDialect,   setPersistenceManagerFactory
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.orm.jdo.JdoTemplate Detail:
 public Object attachCopy(Object detachedEntity) 
Deprecated! in - favor of #makePersistent(Object) . To be removed in Spring 3.0.

 public Collection attachCopyAll(Collection detachedEntities) 
Deprecated! in - favor of #makePersistentAll(java.util.Collection) . To be removed in Spring 3.0.

 protected PersistenceManager createPersistenceManagerProxy(PersistenceManager pm) 
    Create a close-suppressing proxy for the given JDO PersistenceManager. Called by the execute method.

    The proxy also prepares returned JDO Query objects.

 public  void deletePersistent(Object entity) throws DataAccessException 
 public  void deletePersistentAll(Collection entities) throws DataAccessException 
 public Object detachCopy(Object entity) 
 public Collection detachCopyAll(Collection entities) 
 public  void evict(Object entity) throws DataAccessException 
 public  void evictAll() throws DataAccessException 
 public  void evictAll(Collection entities) throws DataAccessException 
 public Object execute(JdoCallback action) throws DataAccessException 
 public Object execute(JdoCallback action,
    boolean exposeNativePersistenceManager) throws DataAccessException 
    Execute the action specified by the given action object within a PersistenceManager.
 public Collection executeFind(JdoCallback action) throws DataAccessException 
 public Collection find(Class entityClass) throws DataAccessException 
 public Collection find(String queryString) throws DataAccessException 
 public Collection find(Class entityClass,
    String filter) throws DataAccessException 
 public Collection find(String language,
    Object queryObject) throws DataAccessException 
 public Collection find(String queryString,
    Object[] values) throws DataAccessException 
 public Collection find(String queryString,
    Map values) throws DataAccessException 
 public Collection find(Class entityClass,
    String filter,
    String ordering) throws DataAccessException 
 public Collection find(Class entityClass,
    String filter,
    String parameters,
    Object[] values) throws DataAccessException 
 public Collection find(Class entityClass,
    String filter,
    String parameters,
    Map values) throws DataAccessException 
 public Collection find(Class entityClass,
    String filter,
    String parameters,
    Object[] values,
    String ordering) throws DataAccessException 
 public Collection find(Class entityClass,
    String filter,
    String parameters,
    Map values,
    String ordering) throws DataAccessException 
 public Collection findByNamedQuery(Class entityClass,
    String queryName) throws DataAccessException 
 public Collection findByNamedQuery(Class entityClass,
    String queryName,
    Object[] values) throws DataAccessException 
 public Collection findByNamedQuery(Class entityClass,
    String queryName,
    Map values) throws DataAccessException 
 public  void flush() throws DataAccessException 
 public Object getObjectById(Object objectId) throws DataAccessException 
 public Object getObjectById(Class entityClass,
    Object idValue) throws DataAccessException 
 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) throws DataAccessException 
 public Collection makePersistentAll(Collection entities) throws DataAccessException 
 protected Object postProcessResult(Object result,
    PersistenceManager pm,
    boolean existingTransaction) 
    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 
    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 
 public  void refreshAll() throws DataAccessException 
 public  void refreshAll(Collection entities) throws DataAccessException 
 public  void setAllowCreate(boolean 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) 
    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".