org.apache.ojb.broker.core
Class PersistenceBrokerImpl

java.lang.Object
  extended byorg.apache.ojb.broker.core.PersistenceBrokerAbstractImpl
      extended byorg.apache.ojb.broker.core.PersistenceBrokerImpl
All Implemented Interfaces:
org.apache.ojb.broker.util.configuration.Configurable, ObjectContainer, PBState, PersistenceBroker

public class PersistenceBrokerImpl
extends PersistenceBrokerAbstractImpl
implements PBState

The PersistenceBrokerImpl is an implementation of the PersistenceBroker Interface that specifies a persistence mechanism for Java objects. This Concrete implementation provides an object relational mapping and allows to store and retrieve arbitrary objects in/from relational databases accessed by JDBC.

Version:
$Id: PersistenceBrokerImpl.java,v 1.83.2.14 2005/03/18 19:25:05 arminw Exp $
Author:
Thomas Mahler, Leandro Rodrigo Saad Cruz, Matthew Baird, Jakob Braeuchi

Constructor Summary
PersistenceBrokerImpl(PBKey key, PersistenceBrokerFactoryIF pbf)
          Constructor used by PersistenceBrokerFactoryIF implementation.
 
Method Summary
 void abortTransaction()
          Abort and close the transaction.
 void addMtoNImplementor(MtoNImplementor m2n)
           
 void beginTransaction()
          begin a transaction against the underlying RDBMS.
 void clearCache()
          clears the brokers internal cache.
 boolean close()
          Close this PersistenceBroker so that no further requests may be made on it.
 void commitTransaction()
          Commit and close the transaction.
 void delete(java.lang.Object obj)
          deletes the objects obj concrete representation in the underlying persistence system.
 void deleteByQuery(Query query)
          Deletes all objects matching the query from the underlying persistence system.
 void deleteMtoNImplementor(MtoNImplementor m2nImpl)
          Deletes and MtoN implementor (a row on a indirection table) As it is today, ojb doesn't handle collection inherence, so collections descriptors are written per class.
 void destroy()
          Release all resources used by this class - CAUTION: No further operations can be done with this instance after calling this method.
 java.lang.Object doGetObjectByIdentity(Identity id)
          Internal used method to retrieve object based on Identity.
 ClassDescriptor getClassDescriptor(java.lang.Class clazz)
          returns a ClassDescriptor for the persistence capable class clazz.
 ManageableCollection getCollectionByQuery(java.lang.Class collectionClass, Query query)
          retrieve a collection of type collectionClass matching the Query query
 java.util.Collection getCollectionByQuery(Query query)
          retrieve a collection of itemClass Objects matching the Query query
 int getCount(Query query)
          Returns the count of elements a given query will return.
 DescriptorRepository getDescriptorRepository()
          Returns the DescriptorRepository associated with this broker.
 MaterializationCache getInternalCache()
           
 java.util.Iterator getIteratorByQuery(Query query)
          returns an Iterator that iterates Objects of class c if calling the .next() method.
 java.lang.Object getObjectByIdentity(Identity id)
          Retrieve an Object by its Identity.
 java.lang.Object getObjectByQuery(Query query)
          retrieve an Object by query I.e perform a SELECT ...
 PBKey getPBKey()
          Get the PBKey for this broker.
 java.util.Enumeration getPKEnumerationByQuery(java.lang.Class primaryKeyClass, Query query)
          returns an Enumeration of PrimaryKey Objects for objects of class DataClass.
 QueryReferenceBroker getReferenceBroker()
           
 RelationshipPrefetcherFactory getRelationshipPrefetcherFactory()
           
 java.util.Iterator getReportQueryIteratorByQuery(Query query)
          Get an Iterator based on the ReportQuery
 java.lang.Class getTopLevelClass(java.lang.Class clazz)
          Returns the top level (extent) class to which the given class belongs.
 boolean hasClassDescriptor(java.lang.Class clazz)
          Same as getClassDescriptor(java.lang.Class), but does not throw an exception when class was not found.
 boolean isClosed()
          Returns true if this instance is closed.
 boolean isInTransaction()
          returns true if the broker is currently running a transaction.
 void link(java.lang.Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor rds, java.lang.Object referencedObject, boolean insert)
          Assign FK value to target object by reading PK values of referenced object.
 void linkMtoN(java.lang.Object obj, CollectionDescriptor cod, boolean insert)
          Assign FK values and store entries in indirection table for all objects referenced by given object.
 void linkOneToMany(java.lang.Object obj, CollectionDescriptor cod, boolean insert)
          Assign FK value to all n-side objects referenced by given object.
 void linkOneToOne(java.lang.Object obj, ClassDescriptor cld, ObjectReferenceDescriptor rds, boolean insert)
          Assign FK value of main object with PK values of the reference object.
 Query query()
          factory method to create a new Query object.
 void refresh()
          Lookup the current DescriptorRepository for this class.
 void refreshRelationships(java.lang.Object obj, ClassDescriptor cld)
          Refresh Relationships
 void removeFromCache(java.lang.Object objectOrIdentity)
          Removes the object from the brokers internal cache.
 void retrieveAllReferences(java.lang.Object pInstance)
          retrieve all References (also Collection-attributes) of a given instance.
 void retrieveReference(java.lang.Object pInstance, java.lang.String pAttributeName)
          retrieve a single reference- or collection attribute of a persistent instance.
 BrokerHelper serviceBrokerHelper()
          Returns the BrokerHelper instance associated with this broker.
 ConnectionManagerIF serviceConnectionManager()
          Returns the ConnectionManagerIF instance associated with this broker.
 IdentityFactory serviceIdentity()
          Return the IdentityFactory instance associated with this broker.
 JdbcAccess serviceJdbcAccess()
          Returns the JdbcAccess instance associated with this broker.
 ObjectCache serviceObjectCache()
          Returns the ObjectCache instance associated with this broker.
 SequenceManager serviceSequenceManager()
          Returns the SequenceManager instance associated with this broker.
 org.apache.ojb.broker.accesslayer.sql.SqlGenerator serviceSqlGenerator()
          Returns the SqlGenerator instance associated with this broker.
 StatementManagerIF serviceStatementManager()
          Returns the StatementManagerIF instance associated with this broker.
 void setClosed(boolean closed)
           
 void setInTransaction(boolean inTransaction)
           
 void setPBKey(PBKey key)
           
 void store(java.lang.Object obj)
          Store an Object.
 void store(java.lang.Object obj, ObjectModification mod)
          makes object obj persistent in the underlying persistence system.
 void unlinkFK(java.lang.Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor rds)
          Unkink FK fields of target object.
 void unlinkXtoN(java.lang.Object obj, CollectionDescriptor col)
           
 
Methods inherited from class org.apache.ojb.broker.core.PersistenceBrokerAbstractImpl
addListener, addListener, configure, fireBrokerEvent, fireBrokerEvent, fireBrokerEvent, removeAllListeners, removeAllListeners, removeListener
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistenceBrokerImpl

public PersistenceBrokerImpl(PBKey key,
                             PersistenceBrokerFactoryIF pbf)
Constructor used by PersistenceBrokerFactoryIF implementation.

Method Detail

getInternalCache

public MaterializationCache getInternalCache()

serviceIdentity

public IdentityFactory serviceIdentity()
Description copied from interface: PersistenceBroker
Return the IdentityFactory instance associated with this broker.

Specified by:
serviceIdentity in interface PersistenceBroker
Returns:
service to create Identity objects.

serviceSqlGenerator

public org.apache.ojb.broker.accesslayer.sql.SqlGenerator serviceSqlGenerator()
Description copied from interface: PersistenceBroker
Returns the SqlGenerator instance associated with this broker.

Specified by:
serviceSqlGenerator in interface PersistenceBroker

serviceStatementManager

public StatementManagerIF serviceStatementManager()
Description copied from interface: PersistenceBroker
Returns the StatementManagerIF instance associated with this broker.

Specified by:
serviceStatementManager in interface PersistenceBroker

