|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.support.JdbcAccessor org.springframework.orm.ojb.OjbAccessor org.springframework.orm.ojb.PersistenceBrokerTemplate
public class PersistenceBrokerTemplate
Helper class that simplifies OJB PersistenceBroker data access code,
and converts PersistenceBrokerExceptions into Spring DataAccessExceptions,
following 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 or code calling the latter only have to deal with business objects,
query objects, and org.springframework.dao
exceptions.
Note that operations that return an Iterator (that is,
getIteratorByQuery
and getReportQueryIteratorByQuery
)
are supposed to be used within Spring-managed transactions
(with PersistenceBrokerTransactionManager or JtaTransactionManager).
Else, the Iterator won't be able to read results from its ResultSet anymore,
as the underlying PersistenceBroker will already have been closed.
OjbAccessor.setPbKey(org.apache.ojb.broker.PBKey)
,
PersistenceBrokerCallback
,
PersistenceBroker
,
getIteratorByQuery(org.apache.ojb.broker.query.Query)
,
getReportQueryIteratorByQuery(org.apache.ojb.broker.query.Query)
,
PersistenceBrokerTransactionManager
,
JtaTransactionManager
Field Summary |
---|
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor |
---|
logger |
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()
|
void |
delete(Object entity)
|
void |
deleteByQuery(org.apache.ojb.broker.query.Query query)
|
Object |
execute(PersistenceBrokerCallback action)
Execute the action specified by the given action object within a PersistenceBroker. |
Collection |
executeFind(PersistenceBrokerCallback action)
Execute the specified action assuming that the result object is a Collection. |
Collection |
getCollectionByQuery(org.apache.ojb.broker.query.Query query)
|
int |
getCount(org.apache.ojb.broker.query.Query query)
|
Iterator |
getIteratorByQuery(org.apache.ojb.broker.query.Query query)
|
Object |
getObjectById(Class entityClass,
Object idValue)
Return the persistent instance of the given entity class with the given id value, throwing an exception if not found. |
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. |
Iterator |
getReportQueryIteratorByQuery(org.apache.ojb.broker.query.Query query)
|
boolean |
isAllowCreate()
Return if a new PersistenceBroker should be created if no thread-bound found. |
protected void |
releasePersistenceBroker(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 |
removeFromCache(Object entityOrId)
|
void |
setAllowCreate(boolean allowCreate)
Set if a new PersistenceBroker should be created when no transactional PersistenceBroker can be found for the current thread. |
void |
store(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, isLazyInit, setDatabaseProductName, setDataSource, setExceptionTranslator, setLazyInit |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public PersistenceBrokerTemplate()
public PersistenceBrokerTemplate(boolean allowCreate)
allowCreate
- if a non-transactional PersistenceBroker should be created
when no transactional PersistenceBroker can be found for the current threadpublic PersistenceBrokerTemplate(org.apache.ojb.broker.PBKey pbKey)
pbKey
- the PBKey of the PersistenceBroker configuration to usepublic PersistenceBrokerTemplate(org.apache.ojb.broker.PBKey pbKey, boolean allowCreate)
pbKey
- the PBKey of the PersistenceBroker configuration to useallowCreate
- if a new PersistenceBroker should be created
if no thread-bound foundMethod Detail |
---|
public void setAllowCreate(boolean allowCreate)
JdoTemplate is aware of a corresponding PersistenceBroker bound to the current thread, for example when using PersistenceBrokerTransactionManager. 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 boolean isAllowCreate()
public Object execute(PersistenceBrokerCallback action) throws DataAccessException
PersistenceBrokerOperations
Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager like PersistenceBrokerTransactionManager.
execute
in interface PersistenceBrokerOperations
action
- action object that specifies the OJB action
null
DataAccessException
- in case of OJB errorsPersistenceBrokerTransactionManager
,
org.springframework.dao
,
org.springframework.transaction
public Collection executeFind(PersistenceBrokerCallback action) throws DataAccessException
PersistenceBrokerOperations
executeFind
in interface PersistenceBrokerOperations
action
- action object that specifies the OJB action
null
DataAccessException
- in case of OJB errorspublic Object getObjectById(Class entityClass, Object idValue) throws DataAccessException
PersistenceBrokerOperations
The given id value is typically just unique within the namespace of the persistent class, corresponding to a single primary key in a database table.
getObjectById
in interface PersistenceBrokerOperations
entityClass
- a persistent classidValue
- an id value of the persistent instance
ObjectRetrievalFailureException
- if not found
DataAccessException
- in case of OJB errorsIdentityFactory.buildIdentity(Class, Object)
,
PersistenceBroker.getObjectByIdentity(org.apache.ojb.broker.Identity)
public Object getObjectByQuery(org.apache.ojb.broker.query.Query query) throws DataAccessException
getObjectByQuery
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.getObjectByQuery(org.apache.ojb.broker.query.Query)
public Collection getCollectionByQuery(org.apache.ojb.broker.query.Query query) throws DataAccessException
getCollectionByQuery
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.getCollectionByQuery(org.apache.ojb.broker.query.Query)
public Iterator getIteratorByQuery(org.apache.ojb.broker.query.Query query) throws DataAccessException
getIteratorByQuery
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.getIteratorByQuery(org.apache.ojb.broker.query.Query)
public Iterator getReportQueryIteratorByQuery(org.apache.ojb.broker.query.Query query)
getReportQueryIteratorByQuery
in interface PersistenceBrokerOperations
PersistenceBroker.getReportQueryIteratorByQuery(org.apache.ojb.broker.query.Query)
public int getCount(org.apache.ojb.broker.query.Query query) throws DataAccessException
getCount
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.getCount(org.apache.ojb.broker.query.Query)
public void removeFromCache(Object entityOrId) throws DataAccessException
removeFromCache
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.removeFromCache(java.lang.Object)
public void clearCache() throws DataAccessException
clearCache
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.clearCache()
public void store(Object entity) throws DataAccessException
store
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.store(java.lang.Object, org.apache.ojb.broker.util.ObjectModification)
public void delete(Object entity) throws DataAccessException
delete
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.delete(java.lang.Object)
public void deleteByQuery(org.apache.ojb.broker.query.Query query) throws DataAccessException
deleteByQuery
in interface PersistenceBrokerOperations
DataAccessException
- in case of OJB errorsPersistenceBroker.deleteByQuery(org.apache.ojb.broker.query.Query)
protected org.apache.ojb.broker.PersistenceBroker getPersistenceBroker() throws DataAccessResourceFailureException, IllegalStateException
Default implementation delegates to OjbFactoryUtils. Can be overridden in subclasses, e.g. for testing purposes.
DataAccessResourceFailureException
- if the PersistenceBroker couldn't be created
IllegalStateException
- if no thread-bound PersistenceBroker found and allowCreate falseOjbAccessor.setJcdAlias(java.lang.String)
,
OjbAccessor.setPbKey(org.apache.ojb.broker.PBKey)
,
setAllowCreate(boolean)
,
OjbFactoryUtils.getPersistenceBroker(PBKey, boolean)
protected void releasePersistenceBroker(org.apache.ojb.broker.PersistenceBroker pb)
Default implementation delegates to OjbFactoryUtils. Can be overridden in subclasses, e.g. for testing purposes.
pb
- PersistenceBroker to closeOjbAccessor.setJcdAlias(java.lang.String)
,
OjbAccessor.setPbKey(org.apache.ojb.broker.PBKey)
,
OjbFactoryUtils.releasePersistenceBroker(org.apache.ojb.broker.PersistenceBroker, org.apache.ojb.broker.PBKey)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |