public class DataContext extends BaseContext
ObjectContext
. DataContext is an
isolated container of an object graph, in a sense that any uncommitted
changes to persistent objects that are registered with the context, are not
visible to the users of other contexts.Modifier and Type | Field and Description |
---|---|
protected org.apache.cayenne.access.DataContextMergeHandler |
mergeHandler |
protected ObjectStore |
objectStore |
protected TransactionFactory |
transactionFactory
Deprecated.
since 4.0 used in a method that itself should be deprecated,
so this is a temp code
|
protected boolean |
usingSharedSnaphsotCache |
channel, entityResolver, graphAction, queryCache, threadObjectContext, userProperties, validatingObjectsOnCommit
FLUSH_CASCADE_SYNC, FLUSH_NOCASCADE_SYNC, GRAPH_CHANGED_SUBJECT, GRAPH_FLUSHED_SUBJECT, GRAPH_ROLLEDBACK_SUBJECT, ROLLBACK_CASCADE_SYNC
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
protected void |
attachToChannel(DataChannel channel)
Attaches to a provided DataChannel.
|
protected void |
attachToRuntime(Injector injector)
Attaches this context to the CayenneRuntime whose Injector is passed as
an argument to this method.
|
void |
commitChanges()
Synchronizes object graph with the database.
|
void |
commitChangesToParent()
"Flushes" the changes to the parent
DataChannel . |
DataRow |
currentSnapshot(Persistent object)
Returns a DataRow reflecting current, possibly uncommitted, object state.
|
Collection<?> |
deletedObjects()
Returns a list of objects that are registered with this DataContext and
have a state PersistenceState.DELETED
|
protected void |
fireDataChannelChanged(Object postedBy,
GraphDiff changes) |
DataContextDelegate |
getDelegate()
Returns a delegate currently associated with this DataContext.
|
GraphManager |
getGraphManager()
Returns this context's ObjectStore.
|
ObjectStore |
getObjectStore()
Returns ObjectStore associated with this DataContext.
|
DataDomain |
getParentDataDomain()
Returns a DataDomain used by this DataContext.
|
boolean |
hasChanges()
Returns
true if there are any modified, deleted or new
objects registered with this DataContext, false otherwise. |
boolean |
isUsingSharedSnapshotCache()
Returns
true if the ObjectStore uses shared cache of a
parent DataDomain. |
<T> ResultIterator<T> |
iterator(Select<T> query)
Creates a ResultIterator based on the provided query.
|
Collection<?> |
modifiedObjects()
Returns a list of objects that are registered with this DataContext and
have a state PersistenceState.MODIFIED
|
<T> T |
newObject(Class<T> persistentClass)
Creates and registers a new persistent object.
|
Persistent |
newObject(String entityName)
Instantiates a new object and registers it with this context.
|
Collection<?> |
newObjects()
Returns a list of objects that are registered with this DataContext and
have a state PersistenceState.NEW
|
<T extends Persistent> |
objectFromDataRow(Class<T> objectClass,
DataRow dataRow)
Creates a DataObject from DataRow.
|
DataObject |
objectFromDataRow(String entityName,
DataRow dataRow)
Creates a DataObject from DataRow.
|
List |
objectsFromDataRows(ClassDescriptor descriptor,
List<? extends DataRow> dataRows)
Converts a list of DataRows to a List of DataObject registered with this
DataContext.
|
protected GraphDiff |
onContextFlush(ObjectContext originatingContext,
GraphDiff changes,
boolean cascade) |
QueryResponse |
onQuery(ObjectContext context,
Query query)
An implementation of a
DataChannel method that is used by child
contexts to execute queries. |
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(String queryName)
Performs a named mapped query that does not select rows.
|
int[] |
performNonSelectingQuery(String queryName,
Map<String,?> parameters)
Performs a named mapped non-selecting query using a map of parameters.
|
List |
performQuery(Query query)
Performs a single selecting query.
|
List<?> |
performQuery(String queryName,
boolean expireCachedLists)
Returns a list of objects or DataRows for a named query stored in one of
the DataMaps.
|
List<?> |
performQuery(String queryName,
Map parameters,
boolean expireCachedLists)
Returns a list of objects or DataRows for a named query stored in one of
the DataMaps.
|
void |
registerNewObject(Object object)
Registers a transient object with the context, recursively registering
all transient persistent objects 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 |
setDelegate(DataContextDelegate delegate)
Sets a DataContextDelegate for this context.
|
void |
setTransactionFactory(TransactionFactory transactionFactory)
Deprecated.
since 4.0 avoid using this directly. Transaction management
at this level will be eventually removed
|
void |
setUsingSharedSnapshotCache(boolean flag) |
Collection<?> |
uncommittedObjects()
Returns a collection of all uncommitted registered objects.
|
void |
unregisterObjects(Collection dataObjects)
Unregisters a Collection of DataObjects from the DataContext and the
underlying ObjectStore.
|
attachToRuntimeIfNeeded, batchIterator, bindThreadObjectContext, deleteObject, deleteObjects, deleteObjects, fireDataChannelCommitted, fireDataChannelRolledback, getChannel, getEntityResolver, getEventManager, getQueryCache, getThreadObjectContext, getUserProperties, getUserProperty, injectInitialValue, invalidateObjects, invalidateObjects, isValidatingObjectsOnCommit, iterate, localObject, onSync, prepareForAccess, propertyChanged, select, selectFirst, selectOne, setChannel, setEntityResolver, setQueryCache, setUserProperty, setValidatingObjectsOnCommit
protected boolean usingSharedSnaphsotCache
protected ObjectStore objectStore
@Deprecated protected transient TransactionFactory transactionFactory
protected transient org.apache.cayenne.access.DataContextMergeHandler mergeHandler
public DataContext()
public DataContext(DataChannel channel, ObjectStore objectStore)
protected void attachToRuntime(Injector injector)
BaseContext
attachToRuntime
in class BaseContext
protected void attachToChannel(DataChannel channel)
BaseContext
attachToChannel
in class BaseContext
public DataDomain getParentDataDomain()
public void setDelegate(DataContextDelegate delegate)
public DataContextDelegate getDelegate()
public ObjectStore getObjectStore()
public boolean hasChanges()
true
if there are any modified, deleted or new
objects registered with this DataContext, false
otherwise.public Collection<?> newObjects()
newObjects
in interface ObjectContext
newObjects
in class BaseContext
public Collection<?> deletedObjects()
deletedObjects
in interface ObjectContext
deletedObjects
in class BaseContext
public Collection<?> modifiedObjects()
modifiedObjects
in interface ObjectContext
modifiedObjects
in class BaseContext
public Collection<?> uncommittedObjects()
uncommittedObjects
in interface ObjectContext
uncommittedObjects
in class BaseContext
public DataRow currentSnapshot(Persistent object)
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.
public List objectsFromDataRows(ClassDescriptor descriptor, List<? extends DataRow> dataRows)
public <T extends Persistent> T objectFromDataRow(Class<T> objectClass, DataRow dataRow)
DataRow
public DataObject objectFromDataRow(String entityName, DataRow dataRow)
DataRow
public <T> T newObject(Class<T> persistentClass)
newObject
in interface ObjectContext
newObject
in class BaseContext
public Persistent newObject(String entityName)
Note: in most cases newObject(Class)
method should be used,
however this method is helpful when generic persistent classes are
used.
public void registerNewObject(Object object)
Note that since 3.0 this method takes Object as an argument instead of
a DataObject
.
registerNewObject
in interface ObjectContext
registerNewObject
in class BaseContext
object
- new object that needs to be made persistent.public void unregisterObjects(Collection dataObjects)
public void rollbackChangesLocally()
rollbackChangesLocally
in interface ObjectContext
rollbackChangesLocally
in class BaseContext
public void rollbackChanges()
rollbackChanges
in interface ObjectContext
rollbackChanges
in class BaseContext
public void commitChangesToParent()
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()
.commitChangesToParent
in interface ObjectContext
commitChangesToParent
in class BaseContext
commitChanges()
public void commitChanges() throws CayenneRuntimeException
commitChanges
in interface ObjectContext
commitChanges
in class BaseContext
CayenneRuntimeException
protected GraphDiff onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade)
onContextFlush
in class BaseContext
public <T> ResultIterator<T> iterator(Select<T> query)
ObjectContext
ObjectContext.iterate(Select, ResultIteratorCallback)
as an alternative.iterator
in interface ObjectContext
iterator
in class BaseContext
public ResultIterator performIteratedQuery(Query query)
Note that 'performIteratedQuery' always returns ResultIterator over
DataRows. Use
BaseContext.iterate(Select, org.apache.cayenne.ResultIteratorCallback)
to
get access to objects.
public QueryResponse performGenericQuery(Query query)
performGenericQuery
in interface ObjectContext
performGenericQuery
in class BaseContext
public List performQuery(Query query)
Since 1.2 takes any Query parameter, not just GenericSelectQuery
performQuery
in interface ObjectContext
performQuery
in class BaseContext
QueryMetadata.isFetchingDataRows()
.public QueryResponse onQuery(ObjectContext context, Query query)
DataChannel
method that is used by child
contexts to execute queries. Not intended for direct use.context
- an ObjectContext that originated the query, used to
register result objects.public int[] performNonSelectingQuery(Query query)
public int[] performNonSelectingQuery(String queryName)
public int[] performNonSelectingQuery(String queryName, Map<String,?> parameters)
public List<?> performQuery(String queryName, boolean expireCachedLists)
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.public List<?> performQuery(String queryName, Map parameters, boolean expireCachedLists)
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.public boolean isUsingSharedSnapshotCache()
true
if the ObjectStore uses shared cache of a
parent DataDomain.public void setUsingSharedSnapshotCache(boolean flag)
public GraphManager getGraphManager()
getGraphManager
in interface ObjectContext
getGraphManager
in class BaseContext
protected void fireDataChannelChanged(Object postedBy, GraphDiff changes)
fireDataChannelChanged
in class BaseContext
@Deprecated public void setTransactionFactory(TransactionFactory transactionFactory)
Copyright © 2001–2018 Apache Cayenne. All rights reserved.