Class BaseContext
- All Implemented Interfaces:
Serializable,DataChannel,ObjectContext
- Direct Known Subclasses:
DataContext
- Since:
- 3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DataChannelprotected EntityResolverprotected ObjectContextGraphActionGraph action that handles property changesprotected QueryCacheprotected static final ThreadLocal<ObjectContext> A holder of a ObjectContext bound to the current thread.Stores user defined properties associated with this DataContext.protected booleanFields 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidattachToChannel(DataChannel channel) Attaches to a provided DataChannel.protected voidattachToRuntime(Injector injector) Attaches this context to the CayenneRuntime whose Injector is passed as an argument to this method.protected booleanChecks whether this context is attached to Cayenne runtime stack and if not, attempts to attach itself to the runtime using Injector returned from the call toCayenneRuntime.getThreadInjector().<T> ResultBatchIterator<T> batchIterator(Select<T> query, int size) Creates a ResultBatchIterator based on the provided query and batch size.static voidbindThreadObjectContext(ObjectContext context) Binds a ObjectContext to the current thread.abstract voidFlushes all changes to objects in this context to the parent DataChannel, cascading flush operation all the way through the stack, ultimately saving data in the database.abstract voidFlushes all changes to objects in this context to the parent DataChannel.abstract Collection<?> Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.DELETEDvoiddeleteObject(Object object) Schedules deletion of a persistent object.voiddeleteObjects(Collection<?> objects) Schedules deletion of a collection of persistent objects.<T> voiddeleteObjects(T... objects) Schedules deletion of one or more persistent objects.protected voidfireDataChannelChanged(Object postedBy, GraphDiff changes) protected voidfireDataChannelCommitted(Object postedBy, GraphDiff changes) protected voidfireDataChannelRolledback(Object postedBy, GraphDiff changes) Returns an DataChannel used by this context.Returns EntityResolver that stores all mapping information accessible by this ObjectContext.Returns EventManager associated with the ObjectStore.abstract GraphManagerReturns GraphManager that manages object graph associated with this context.static ObjectContextReturns the ObjectContext bound to the current thread.Returns a map of user-defined properties associated with this DataContext.getUserProperty(String key) Returns a user-defined property previously set via 'setUserProperty'.protected voidinjectInitialValue(Object obj) If ObjEntity qualifier is set, asks it to inject initial value to an object.voidinvalidateObjects(Collection<?> objects) Invalidates a Collection of persistent objects.<T> voidinvalidateObjects(T... objects) Invalidates one or more persistent objects.booleanReturns whether this ObjectContext performs object validation before commit is executed.<T> voiditerate(Select<T> query, ResultIteratorCallback<T> callback) Creates a ResultIterator based on the provided query and passes it to a callback for processing.abstract <T> ResultIterator<T> Creates a ResultIterator based on the provided query.<T extends Persistent>
TlocalObject(T objectFromAnotherContext) Returns a local copy of 'objectFromAnotherContext' object.abstract Collection<?> Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.MODIFIEDabstract <T> TCreates a new persistent object of a given class scheduled to be inserted to the database on next commit.abstract Collection<?> Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.NEWprotected abstract GraphDiffonContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade) onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) Processes synchronization request from a child ObjectContext, returning a GraphDiff that describes changes to objects made on the receiving end as a result of synchronization.abstract QueryResponseperformGenericQuery(Query query) Executes any kind of query providing the result in a form of QueryResponse.abstract ListperformQuery(Query query) Executes a selecting query, returning a list of persistent objects or data rows.voidprepareForAccess(Persistent object, String property, boolean lazyFaulting) A callback method that child Persistent objects are expected to call before accessing property values.voidpropertyChanged(Persistent object, String property, Object oldValue, Object newValue) A callback method that child Persistent objects are expected to call from inside the setter after modifying a value of a persistent property, including "simple" and "arc" properties.abstract voidregisterNewObject(Object object) Registers a transient object with the context.abstract voidResets all uncommitted changes made to the objects in this ObjectContext, cascading rollback operation all the way through the stack.abstract voidResets all uncommitted changes made to the objects in this ObjectContext.<T> List<T> Executes a selecting query, returning a list of persistent objects or data rows.<T> TselectFirst(Select<T> query) Selects a single object using provided query.<T> TExecutes a selecting query, returning either NULL if query matched no objects, or a single object.voidsetChannel(DataChannel channel) Sets a new DataChannel for this context.voidsetEntityResolver(EntityResolver entityResolver) voidsetQueryCache(QueryCache queryCache) Sets a QueryCache to be used for storing cached query results.voidsetUserProperty(String key, Object value) Sets a user-defined property.voidsetValidatingObjectsOnCommit(boolean flag) Sets the property defining whether this ObjectContext should perform object validation before commit is executed.abstract Collection<?> Returns a collection of MODIFIED, DELETED or NEW objects.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.cayenne.DataChannel
onQueryMethods inherited from interface org.apache.cayenne.ObjectContext
hasChanges
-
Field Details
-
threadObjectContext
A holder of a ObjectContext bound to the current thread.- Since:
- 3.0
-
channel
-
queryCache
-
entityResolver
-
validatingObjectsOnCommit
protected boolean validatingObjectsOnCommit -
graphAction
Graph action that handles property changes- Since:
- 3.1
-
userProperties
Stores user defined properties associated with this DataContext.- Since:
- 3.0
-
-
Constructor Details
-
BaseContext
protected BaseContext()
-
-
Method Details
-
getThreadObjectContext
Returns the ObjectContext bound to the current thread.- Returns:
- the ObjectContext associated with caller thread.
- Throws:
IllegalStateException- if there is no ObjectContext bound to the current thread.- Since:
- 3.0
-
bindThreadObjectContext
Binds a ObjectContext to the current thread. ObjectContext can later be retrieved by users in the same thread by callinggetThreadObjectContext(). Using null parameter will unbind currently bound ObjectContext.- Since:
- 3.0
-
attachToRuntimeIfNeeded
protected boolean attachToRuntimeIfNeeded()Checks whether this context is attached to Cayenne runtime stack and if not, attempts to attach itself to the runtime using Injector returned from the call toCayenneRuntime.getThreadInjector(). If thread Injector is not available and the context is not attached, throws CayenneRuntimeException.This method is called internally by the context before access to transient variables to allow the context to attach to the stack lazily following deserialization.
- Returns:
- true if the context successfully attached to the thread runtime, false - if it was already attached.
- Since:
- 3.1
-
attachToRuntime
Attaches this context to the CayenneRuntime whose Injector is passed as an argument to this method.- Since:
- 3.1
-
attachToChannel
Attaches to a provided DataChannel.- Since:
- 3.1
-
commitChanges
public abstract void commitChanges()Description copied from interface:ObjectContextFlushes all changes to objects in this context to the parent DataChannel, cascading flush operation all the way through the stack, ultimately saving data in the database.- Specified by:
commitChangesin interfaceObjectContext
-
commitChangesToParent
public abstract void commitChangesToParent()Description copied from interface:ObjectContextFlushes all changes to objects in this context to the parent DataChannel. Same asObjectContext.commitChanges(), but no cascading flush occurs.- Specified by:
commitChangesToParentin interfaceObjectContext
-
deleteObject
Description copied from interface:ObjectContextSchedules deletion of a persistent object.- Specified by:
deleteObjectin interfaceObjectContext- Throws:
DeleteDenyException- if aDeleteRule.DENYdelete rule is applicable for object deletion.
-
deletedObjects
Description copied from interface:ObjectContextReturns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.DELETED- Specified by:
deletedObjectsin interfaceObjectContext
-
getChannel
Description copied from interface:ObjectContextReturns an DataChannel used by this context.- Specified by:
getChannelin interfaceObjectContext
-
setChannel
Sets a new DataChannel for this context.- Since:
- 3.1
-
getEntityResolver
Description copied from interface:ObjectContextReturns EntityResolver that stores all mapping information accessible by this ObjectContext.- Specified by:
getEntityResolverin interfaceDataChannel- Specified by:
getEntityResolverin interfaceObjectContext
-
setEntityResolver
- Since:
- 3.1
-
isValidatingObjectsOnCommit
public boolean isValidatingObjectsOnCommit()Returns whether this ObjectContext performs object validation before commit is executed.- Since:
- 1.1
-
setValidatingObjectsOnCommit
public void setValidatingObjectsOnCommit(boolean flag) Sets the property defining whether this ObjectContext should perform object validation before commit is executed.- Since:
- 1.1
-
localObject
Description copied from interface:ObjectContextReturns a local copy of 'objectFromAnotherContext' object. "Local" means that the returned object is registered in this context. If the local object hasn't been previously cached in this context, a hollow object is created and returned to the caller. No DB query is performed to resolve an object.Note that passing an object with a non-existing id, may later result in FaultFailureException on attempt to read returned object properties.
- Specified by:
localObjectin interfaceObjectContext- Since:
- 3.1
-
getGraphManager
Description copied from interface:ObjectContextReturns GraphManager that manages object graph associated with this context.- Specified by:
getGraphManagerin interfaceObjectContext
-
modifiedObjects
Description copied from interface:ObjectContextReturns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.MODIFIED- Specified by:
modifiedObjectsin interfaceObjectContext
-
newObject
Description copied from interface:ObjectContextCreates a new persistent object of a given class scheduled to be inserted to the database on next commit.- Specified by:
newObjectin interfaceObjectContext
-
registerNewObject
Description copied from interface:ObjectContextRegisters a transient object with the context. The difference withObjectContext.newObject(Class)is that a user creates an object herself, before attaching it to the context, instead of relying on Cayenne to do that.- Specified by:
registerNewObjectin interfaceObjectContext- Parameters:
object- new object that needs to be made persistent.
-
newObjects
Description copied from interface:ObjectContextReturns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.NEW- Specified by:
newObjectsin interfaceObjectContext
-
performGenericQuery
Description copied from interface:ObjectContextExecutes any kind of query providing the result in a form of QueryResponse.- Specified by:
performGenericQueryin interfaceObjectContext
-
performQuery
Description copied from interface:ObjectContextExecutes a selecting query, returning a list of persistent objects or data rows.- Specified by:
performQueryin interfaceObjectContext
-
select
Description copied from interface:ObjectContextExecutes a selecting query, returning a list of persistent objects or data rows.- Specified by:
selectin interfaceObjectContext- Since:
- 4.0
-
selectOne
Description copied from interface:ObjectContextExecutes a selecting query, returning either NULL if query matched no objects, or a single object. If query matches more than one object,CayenneRuntimeExceptionis thrown.- Specified by:
selectOnein interfaceObjectContext- Since:
- 4.0
-
selectFirst
Description copied from interface:ObjectContextSelects a single object using provided query. The query itself can match any number of objects, but will return only the first one. It returns null if no objects were matched.If it matched more than one object, the first object from the list is returned. This makes 'selectFirst' different from
ObjectContext.selectOne(Select), which would throw in this situation. 'selectFirst' is useful e.g. when the query is ordered and we only want to see the first object (e.g. "most recent news article"), etc.Selecting the first object via "Select.selectFirst(ObjectContext)" is more comprehensible than selecting via "ObjectContext.selectFirst(Select)", because implementations of "Select" set fetch size limit to one.
- Specified by:
selectFirstin interfaceObjectContext- Since:
- 4.0
-
iterate
Description copied from interface:ObjectContextCreates a ResultIterator based on the provided query and passes it to a callback for processing. The caller does not need to worry about closing the iterator. This method takes care of it.- Specified by:
iteratein interfaceObjectContext- Since:
- 4.0
-
iterator
Description copied from interface:ObjectContextCreates a ResultIterator based on the provided query. It is usually backed by an open result set and is useful for processing of large data sets, preserving a constant memory footprint. The caller must wrap iteration in try/finally (or try-with-resources for Java 1.7 and higher) and close the ResultIterator explicitly. Or useObjectContext.iterate(Select, ResultIteratorCallback)as an alternative.- Specified by:
iteratorin interfaceObjectContext
-
batchIterator
Description copied from interface:ObjectContextCreates a ResultBatchIterator based on the provided query and batch size. It is usually backed by an open result set and is useful for processing of large data sets, preserving a constant memory footprint. The caller must wrap iteration in try/finally (or try-with-resources for Java 1.7 and higher) and close the ResultBatchIterator explicitly.- Specified by:
batchIteratorin interfaceObjectContext
-
prepareForAccess
Description copied from interface:ObjectContextA callback method that child Persistent objects are expected to call before accessing property values. This callback allows ObjectContext to "inflate" unresolved objects on demand and also resolve properties that rely on lazy faulting.- Specified by:
prepareForAccessin interfaceObjectContext
-
propertyChanged
Description copied from interface:ObjectContextA callback method that child Persistent objects are expected to call from inside the setter after modifying a value of a persistent property, including "simple" and "arc" properties.- Specified by:
propertyChangedin interfaceObjectContext
-
rollbackChanges
public abstract void rollbackChanges()Description copied from interface:ObjectContextResets all uncommitted changes made to the objects in this ObjectContext, cascading rollback operation all the way through the stack.- Specified by:
rollbackChangesin interfaceObjectContext
-
rollbackChangesLocally
public abstract void rollbackChangesLocally()Description copied from interface:ObjectContextResets all uncommitted changes made to the objects in this ObjectContext. Same asObjectContext.rollbackChanges(), but rollback is local to this context and no cascading changes undoing occurs.- Specified by:
rollbackChangesLocallyin interfaceObjectContext
-
uncommittedObjects
Description copied from interface:ObjectContextReturns a collection of MODIFIED, DELETED or NEW objects.- Specified by:
uncommittedObjectsin interfaceObjectContext
-
getQueryCache
-
setQueryCache
Sets a QueryCache to be used for storing cached query results. -
getEventManager
Returns EventManager associated with the ObjectStore.- Specified by:
getEventManagerin interfaceDataChannel- Since:
- 1.2
-
onSync
Description copied from interface:DataChannelProcesses synchronization request from a child ObjectContext, returning a GraphDiff that describes changes to objects made on the receiving end as a result of synchronization.- Specified by:
onSyncin interfaceDataChannel- Parameters:
originatingContext- an ObjectContext that initiated the sync. Can be null.changes- diff from the context that initiated the sync.syncType- One ofDataChannel.FLUSH_NOCASCADE_SYNC,DataChannel.FLUSH_CASCADE_SYNC,DataChannel.ROLLBACK_CASCADE_SYNC.
-
onContextFlush
protected abstract GraphDiff onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade) -
fireDataChannelCommitted
- Since:
- 1.2
-
fireDataChannelRolledback
- Since:
- 1.2
-
fireDataChannelChanged
- Since:
- 1.2
-
invalidateObjects
Description copied from interface:ObjectContextInvalidates a Collection of persistent objects. This operation only applies to the objects already committed to the database and does nothing to the NEW objects. It would remove each object's snapshot from caches and change object's state to HOLLOW. On the next access to this object, the object will be refetched.- Specified by:
invalidateObjectsin interfaceObjectContext
-
invalidateObjects
public <T> void invalidateObjects(T... objects) Description copied from interface:ObjectContextInvalidates one or more persistent objects. Same asObjectContext.invalidateObjects(Collection)only with a vararg argument list for easier invalidation of individual objects. If no arguments are passed to this method, it does nothing.- Specified by:
invalidateObjectsin interfaceObjectContext- Since:
- 3.1
-
getUserProperties
Returns a map of user-defined properties associated with this DataContext.- Since:
- 3.0
-
getUserProperty
Returns a user-defined property previously set via 'setUserProperty'. Note that it is a caller responsibility to synchronize access to properties.- Specified by:
getUserPropertyin interfaceObjectContext- Since:
- 3.0
-
setUserProperty
Sets a user-defined property. Note that it is a caller responsibility to synchronize access to properties.- Specified by:
setUserPropertyin interfaceObjectContext- Since:
- 3.0
-
injectInitialValue
If ObjEntity qualifier is set, asks it to inject initial value to an object. Also performs all Persistent initialization operations -
deleteObjects
Description copied from interface:ObjectContextSchedules deletion of one or more persistent objects. Same asObjectContext.deleteObjects(Collection)only with a vararg argument list for easier deletion of individual objects.- Specified by:
deleteObjectsin interfaceObjectContext- Throws:
DeleteDenyException- if aDeleteRule.DENYdelete rule is applicable for object deletion.- Since:
- 3.1
-
deleteObjects
Description copied from interface:ObjectContextSchedules deletion of a collection of persistent objects.- Specified by:
deleteObjectsin interfaceObjectContext- Throws:
DeleteDenyException- if aDeleteRule.DENYdelete rule is applicable for object deletion.
-