org.springframework.orm.ojb
Class PersistenceBrokerTemplate

java.lang.Object
  extended byorg.springframework.jdbc.support.JdbcAccessor
      extended byorg.springframework.orm.ojb.OjbAccessor
          extended byorg.springframework.orm.ojb.PersistenceBrokerTemplate
All Implemented Interfaces:
PersistenceBrokerOperations

public class PersistenceBrokerTemplate
extends OjbAccessor
implements PersistenceBrokerOperations

Helper class that simplifies OJB PersistenceBroker data access code, and converts PersistenceBrokerExceptions into exceptions compatible to the org.springframework.dao exception hierarchy.

The central method is "execute", supporting OJB code implementing the PersistenceBrokerCallback interface. It provides PersistenceBroker handling such that neither the PersistenceBrokerCallback implementation nor the calling code needs to explicitly care about retrieving/closing PersistenceBrokers, or handling OJB lifecycle exceptions.

Typically used to implement data access or business logic services that use OJB within their implementation but are OJB-agnostic in their interface. The latter resp. code calling the latter only have to deal with business objects, query objects, and org.springframework.dao exceptions.

Since:
02.07.2004
Author:
Juergen Hoeller
See Also:
PersistenceBrokerCallback

Constructor Summary
PersistenceBrokerTemplate()
          Create a new PersistenceBrokerTemplate, using the default connection configured for OJB.
PersistenceBrokerTemplate(boolean allowCreate)
          Create a new PersistenceBrokerTemplate, using the default connection configured for OJB.
PersistenceBrokerTemplate(org.apache.ojb.broker.PBKey pbKey)
          Create a new PersistenceBrokerTemplate.
PersistenceBrokerTemplate(org.apache.ojb.broker.PBKey pbKey, boolean allowCreate)
          Create a new PersistenceBrokerTemplate.
 
Method Summary
 void clearCache()
           
protected  void closePersistenceBrokerIfNecessary(org.apache.ojb.broker.PersistenceBroker pb)
          Close the given PersistenceBroker, created for the PBKey of this template, if it isn't bound to the thread.
 void delete(java.lang.Object entity)
           
 void deleteByQuery(org.apache.ojb.broker.query.Query query)
           
 java.lang.Object execute(PersistenceBrokerCallback action)
          Execute the action specified by the given action object within a PersistenceBroker.
 java.util.Collection executeFind(PersistenceBrokerCallback action)
          Execute the specified action assuming that the result object is a Collection.
 java.util.Collection getCollectionByQuery(org.apache.ojb.broker.query.Query query)
           
 int getCount(org.apache.ojb.broker.query.Query query)
           
 java.util.Iterator getIteratorByQuery(org.apache.ojb.broker.query.Query query)
           
 java.lang.Object getObjectByQuery(org.apache.ojb.broker.query.Query query)
           
protected  org.apache.ojb.broker.PersistenceBroker getPersistenceBroker()
          Get an OJB PersistenceBroker for the PBKey of this template.
 java.util.Iterator getReportQueryIteratorByQuery(org.apache.ojb.broker.query.Query query)
           
 boolean isAllowCreate()
          Return if a new PersistenceBroker should be created if no thread-bound found.
 void removeFromCache(java.lang.Object entityOrId)
           
 void setAllowCreate(boolean allowCreate)
          Set if a new PersistenceBroker should be created if no thread-bound found.
 void store(java.lang.Object entity)
           
 
Methods inherited from class org.springframework.orm.ojb.OjbAccessor
convertJdbcAccessException, convertOjbAccessException, getJcdAlias, getPbKey, setJcdAlias, setPbKey
 
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor
afterPropertiesSet, getDataSource, getExceptionTranslator, setDataSource, setExceptionTranslator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistenceBrokerTemplate

public PersistenceBrokerTemplate()
Create a new PersistenceBrokerTemplate, using the default connection configured for OJB. Can be further configured via bean properties.


PersistenceBrokerTemplate

public PersistenceBrokerTemplate(boolean allowCreate)
Create a new PersistenceBrokerTemplate, using the default connection configured for OJB.

Parameters:
allowCreate - if a new PersistenceBroker should be created if no thread-bound found

PersistenceBrokerTemplate

public PersistenceBrokerTemplate(org.apache.ojb.broker.PBKey pbKey)
Create a new PersistenceBrokerTemplate.

Parameters:
pbKey - the PBKey of the PersistenceBroker configuration to use

PersistenceBrokerTemplate

public PersistenceBrokerTemplate(org.apache.ojb.broker.PBKey pbKey,
                                 boolean allowCreate)
Create a new PersistenceBrokerTemplate.

Parameters:
pbKey - the PBKey of the PersistenceBroker configuration to use
allowCreate - if a new PersistenceBroker should be created if no thread-bound found
Method Detail

setAllowCreate

public void setAllowCreate(boolean allowCreate)
Set if a new PersistenceBroker should be created if no thread-bound found.

