org.apache.cayenne.access
Class DataContext

java.lang.Object
  extended by org.apache.cayenne.access.DataContext
All Implemented Interfaces:
java.io.Serializable, QueryEngine, DataChannel, ObjectContext

public class DataContext
extends java.lang.Object
implements ObjectContext, DataChannel, QueryEngine, java.io.Serializable

Class that provides applications with access to Cayenne persistence features. In most cases this is the only access class directly used in the application.

Most common DataContext use pattern is to create one DataContext per session. "Session" may be a an HttpSession in a web application, or any other similar concept in a multiuser application.

DataObjects are registered with DataContext either implicitly when they are fetched via a query, or read via a relationship from another object, or explicitly via calling createAndRegisterNewObject(Class)during new DataObject creation. DataContext tracks changes made to its DataObjects in memory, and flushes them to the database when commitChanges()is called. Until DataContext is committed, changes made to its objects are not visible in other DataContexts.

Each DataObject can belong only to a single DataContext. To create a replica of an object from a different DataContext in a local context, use localObject(ObjectId, Persistent) method.

For more information see Cayenne User Guide.

Note that all QueryEngine interface methods are deprecated in the DataContext. Since 1.2 release DataContext implements ObjectContext and DataChannel interfaces.

Author:
Andrus Adamchik
See Also:
Serialized Form

Field Summary
protected  DataChannel channel
           
static EventSubject DID_COMMIT
           
static EventSubject DID_ROLLBACK
           
protected  EntityResolver entityResolver
           
protected  java.lang.String lazyInitParentDomainName
          Stores the name of parent DataDomain.
protected  org.apache.cayenne.access.DataContextMergeHandler mergeHandler
           
protected  ObjectStore objectStore
           
protected static java.lang.ThreadLocal threadDataContext
          A holder of a DataContext bound to the current thread.
protected  java.util.Map userProperties
          Stores user defined properties associated with this DataContext.
protected  boolean usingSharedSnaphsotCache
           
protected  boolean validatingObjectsOnCommit
           
static EventSubject WILL_COMMIT
           
 
Fields inherited from interface org.apache.cayenne.DataChannel
FLUSH_CASCADE_SYNC, FLUSH_NOCASCADE_SYNC, GRAPH_CHANGED_SUBJECT, GRAPH_FLUSHED_SUBJECT, GRAPH_ROLLEDBACK_SUBJECT, ROLLBACK_CASCADE_SYNC
 
Constructor Summary
DataContext()
          Creates a new DataContext that is not attached to the Cayenne stack.
DataContext(DataChannel channel, ObjectStore objectStore)
          Creates a new DataContext with parent DataChannel and ObjectStore.
DataContext(QueryEngine parent, ObjectStore objectStore)
          Deprecated. since 1.2 - use DataContext(DataChannel, ObjectStore) constructor instead. Note that DataDomain is both a DataChannel and a QueryEngine, so you may need to do a cast: new DataContext((DataChannel) domain, objectStore).
 
Method Summary
static void bindThreadDataContext(DataContext context)
          Binds a DataContext to the current thread.
 void commitChanges()
          Synchronizes object graph with the database.
 void commitChanges(org.apache.log4j.Level logLevel)
          Deprecated. Since 1.2, use commitChanges() instead.
 void commitChangesToParent()
          "Flushes" the changes to the parent DataChannel.
 DataObject createAndRegisterNewObject(java.lang.Class objectClass)
          Instantiates new object and registers it with itself.
 DataObject createAndRegisterNewObject(java.lang.String objEntityName)
          Instantiates new object and registers it with itself.
 DataContext createChildDataContext()
          Creates and returns a new child DataContext.
static DataContext createDataContext()
          Factory method that creates and returns a new instance of DataContext based on default domain.
static DataContext createDataContext(boolean useSharedCache)
          Factory method that creates and returns a new instance of DataContext based on default domain.
static DataContext createDataContext(java.lang.String domainName)
          Factory method that creates and returns a new instance of DataContext using named domain as its parent.
static DataContext createDataContext(java.lang.String domainName, boolean useSharedCache)
          Creates and returns new DataContext that will use a named DataDomain as its parent.
 DataRow currentSnapshot(DataObject object)
          Returns a DataRow reflecting current, possibly uncommitted, object state.
 java.util.Collection deletedObjects()
          Returns a list of objects that are registered with this DataContext and have a state PersistenceState.DELETED
 void deleteObject(Persistent object)
          Schedules an object for deletion on the next commit of this DataContext.
 void deleteObjects(java.util.Collection objects)
          Schedules all objects in the collection for deletion on the next commit of this DataContext.
 DataChannel getChannel()
          Returns parent DataChannel, that is normally a DataDomain or another DataContext.
 java.util.Collection getDataMaps()
          Deprecated. since 1.2. Use 'getEntityResolver().getDataMaps()' instead.
 DataContextDelegate getDelegate()
          Returns a delegate currently associated with this DataContext.
 EntityResolver getEntityResolver()
          Returns EntityResolver.
 EventManager getEventManager()
          Returns EventManager associated with the ObjectStore.
 GraphManager getGraphManager()
          Returns this context's ObjectStore.
 ObjectStore getObjectStore()
          Returns ObjectStore associated with this DataContext.
 QueryEngine getParent()
          Deprecated. since 1.2. Use 'getParentDataDomain()' or 'getChannel()' instead.
 DataDomain getParentDataDomain()
          Returns a DataDomain used by this DataContext.
static DataContext getThreadDataContext()
          Returns the DataContext bound to the current thread.
protected  java.util.Map getUserProperties()
          Returns a map of user-defined properties associated with this DataContext.
 java.lang.Object getUserProperty(java.lang.String key)
          Returns a user-defined property previously set via 'setUserProperty'.
 boolean hasChanges()
          Returns true if there are any modified, deleted or new objects registered with this DataContext, false otherwise.
 void invalidateObjects(java.util.Collection dataObjects)
          "Invalidates" a Collection of DataObject.
 boolean isTransactionEventsEnabled()
           
 boolean isUsingSharedSnapshotCache()
          Returns true if the ObjectStore uses shared cache of a parent DataDomain.
 boolean isValidatingObjectsOnCommit()
          Returns whether this DataContext performs object validation before commit is executed.
 Persistent localObject(ObjectId id, Persistent prototype)
          Returns an object local to this DataContext and matching the ObjectId.
 java.util.List localObjects(java.util.List objects)
          Deprecated. since 1.2 - use localObject(ObjectId, Persistent) to specify how each local object should be handled.
 DataNode lookupDataNode(DataMap dataMap)
          Deprecated. since 1.2 DataContext's QueryEngine implementation is replaced by DataChannel. Use "getParentDataDomain().lookupDataNode(..)".
 java.util.Collection modifiedObjects()
          Returns a list of objects that are registered with this DataContext and have a state PersistenceState.MODIFIED
 Persistent newObject(java.lang.Class persistentClass)
          Creates and registers new persistent object.
 java.util.Collection newObjects()
          Returns a list of objects that are registered with this DataContext and have a state PersistenceState.NEW
 DataObject objectFromDataRow(java.lang.Class objectClass, DataRow dataRow, boolean refresh)
          Creates a DataObject from DataRow.
 java.util.List objectsFromDataRows(java.lang.Class objectClass, java.util.List dataRows, boolean refresh, boolean resolveInheritanceHierarchy)
          Converts a list of DataRows to a List of DataObject registered with this DataContext.
 java.util.List objectsFromDataRows(ObjEntity entity, java.util.List dataRows, boolean refresh, boolean resolveInheritanceHierarchy)
          Converts a list of data rows to a list of DataObjects.
 QueryResponse onQuery(ObjectContext context, Query query)
          An implementation of a DataChannel method that is used by child contexts to execute queries.
 GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType)
          An implementation of a DataChannel method that is used by child contexts to synchronize state with this context.
 QueryResponse performGenericQuery(Query query)
          Executes a query returning a generic response.
 ResultIterator performIteratedQuery(Query query)
          Performs a single database select query returning result as a ResultIterator.
 int[] performNonSelectingQuery(Query query)
          Performs a single database query that does not select rows.
 int[] performNonSelectingQuery(java.lang.String queryName)
          Performs a named mapped query that does not select rows.
 int[] performNonSelectingQuery(java.lang.String queryName, java.util.Map parameters)
          Performs a named mapped non-selecting query using a map of parameters.
 void performQueries(java.util.Collection queries, OperationObserver callback)
          Deprecated. since 1.2 DataContext's QueryEngine implementation is replaced by DataChannel.
 void performQueries(java.util.Collection queries, OperationObserver callback, Transaction transaction)
          Deprecated. since 1.2. Use Transaction.bindThreadTransaction(..) to provide custom transactions, besides DataContext's QueryEngine implementation is replaced by DataChannel.
 java.util.List performQuery(Query query)
          Performs a single selecting query.
 java.util.List performQuery(java.lang.String queryName, boolean expireCachedLists)
          Returns a list of objects or DataRows for a named query stored in one of the DataMaps.
 java.util.List performQuery(java.lang.String queryName, java.util.Map parameters, boolean expireCachedLists)
          Returns a list of objects or DataRows for a named query stored in one of the DataMaps.
 void prefetchRelationships(SelectQuery query, java.util.List objects)
          Deprecated. Since 1.2. This is a utility method that handles a very specific case. It shouldn't be in DataContext.
 void prepareForAccess(Persistent object, java.lang.String property)
          Resolves object fault if needed.
 void propertyChanged(Persistent object, java.lang.String property, java.lang.Object oldValue, java.lang.Object newValue)
          Retains DataObject snapshot and changes its state if needed.
 DataObject refetchObject(ObjectId oid)
          Refetches object data for ObjectId.
 DataObject registeredObject(ObjectId id)
          Deprecated. since 1.2 use 'localObject(id, null)'
 void registerNewObject(DataObject object)
          Registers a transient object with the context, recursively registering all transient DataObjects attached to this object via relationships.
 void rollbackChanges()
          Reverts any changes that have occurred to objects registered with DataContext; also performs cascading rollback of all parent DataContexts.
 void rollbackChangesLocally()
          If the parent channel is a DataContext, reverts local changes to make this context look like the parent, if the parent channel is a DataDomain, reverts all changes.
 void setChannel(DataChannel channel)
           
 void setDelegate(DataContextDelegate delegate)
          Sets a DataContextDelegate for this context.
 void setParent(QueryEngine parent)
          Deprecated. since 1.2, use setChannel instead.
 void setTransactionEventsEnabled(boolean flag)
          Enables or disables posting of transaction events by this DataContext.
static void setTransactionEventsEnabledDefault(boolean flag)
          Sets default for posting transaction events by new DataContexts.
 void setUserProperty(java.lang.String key, java.lang.Object value)
          Sets a user-defined property.
 void setValidatingObjectsOnCommit(boolean flag)
          Sets the property defining whether this DataContext should perform object validation before commit is executed.
 java.util.Collection uncommittedObjects()
          Returns a collection of all uncommitted registered objects.
 void unregisterObjects(java.util.Collection dataObjects)
          Unregisters a Collection of DataObjects from the DataContext and the underlying ObjectStore.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WILL_COMMIT

public static final EventSubject WILL_COMMIT

DID_COMMIT

public static final EventSubject DID_COMMIT

DID_ROLLBACK

public static final EventSubject DID_ROLLBACK

threadDataContext

protected static final java.lang.ThreadLocal threadDataContext
A holder of a DataContext bound to the current thread.

Since:
1.1

usingSharedSnaphsotCache

protected boolean usingSharedSnaphsotCache

validatingObjectsOnCommit

protected boolean validatingObjectsOnCommit

objectStore

protected ObjectStore objectStore

channel

protected transient DataChannel channel

entityResolver

protected transient EntityResolver entityResolver

mergeHandler

protected transient org.apache.cayenne.access.DataContextMergeHandler mergeHandler

userProperties

protected java.util.Map userProperties
Stores user defined properties associated with this DataContext.

Since:
1.2

lazyInitParentDomainName

protected transient java.lang.String lazyInitParentDomainName
Stores the name of parent DataDomain. Used to defer initialization of the parent QueryEngine after deserialization. This helps avoid an issue with certain servlet engines (e.g. Tomcat) where HttpSessions with DataContext's are deserialized at startup before Cayenne stack is fully initialized.

Constructor Detail

DataContext

public DataContext()
Creates a new DataContext that is not attached to the Cayenne stack.


DataContext

public DataContext(QueryEngine parent,
                   ObjectStore objectStore)
Deprecated. since 1.2 - use DataContext(DataChannel, ObjectStore) constructor instead. Note that DataDomain is both a DataChannel and a QueryEngine, so you may need to do a cast: new DataContext((DataChannel) domain, objectStore).

Creates a DataContext with parent QueryEngine and a DataRowStore that should be used by the ObjectStore.

Parameters:
parent - parent QueryEngine used to communicate with the data source.
objectStore - ObjectStore used by DataContext.
Since:
1.1

DataContext

public DataContext(DataChannel channel,
                   ObjectStore objectStore)
Creates a new DataContext with parent DataChannel and ObjectStore.

Since:
1.2
Method Detail

getThreadDataContext

public static DataContext getThreadDataContext()
                                        throws java.lang.IllegalStateException
Returns the DataContext bound to the current thread.

Returns:
the DataContext associated with caller thread.
Throws:
java.lang.IllegalStateException - if there is no DataContext bound to the current thread.
Since:
1.1
See Also:
WebApplicationContextFilter

bindThreadDataContext

public static void bindThreadDataContext(DataContext context)
Binds a DataContext to the current thread. DataContext can later be retrieved by users in the same thread by calling getThreadDataContext(). Using null parameter will unbind currently bound DataContext.

Since:
1.1

createDataContext

public static DataContext createDataContext()
Factory method that creates and returns a new instance of DataContext based on default domain. If more than one domain exists in the current configuration, createDataContext(String) must be used instead. ObjectStore associated with created DataContext will have a cache stack configured using parent domain settings.


createDataContext

public static DataContext createDataContext(boolean useSharedCache)
Factory method that creates and returns a new instance of DataContext based on default domain. If more than one domain exists in the current configuration, createDataContext(String, boolean) must be used instead. ObjectStore associated with newly created DataContext will have a cache stack configured according to the specified policy, overriding a parent domain setting.

Since:
1.1

createDataContext

public static DataContext createDataContext(java.lang.String domainName)
Factory method that creates and returns a new instance of DataContext using named domain as its parent. If there is no domain matching the name argument, an exception is thrown.


createDataContext

public static DataContext createDataContext(java.lang.String domainName,
                                            boolean useSharedCache)
Creates and returns new DataContext that will use a named DataDomain as its parent. ObjectStore associated with newly created DataContext will have a cache stack configured according to the specified policy, overriding a parent domain setting.

Since:
1.1

getUserProperties

protected java.util.Map getUserProperties()
Returns a map of user-defined properties associated with this DataContext.

Since:
1.2

createChildDataContext

public DataContext createChildDataContext()
Creates and returns a new child DataContext.

Since:
1.2

getUserProperty

public java.lang.Object getUserProperty(java.lang.String key)
Returns a user-defined property previously set via 'setUserProperty'. Note that it is a caller responsibility to synchronize access to properties.

Since:
1.2

setUserProperty

public void setUserProperty(java.lang.String key,
                            java.lang.Object value)
Sets a user-defined property. Note that it is a caller responsibility to synchronize access to properties.

Since:
1.2

getParent

public QueryEngine getParent()
Deprecated. since 1.2. Use 'getParentDataDomain()' or 'getChannel()' instead.

Returns parent QueryEngine object. In most cases returned object is an instance of DataDomain.


setParent

public void setParent(QueryEngine parent)
Deprecated. since 1.2, use setChannel instead.

Sets direct parent of this DataContext.


getChannel

public DataChannel getChannel()
Returns parent DataChannel, that is normally a DataDomain or another DataContext.

Specified by:
getChannel in interface ObjectContext
Since:
1.2

setChannel

public void setChannel(DataChannel channel)
Since:
1.2

getParentDataDomain