serviceJdbcAccess

public JdbcAccess serviceJdbcAccess()
Description copied from interface: PersistenceBroker
Returns the JdbcAccess instance associated with this broker.

Specified by:
serviceJdbcAccess in interface PersistenceBroker

serviceConnectionManager

public ConnectionManagerIF serviceConnectionManager()
Description copied from interface: PersistenceBroker
Returns the ConnectionManagerIF instance associated with this broker.

Specified by:
serviceConnectionManager in interface PersistenceBroker

serviceSequenceManager

public SequenceManager serviceSequenceManager()
Description copied from interface: PersistenceBroker
Returns the SequenceManager instance associated with this broker.

Specified by:
serviceSequenceManager in interface PersistenceBroker

serviceBrokerHelper

public BrokerHelper serviceBrokerHelper()
Description copied from interface: PersistenceBroker
Returns the BrokerHelper instance associated with this broker. Makes some helper methods available.

Specified by:
serviceBrokerHelper in interface PersistenceBroker

serviceObjectCache

public ObjectCache serviceObjectCache()
Description copied from interface: PersistenceBroker
Returns the ObjectCache instance associated with this broker.

Specified by:
serviceObjectCache in interface PersistenceBroker

getReferenceBroker

public QueryReferenceBroker getReferenceBroker()

getRelationshipPrefetcherFactory

public RelationshipPrefetcherFactory getRelationshipPrefetcherFactory()

isClosed

public boolean isClosed()
Description copied from interface: PersistenceBroker
Returns true if this instance is closed.

Specified by:
isClosed in interface PersistenceBroker

setClosed

public void setClosed(boolean closed)
Specified by:
setClosed in interface PBState

refresh

public void refresh()
Lookup the current DescriptorRepository for this class. This method is responsible to keep this PB instance in sync with MetadataManager.


destroy

public void destroy()
Release all resources used by this class - CAUTION: No further operations can be done with this instance after calling this method.


getPBKey

public PBKey getPBKey()
Description copied from interface: PersistenceBroker
Get the PBKey for this broker.

Specified by:
getPBKey in interface PersistenceBroker

setPBKey

public void setPBKey(PBKey key)

close

public boolean close()
Description copied from interface: PersistenceBroker
Close this PersistenceBroker so that no further requests may be made on it. A PersistenceBroker instance can be used only until it is closed. Closing a PersistenceBroker might release it to the pool of available PersistenceBrokers, or might be garbage collected, at the option of the implementation.

Specified by:
close in interface PersistenceBroker
Returns:
true if successful
See Also:
PersistenceBroker.close()

abortTransaction

public void abortTransaction()
                      throws TransactionNotInProgressException
Abort and close the transaction. Calling abort abandons all persistent object modifications and releases the associated locks. If transaction is not in progress a TransactionNotInProgressException is thrown

Specified by:
abortTransaction in interface PersistenceBroker
Throws:
TransactionNotInProgressException

beginTransaction

public void beginTransaction()
                      throws TransactionInProgressException,
                             TransactionAbortedException
begin a transaction against the underlying RDBMS. Calling beginTransaction multiple times, without an intervening call to commitTransaction or abortTransaction, causes the exception TransactionInProgressException to be thrown on the second and subsequent calls.

Specified by:
beginTransaction in interface PersistenceBroker
Throws:
TransactionInProgressException
TransactionAbortedException

commitTransaction

public void commitTransaction()
                       throws TransactionNotInProgressException,
                              TransactionAbortedException
Commit and close the transaction. Calling commit commits to the database all UPDATE, INSERT and DELETE statements called within the transaction and releases any locks held by the transaction. If beginTransaction() has not been called before a TransactionNotInProgressException exception is thrown. If the transaction cannot be commited a TransactionAbortedException exception is thrown.

Specified by:
commitTransaction in interface PersistenceBroker
Throws:
TransactionNotInProgressException
TransactionAbortedException

delete

public void delete(java.lang.Object obj)
            throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
deletes the objects obj concrete representation in the underlying persistence system. E.G. by DELETE FROM ... WHERE ... in an RDBMS

Specified by:
delete in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.delete(java.lang.Object)

deleteByQuery

public void deleteByQuery(Query query)
                   throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Deletes all objects matching the query from the underlying persistence system. E.G. by DELETE FROM ... WHERE ... in an RDBMS

Note: This method directly perform the delete statement ignoring object references and do not synchronize the cache - take care!

Specified by:
deleteByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.deleteByQuery(Query)

store

public void store(java.lang.Object obj)
           throws PersistenceBrokerException
Store an Object.

Specified by:
store in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.store(Object)

link

public void link(java.lang.Object targetObject,
                 ClassDescriptor cld,
                 ObjectReferenceDescriptor rds,
                 java.lang.Object referencedObject,
                 boolean insert)
Assign FK value to target object by reading PK values of referenced object.

Parameters:
targetObject - real (non-proxy) target object
cld - ClassDescriptor of the real target object
rds - An ObjectReferenceDescriptor or CollectionDescriptor associated with the real object.
referencedObject - referenced object or proxy
insert - Show if "linking" is done while insert or update.

unlinkFK

public void unlinkFK(java.lang.Object targetObject,
                     ClassDescriptor cld,
                     ObjectReferenceDescriptor rds)
Unkink FK fields of target object.

Parameters:
targetObject - real (non-proxy) target object
cld - ClassDescriptor of the real target object
rds - An ObjectReferenceDescriptor or CollectionDescriptor associated with the real object.

linkOneToOne

public void linkOneToOne(java.lang.Object obj,
                         ClassDescriptor cld,
                         ObjectReferenceDescriptor rds,
                         boolean insert)
Assign FK value of main object with PK values of the reference object.

Parameters:
obj - real object with reference (proxy) object (or real object with set FK values on insert)
cld - ClassDescriptor of the real object
rds - An ObjectReferenceDescriptor of real object.
insert - Show if "linking" is done while insert or update.

linkOneToMany

public void linkOneToMany(java.lang.Object obj,
                          CollectionDescriptor cod,
                          boolean insert)
Assign FK value to all n-side objects referenced by given object.

Parameters:
obj - real object with 1:n reference
cod - CollectionDescriptor of referenced 1:n objects
insert - flag signal insert operation, false signals update operation

linkMtoN

public void linkMtoN(java.lang.Object obj,
                     CollectionDescriptor cod,
                     boolean insert)
Assign FK values and store entries in indirection table for all objects referenced by given object.

Parameters:
obj - real object with 1:n reference
cod - CollectionDescriptor of referenced 1:n objects
insert - flag signal insert operation, false signals update operation

unlinkXtoN

public void unlinkXtoN(java.lang.Object obj,
                       CollectionDescriptor col)

retrieveAllReferences

public void retrieveAllReferences(java.lang.Object pInstance)
                           throws PersistenceBrokerException
retrieve all References (also Collection-attributes) of a given instance. Loading is forced, even if the collection- and reference-descriptors differ.

Specified by:
retrieveAllReferences in interface PersistenceBroker
Parameters:
pInstance - the persistent instance to work with
Throws:
PersistenceBrokerException

retrieveReference

public void retrieveReference(java.lang.Object pInstance,
                              java.lang.String pAttributeName)
                       throws PersistenceBrokerException
retrieve a single reference- or collection attribute of a persistent instance.

Specified by:
retrieveReference in interface PersistenceBroker
Parameters:
pInstance - the persistent instance
pAttributeName - the name of the Attribute to load
Throws:
PersistenceBrokerException

refreshRelationships

public void refreshRelationships(java.lang.Object obj,
                                 ClassDescriptor cld)
Refresh Relationships

Parameters:
obj -
cld -
Throws:
PersistenceBrokerException - if there is a error refreshing collections or references

getCollectionByQuery

public ManageableCollection getCollectionByQuery(java.lang.Class collectionClass,
                                                 Query query)
                                          throws PersistenceBrokerException
retrieve a collection of type collectionClass matching the Query query

Specified by:
getCollectionByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.getCollectionByQuery(Class, Query)

getCollectionByQuery

public java.util.Collection getCollectionByQuery(Query query)
                                          throws PersistenceBrokerException
retrieve a collection of itemClass Objects matching the Query query

Specified by:
getCollectionByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException

getIteratorByQuery

public java.util.Iterator getIteratorByQuery(Query query)
                                      throws PersistenceBrokerException
returns an Iterator that iterates Objects of class c if calling the .next() method. The Elements returned come from a SELECT ... WHERE Statement that is defined by the Query query. If itemProxy is null, no proxies are used.

Specified by:
getIteratorByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException

getObjectByIdentity

public java.lang.Object getObjectByIdentity(Identity id)
                                     throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Retrieve an Object by its Identity. Application Developers are encouraged to use getObjectByQuery(). This method is mainly used for internal performant handling of materialization by OID (e.g. in Proxies)

Specified by:
getObjectByIdentity in interface PersistenceBroker
Throws:
PersistenceBrokerException

doGetObjectByIdentity

public java.lang.Object doGetObjectByIdentity(Identity id)
                                       throws PersistenceBrokerException
Internal used method to retrieve object based on Identity.

Parameters:
id -
Returns:
Throws:
PersistenceBrokerException

getObjectByQuery

public java.lang.Object getObjectByQuery(Query query)
                                  throws PersistenceBrokerException
retrieve an Object by query I.e perform a SELECT ... FROM ... WHERE ... in an RDBMS

Specified by:
getObjectByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException

getPKEnumerationByQuery

public java.util.Enumeration getPKEnumerationByQuery(java.lang.Class primaryKeyClass,
                                                     Query query)
                                              throws PersistenceBrokerException
returns an Enumeration of PrimaryKey Objects for objects of class DataClass. The Elements returned come from a SELECT ... WHERE Statement that is defined by the fields and their coresponding values of listFields and listValues. Useful for EJB Finder Methods...

Specified by:
getPKEnumerationByQuery in interface PersistenceBroker
Parameters:
primaryKeyClass - the pk class for the searched objects
query - the query
Throws:
PersistenceBrokerException

store

public void store(java.lang.Object obj,
                  ObjectModification mod)
           throws PersistenceBrokerException
makes object obj persistent in the underlying persistence system. E.G. by INSERT INTO ... or UPDATE ... in an RDBMS. The ObjectModification parameter can be used to determine whether INSERT or update is to be used. This functionality is typically called from transaction managers, that track which objects have to be stored. If the object is an unmaterialized proxy the method return immediately.

Specified by:
store in interface PersistenceBroker
Throws:
PersistenceBrokerException

isInTransaction

public boolean isInTransaction()
returns true if the broker is currently running a transaction.

Specified by:
isInTransaction in interface PersistenceBroker
Returns:
boolean

setInTransaction

public void setInTransaction(boolean inTransaction)

removeFromCache

public void removeFromCache(java.lang.Object objectOrIdentity)
                     throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Removes the object from the brokers internal cache. If object is instance of Identity, the associated object was removed from cache.
Note: Removing is not recursive.

Specified by:
removeFromCache in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.removeFromCache(java.lang.Object)

getClassDescriptor

public ClassDescriptor getClassDescriptor(java.lang.Class clazz)
                                   throws PersistenceBrokerException