PersistenceBrokerTemplate is aware of a respective PersistenceBroker bound to the current thread, for example when using PersistenceBrokerTransactionManager. If allowCreate is true, a new PersistenceBroker will be created if none found. If false, an IllegalStateException will get thrown in this case.

See Also:
PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean)

isAllowCreate

public boolean isAllowCreate()
Return if a new PersistenceBroker should be created if no thread-bound found.


getPersistenceBroker

protected org.apache.ojb.broker.PersistenceBroker getPersistenceBroker()
                                                                throws DataAccessResourceFailureException,
                                                                       java.lang.IllegalStateException
Get an OJB PersistenceBroker for the PBKey of this template.

Default implementation delegates to OjbFactoryUtils. Can be overridden in subclasses, e.g. for testing purposes.

Returns:
the PersistenceBroker
Throws:
DataAccessResourceFailureException - if the PersistenceBroker couldn't be created
java.lang.IllegalStateException - if no thread-bound PersistenceBroker found and allowCreate false
See Also:
OjbAccessor.setJcdAlias(java.lang.String), OjbAccessor.setPbKey(org.apache.ojb.broker.PBKey), setAllowCreate(boolean), OjbFactoryUtils.getPersistenceBroker(PBKey, boolean)

closePersistenceBrokerIfNecessary

protected void closePersistenceBrokerIfNecessary(org.apache.ojb.broker.PersistenceBroker pb)
Close the given PersistenceBroker, created for the PBKey of this template, if it isn't bound to the thread.

Default implementation delegates to OjbFactoryUtils. Can be overridden in subclasses, e.g. for testing purposes.

Parameters:
pb - PersistenceBroker to close
See Also:
OjbAccessor.setJcdAlias(java.lang.String), OjbAccessor.setPbKey(org.apache.ojb.broker.PBKey), OjbFactoryUtils.closePersistenceBrokerIfNecessary(org.apache.ojb.broker.PersistenceBroker, org.apache.ojb.broker.PBKey)

execute

public java.lang.Object execute(PersistenceBrokerCallback action)
                         throws DataAccessException
Description copied from interface: PersistenceBrokerOperations
Execute the action specified by the given action object within a PersistenceBroker. Application exceptions thrown by the action object get propagated to the caller (can only be unchecked). OJB exceptions are transformed into appropriate DAO ones. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager like PersistenceBrokerTransactionManager.

Specified by:
execute in interface PersistenceBrokerOperations
Parameters:
action - action object that specifies the OJB action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - in case of OJB errors
See Also:
PersistenceBrokerTransactionManager, org.springframework.dao, org.springframework.transaction

executeFind

public java.util.Collection executeFind(PersistenceBrokerCallback action)
                                 throws DataAccessException
Description copied from interface: PersistenceBrokerOperations
Execute the specified action assuming that the result object is a Collection. This is a convenience method for executing OJB queries within an action.

Specified by:
executeFind in interface PersistenceBrokerOperations
Parameters:
action - action object that specifies the OJB action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - in case of OJB errors

getObjectByQuery

public java.lang.Object getObjectByQuery(org.apache.ojb.broker.query.Query query)
                                  throws DataAccessException
Specified by:
getObjectByQuery in interface PersistenceBrokerOperations
Throws:
DataAccessException

getCollectionByQuery

public java.util.Collection getCollectionByQuery(org.apache.ojb.broker.query.Query query)
                                          throws DataAccessException
Specified by:
getCollectionByQuery in interface PersistenceBrokerOperations
Throws:
DataAccessException

getIteratorByQuery

public java.util.Iterator getIteratorByQuery(org.apache.ojb.broker.query.Query query)
                                      throws DataAccessException
Specified by:
getIteratorByQuery in interface PersistenceBrokerOperations
Throws:
DataAccessException

getReportQueryIteratorByQuery

public java.util.Iterator getReportQueryIteratorByQuery(org.apache.ojb.broker.query.Query query)
Specified by:
getReportQueryIteratorByQuery in interface PersistenceBrokerOperations

getCount

public int getCount(org.apache.ojb.broker.query.Query query)
             throws DataAccessException
Specified by:
getCount in interface PersistenceBrokerOperations
Throws:
DataAccessException

removeFromCache

public void removeFromCache(java.lang.Object entityOrId)
                     throws DataAccessException
Specified by:
removeFromCache in interface PersistenceBrokerOperations
Throws:
DataAccessException

clearCache

public void clearCache()
                throws DataAccessException
Specified by:
clearCache in interface PersistenceBrokerOperations
Throws:
DataAccessException

store

public void store(java.lang.Object entity)
           throws DataAccessException
Specified by:
store in interface PersistenceBrokerOperations
Throws:
DataAccessException

delete

public void delete(java.lang.Object entity)
            throws DataAccessException
Specified by:
delete in interface PersistenceBrokerOperations
Throws:
DataAccessException

deleteByQuery

public void deleteByQuery(org.apache.ojb.broker.query.Query query)
                   throws DataAccessException
Specified by:
deleteByQuery in interface PersistenceBrokerOperations
Throws:
DataAccessException


Copyright (C) 2003-2004 The Spring Framework Project.