public DataDomain getParentDataDomain()
Returns a DataDomain used by this DataContext. DataDomain is looked up in the DataChannel hierarchy. If a channel is not a DataDomain or a DataContext, null is returned.

Returns:
DataDomain that is a direct or indirect parent of this DataContext in the DataChannel hierarchy.
Since:
1.1

setDelegate

public void setDelegate(DataContextDelegate delegate)
Sets a DataContextDelegate for this context. Delegate is notified of certain events in the DataContext lifecycle and can customize DataContext behavior.

Since:
1.1

getDelegate

public DataContextDelegate getDelegate()
Returns a delegate currently associated with this DataContext.

Since:
1.1

getObjectStore

public ObjectStore getObjectStore()
Returns ObjectStore associated with this DataContext.


hasChanges

public boolean hasChanges()
Returns true if there are any modified, deleted or new objects registered with this DataContext, false otherwise.


newObjects

public java.util.Collection newObjects()
Returns a list of objects that are registered with this DataContext and have a state PersistenceState.NEW

Specified by:
newObjects in interface ObjectContext

deletedObjects

public java.util.Collection deletedObjects()
Returns a list of objects that are registered with this DataContext and have a state PersistenceState.DELETED

Specified by:
deletedObjects in interface ObjectContext

modifiedObjects

public java.util.Collection modifiedObjects()
Returns a list of objects that are registered with this DataContext and have a state PersistenceState.MODIFIED

Specified by:
modifiedObjects in interface ObjectContext

uncommittedObjects

public java.util.Collection uncommittedObjects()
Returns a collection of all uncommitted registered objects.

Specified by:
uncommittedObjects in interface ObjectContext
Since:
1.2

registeredObject

public DataObject registeredObject(ObjectId id)
Deprecated. since 1.2 use 'localObject(id, null)'

Returns an object for a given ObjectId. When an object is not yet registered with this context's ObjectStore, the behavior of this method depends on whether ObjectId is permanent or temporary and whether a DataContext is a part of a nested context hierarchy or not. More specifically the following rules are applied in order:


currentSnapshot

public DataRow currentSnapshot(DataObject object)
Returns a DataRow reflecting current, possibly uncommitted, object state.

Warning: This method will return a partial snapshot if an object or one of its related objects that propagate their keys to this object have temporary ids. DO NOT USE this method if you expect a DataRow to represent a complete object state.

Since:
1.1

localObjects

public java.util.List localObjects(java.util.List objects)
Deprecated. since 1.2 - use localObject(ObjectId, Persistent) to specify how each local object should be handled.

Creates a list of DataObjects local to this DataContext from a list of DataObjects coming from a different DataContext. This method is a way to map objects from one context into the other (as opposed to "synchronize"). This means that the state of modified objects will be reflected only if this context is a child of an original DataObject context. If it is a peer or parent, you won't see any uncommitted changes from the original context.

Note that the objects in the list do not have to be of the same type or even from the same DataContext.

Since:
1.0.3

objectsFromDataRows

public java.util.List objectsFromDataRows(ObjEntity entity,
                                          java.util.List dataRows,
                                          boolean refresh,
                                          boolean resolveInheritanceHierarchy)
Converts a list of data rows to a list of DataObjects.

Since:
1.1

objectsFromDataRows

public java.util.List objectsFromDataRows(java.lang.Class objectClass,
                                          java.util.List dataRows,
                                          boolean refresh,
                                          boolean resolveInheritanceHierarchy)
Converts a list of DataRows to a List of DataObject registered with this DataContext. Internally calls objectsFromDataRows(ObjEntity,List,boolean,boolean).

Since:
1.1
See Also:
DataRow, DataObject

objectFromDataRow

public DataObject objectFromDataRow(java.lang.Class objectClass,
                                    DataRow dataRow,
                                    boolean refresh)
Creates a DataObject from DataRow. This is a convenience shortcut to objectsFromDataRows(Class,java.util.List,boolean,boolean).

See Also:
DataRow, DataObject

createAndRegisterNewObject

public DataObject createAndRegisterNewObject(java.lang.String objEntityName)
Instantiates new object and registers it with itself. Object class is determined from ObjEntity. Object class must have a default constructor.