returns a ClassDescriptor for the persistence capable class clazz. throws a PersistenceBrokerException if clazz is not persistence capable, i.e. if clazz is not defined in the DescriptorRepository.

Specified by:
getClassDescriptor in interface PersistenceBroker
Throws:
PersistenceBrokerException

hasClassDescriptor

public boolean hasClassDescriptor(java.lang.Class clazz)
Description copied from interface: PersistenceBroker
Same as PersistenceBroker.getClassDescriptor(java.lang.Class), but does not throw an exception when class was not found. Useful for checking if an object is persistence Capable.

Specified by:
hasClassDescriptor in interface PersistenceBroker
Parameters:
clazz - target class
Returns:
true if descriptor was found

clearCache

public void clearCache()
                throws PersistenceBrokerException
clears the brokers internal cache. removing is recursive. That is referenced Objects are also removed from the cache, if the auto-retrieve flag is set for obj.getClass() in the metadata repository.

Specified by:
clearCache in interface PersistenceBroker
Throws:
PersistenceBrokerException

getTopLevelClass

public java.lang.Class getTopLevelClass(java.lang.Class clazz)
                                 throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Returns the top level (extent) class to which the given class belongs. This may be a (abstract) base-class, an interface or the given class itself, if no extent is defined.

Specified by:
getTopLevelClass in interface PersistenceBroker
Throws:
PersistenceBrokerException - if clazz is not persistence capable, i.e. if clazz is not defined in the DescriptorRepository.
See Also:
PersistenceBroker.getTopLevelClass(java.lang.Class)

getCount

public int getCount(Query query)
             throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Returns the count of elements a given query will return.

Specified by:
getCount in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.getCount(Query)

getReportQueryIteratorByQuery

public java.util.Iterator getReportQueryIteratorByQuery(Query query)
                                                 throws PersistenceBrokerException
Get an Iterator based on the ReportQuery

Specified by:
getReportQueryIteratorByQuery in interface PersistenceBroker
Parameters:
query -
Returns:
Iterator
Throws:
PersistenceBrokerException

query

public Query query()
Description copied from interface: ObjectContainer
factory method to create a new Query object.

Specified by:
query in interface ObjectContainer
Returns:
a new Query object
See Also:
ObjectContainer.query()

getDescriptorRepository

public DescriptorRepository getDescriptorRepository()
Description copied from interface: PersistenceBroker
Returns the DescriptorRepository associated with this broker.

Specified by:
getDescriptorRepository in interface PersistenceBroker
Returns:
DescriptorRepository

deleteMtoNImplementor

public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
                           throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Deletes and MtoN implementor (a row on a indirection table) As it is today, ojb doesn't handle collection inherence, so collections descriptors are written per class. We try to match one of these collection descriptors, iterating from the left side and looking for possible for classes on the right side using : collection descriptor element . isAssinableFrom(rightClass) TODO: handle cache problems TODO: delete more than one row if possible Pre-coditions :
  • Pos-coditions :
  • Assertions :
  • Specified by:
    deleteMtoNImplementor in interface PersistenceBroker
    Parameters:
    m2nImpl -
    Throws:
    PersistenceBrokerException - if an error occours
    See Also:
    PersistenceBroker.deleteMtoNImplementor(org.apache.ojb.broker.MtoNImplementor)

    addMtoNImplementor

    public void addMtoNImplementor(MtoNImplementor m2n)
                            throws PersistenceBrokerException
    Specified by:
    addMtoNImplementor in interface PersistenceBroker
    Throws:
    PersistenceBrokerException
    See Also:
    PersistenceBroker.addMtoNImplementor(org.apache.ojb.broker.MtoNImplementor)


    (C) 2002 - 2004 Apache Software Foundation
    All rights reserved. Published under the Apache License 2.0.
    http://db.apache.org/ojb
    Version: 1.0.3, 2005-04-2