Note: preferred way to create new objects is via createAndRegisterNewObject(Class)method. It works exactly the same way, but makes the application type-safe.

See Also:
createAndRegisterNewObject(Class)

newObject

public Persistent newObject(java.lang.Class persistentClass)
Creates and registers new persistent object. This is an ObjectContext version of 'createAndRegisterNewObject'.

Specified by:
newObject in interface ObjectContext
Since:
1.2

createAndRegisterNewObject

public DataObject createAndRegisterNewObject(java.lang.Class objectClass)
Instantiates new object and registers it with itself. Object class must have a default constructor.

Since:
1.1

registerNewObject

public void registerNewObject(DataObject object)
Registers a transient object with the context, recursively registering all transient DataObjects attached to this object via relationships.

Parameters:
object - new object that needs to be made persistent.

unregisterObjects

public void unregisterObjects(java.util.Collection dataObjects)
Unregisters a Collection of DataObjects from the DataContext and the underlying ObjectStore. This operation also unsets DataContext and ObjectId for each object and changes its state to TRANSIENT.

See Also:
invalidateObjects(Collection)

invalidateObjects

public void invalidateObjects(java.util.Collection dataObjects)
"Invalidates" a Collection of DataObject. This operation would remove each object's snapshot from cache and change object's state to HOLLOW. On the next access to this object, it will be refetched.

See Also:
unregisterObjects(Collection)

deleteObjects

public void deleteObjects(java.util.Collection objects)
Schedules all objects in the collection for deletion on the next commit of this DataContext. Object's persistence state is changed to PersistenceState.DELETED; objects related to this object are processed according to delete rules, i.e. relationships can be unset ("nullify" rule), deletion operation is cascaded (cascade rule).

"Nullify" delete rule side effect: passing a collection representing to-many relationship with nullify delete rule may result in objects being removed from collection.

Since:
1.2

deleteObject

public void deleteObject(Persistent object)
                  throws DeleteDenyException
Schedules an object for deletion on the next commit of this DataContext. Object's persistence state is changed to PersistenceState.DELETED; objects related to this object are processed according to delete rules, i.e. relationships can be unset ("nullify" rule), deletion operation is cascaded (cascade rule).

Specified by:
deleteObject in interface ObjectContext
Parameters:
object - a persistent object that we want to delete.
Throws:
DeleteDenyException - if a DENY delete rule is applicable for object deletion.
java.lang.NullPointerException - if object is null.
DeleteDenyException

refetchObject

public DataObject refetchObject(ObjectId oid)
Refetches object data for ObjectId. This method is used internally by Cayenne to resolve objects in state PersistenceState.HOLLOW. It can also be used to refresh certain objects.

Throws:
CayenneRuntimeException - if object id doesn't match any records, or if there is more than one object is fetched.

lookupDataNode

public DataNode lookupDataNode(DataMap dataMap)
Deprecated. since 1.2 DataContext's QueryEngine implementation is replaced by DataChannel. Use "getParentDataDomain().lookupDataNode(..)".

Returns a DataNode that should handle queries for all DataMap components.

Specified by:
lookupDataNode in interface QueryEngine
Since:
1.1

rollbackChangesLocally

public void rollbackChangesLocally()
If the parent channel is a DataContext, reverts local changes to make this context look like the parent, if the parent channel is a DataDomain, reverts all changes.

Specified by:
rollbackChangesLocally in interface ObjectContext
Since:
1.2

rollbackChanges

public void rollbackChanges()
Reverts any changes that have occurred to objects registered with DataContext; also performs cascading rollback of all parent DataContexts.

Specified by:
rollbackChanges in interface ObjectContext

commitChangesToParent

public void commitChangesToParent()
"Flushes" the changes to the parent DataChannel. If the parent channel is a DataContext, it updates its objects with this context's changes, without a database update. If it is a DataDomain (the most common case), the changes are written to the database. To cause cascading commit all the way to the database, one must use commitChanges().

Specified by:
commitChangesToParent in interface ObjectContext
Since:
1.2
See Also:
commitChanges()

commitChanges

public void commitChanges(org.apache.log4j.Level logLevel)
                   throws CayenneRuntimeException
Deprecated. Since 1.2, use commitChanges() instead.

Throws:
CayenneRuntimeException

commitChanges

public void commitChanges()
                   throws CayenneRuntimeException
Synchronizes object graph with the database. Executes needed insert, update and delete queries (generated internally).

Specified by:
commitChanges in interface ObjectContext
Throws:
CayenneRuntimeException

getEventManager

public EventManager getEventManager()
Returns EventManager associated with the ObjectStore.

Specified by:
getEventManager in interface DataChannel
Since:
1.2

onSync

public GraphDiff onSync(ObjectContext originatingContext,
                        GraphDiff changes,
                        int syncType)
An implementation of a DataChannel method that is used by child contexts to synchronize state with this context. Not intended for direct use.

Specified by:
onSync in interface DataChannel
Parameters:
originatingContext - an ObjectContext that initiated the sync. Can be null.
changes - diff from the context that initiated the sync.
syncType - One of DataChannel.FLUSH_NOCASCADE_SYNC, DataChannel.FLUSH_CASCADE_SYNC, DataChannel.ROLLBACK_CASCADE_SYNC.
Since:
1.2

performIteratedQuery

public ResultIterator performIteratedQuery(Query query)
                                    throws CayenneException
Performs a single database select query returning result as a ResultIterator. It is caller's responsibility to explicitly close the ResultIterator. A failure to do so will result in a database connection not being released. Another side effect of an open ResultIterator is that an internal Cayenne transaction that originated in this method stays open until the iterator is closed. So users should normally close the iterator within the same thread that opened it.

Throws:
CayenneException

performGenericQuery

public QueryResponse performGenericQuery(Query query)
Executes a query returning a generic response.

Specified by:
performGenericQuery in interface ObjectContext
Since:
1.2

performQuery

public java.util.List performQuery(Query query)
Performs a single selecting query. Various query setting control the behavior of this method and the results returned:

Since 1.2 takes any Query parameter, not just GenericSelectQuery

Specified by:
performQuery in interface ObjectContext
Returns:
A list of DataObjects or a DataRows, depending on the value returned by QueryMetadata.isFetchingDataRows().

onQuery

public QueryResponse onQuery(ObjectContext context,
                             Query query)
An implementation of a DataChannel method that is used by child contexts to execute queries. Not intended for direct use.

Specified by:
onQuery in interface DataChannel
Parameters:
context - an ObjectContext that originated the query, used to register result objects.
Returns:
a generic response object that encapsulates result of the execution.
Since:
1.2

performNonSelectingQuery

public int[] performNonSelectingQuery(Query query)
Performs a single database query that does not select rows. Returns an array of update counts.

Since:
1.1

performNonSelectingQuery

public int[] performNonSelectingQuery(java.lang.String queryName)
Performs a named mapped query that does not select rows. Returns an array of update counts.

Since:
1.1

performNonSelectingQuery

public int[] performNonSelectingQuery(java.lang.String queryName,
                                      java.util.Map parameters)
Performs a named mapped non-selecting query using a map of parameters. Returns an array of update counts.

Since:
1.1

performQueries

public void performQueries(java.util.Collection queries,
                           OperationObserver callback)
Deprecated. since 1.2 DataContext's QueryEngine implementation is replaced by DataChannel.

Executes all queries in collection.

Specified by:
performQueries in interface QueryEngine

performQueries

public void performQueries(java.util.Collection queries,
                           OperationObserver callback,
                           Transaction transaction)
Deprecated. since 1.2. Use Transaction.bindThreadTransaction(..) to provide custom transactions, besides DataContext's QueryEngine implementation is replaced by DataChannel.

Binds provided transaction to the current thread, and then runs queries.

Specified by:
performQueries in interface QueryEngine
Since:
1.1

prefetchRelationships

public void prefetchRelationships(SelectQuery query,
                                  java.util.List objects)
Deprecated. Since 1.2. This is a utility method that handles a very specific case. It shouldn't be in DataContext.

Performs prefetching. Prefetching would resolve a set of relationships for a list of DataObjects in the most optimized way (preferrably in a single query per relationship).

WARNING: Currently supports only "one-step" to one relationships. This is an arbitrary limitation and will be removed eventually.


performQuery

public java.util.List performQuery(java.lang.String queryName,
                                   boolean expireCachedLists)
Returns a list of objects or DataRows for a named query stored in one of the DataMaps. Internally Cayenne uses a caching policy defined in the named query. If refresh flag is true, a refresh is forced no matter what the caching policy is.

Parameters:
queryName - a name of a GenericSelectQuery defined in one of the DataMaps. If no such query is defined, this method will throw a CayenneRuntimeException.
expireCachedLists - A flag that determines whether refresh of cached lists is required in case a query uses caching.
Since:
1.1

performQuery

public java.util.List performQuery(java.lang.String queryName,
                                   java.util.Map parameters,
                                   boolean expireCachedLists)
Returns a list of objects or DataRows for a named query stored in one of the DataMaps. Internally Cayenne uses a caching policy defined in the named query. If refresh flag is true, a refresh is forced no matter what the caching policy is.

Parameters:
queryName - a name of a GenericSelectQuery defined in one of the DataMaps. If no such query is defined, this method will throw a CayenneRuntimeException.
parameters - A map of parameters to use with stored query.
expireCachedLists - A flag that determines whether refresh of cached lists is required in case a query uses caching.
Since:
1.1

getEntityResolver

public EntityResolver getEntityResolver()
Returns EntityResolver. EntityResolver can be null if DataContext has not been attached to an DataChannel.

Specified by:
getEntityResolver in interface QueryEngine
Specified by:
getEntityResolver in interface DataChannel
Specified by:
getEntityResolver in interface ObjectContext

setTransactionEventsEnabledDefault

public static void setTransactionEventsEnabledDefault(boolean flag)
Sets default for posting transaction events by new DataContexts.


setTransactionEventsEnabled

public void setTransactionEventsEnabled(boolean flag)
Enables or disables posting of transaction events by this DataContext.


isTransactionEventsEnabled

public boolean isTransactionEventsEnabled()

isUsingSharedSnapshotCache

public boolean isUsingSharedSnapshotCache()
Returns true if the ObjectStore uses shared cache of a parent DataDomain.

Since:
1.1

isValidatingObjectsOnCommit

public boolean isValidatingObjectsOnCommit()
Returns whether this DataContext performs object validation before commit is executed.

Since:
1.1

setValidatingObjectsOnCommit

public void setValidatingObjectsOnCommit(boolean flag)
Sets the property defining whether this DataContext should perform object validation before commit is executed.

Since:
1.1

getDataMaps

public java.util.Collection getDataMaps()
Deprecated. since 1.2. Use 'getEntityResolver().getDataMaps()' instead.

Description copied from interface: QueryEngine
Returns a collection of DataMaps associated with this QueryEngine.

Specified by:
getDataMaps in interface QueryEngine

prepareForAccess

public void prepareForAccess(Persistent object,
                             java.lang.String property)
Resolves object fault if needed. If a property is not null, it is assumed that the object will be modified, so object snapshot is retained and object state is changed.

Specified by:
prepareForAccess in interface ObjectContext
Since:
1.2

propertyChanged

public void propertyChanged(Persistent object,
                            java.lang.String property,
                            java.lang.Object oldValue,
                            java.lang.Object newValue)
Retains DataObject snapshot and changes its state if needed.

Specified by:
propertyChanged in interface ObjectContext
Since:
1.2

getGraphManager

public GraphManager getGraphManager()
Returns this context's ObjectStore.

Specified by:
getGraphManager in interface ObjectContext
Since:
1.2

localObject

public Persistent localObject(ObjectId id,
                              Persistent prototype)
Returns an object local to this DataContext and matching the ObjectId. If prototype is not null, local object is refreshed with the prototype values.

In case you pass a non-null second parameter, you are responsible for setting correct persistence state of the returned local object, as generally there is no way for Cayenne to determine the resulting local object state.

Specified by:
localObject in interface ObjectContext
Since:
1.2


Copyright © 2001-2006 Apache.org All Rights Reserved.