Class DataContext
- All Implemented Interfaces:
ObjectContext
ObjectContext.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final DataChannelprotected final EntityResolverprotected final DataContextObjectStoreprotected final QueryCacheprotected static final ThreadLocal<ObjectContext> Deprecated, for removal: This API element is subject to removal in a future version.Stores user defined properties associated with this DataContext.protected final booleanprotected final boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDataContext(DataChannel channel, DataRowStore snapshotCache, Map<ObjectId, Persistent> objectMap, boolean syncWithSnapshotCache, boolean usingSharedSnapshotCache, boolean validatingObjectsOnCommit, QueryCache queryCache) Creates a DataContext and its ObjectStore as a single unit. -
Method Summary
Modifier and TypeMethodDescription<T> ResultBatchIterator<T> batchIterator(Select<T> query, int size) Creates a ResultBatchIterator based on the provided query and batch size.static voidbindThreadObjectContext(ObjectContext context) Deprecated, for removal: This API element is subject to removal in a future version.if you are using thread context, you can create your own ThreadLocalstatic DataContext.Builderbuilder(DataChannel channel) Starts building a DataContext attached to the given channel.voidRemoves all user-defined properties.voidSynchronizes object graph with the database.void"Flushes" the changes to the parentDataChannel.currentSnapshot(Persistent object) Returns aDataRowreflecting current, possibly uncommitted, object state.<T extends Persistent>
voiddeleteObjects(T... objects) Schedules deletion of one or more persistent objects.Executes a query returning all of its result sets, update counts, iterators and OUT parameters in the order they were produced.protected voidfireDataChannelChanged(Object postedBy, GraphDiff changes) protected voidfireDataChannelCommitted(Object postedBy, GraphDiff changes) protected voidfireDataChannelRolledback(Object postedBy, GraphDiff changes) Returns a DataChannel that this context uses to access the persistent store.Returns a delegate currently associated with this DataContext.Returns EntityResolver that stores all mapping information accessible by this ObjectContext.Returns the store of persistent objects registered with this context.Deprecated, for removal: This API element is subject to removal in a future version.useDataChannel.getDataDomain()on the channel returned bygetChannel()Returns a cache for query results, set viaDataContext.Builder.queryCache(QueryCache).static ObjectContextDeprecated, for removal: This API element is subject to removal in a future version.if you are using thread context, you can create your own ThreadLocalReturns a map of user-defined properties associated with this DataContext.getUserProperty(String key) Returns a user-defined property previously set via 'setUserProperty'.voidinvalidateObjects(Collection<? extends Persistent> objects) Invalidates a Collection of persistent objects.final <T extends Persistent>
voidinvalidateObjects(T... objects) Invalidates one or more persistent objects.booleanReturnstrueif the ObjectStore uses shared cache of a parent DataDomain.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.<T> ResultIterator<T> Performs a single database select query returning result as aResultIterator.<T extends Persistent>
TlocalObject(T objectFromAnotherContext) Returns a local copy of 'objectFromAnotherContext' object.Creates a new persistent object of a given entity scheduled to be inserted to the database on next commit.objectForPK(ObjectId id) Returns an object matching an ObjectId, or null if there's no such object.<T extends Persistent>
TobjectFromDataRow(Class<T> objectClass, DataRow dataRow) Creates a Persistent from DataRow.objectFromDataRow(String entityName, DataRow dataRow) Creates a Persistent from DataRow.objectsFromDataRows(ClassDescriptor descriptor, List<? extends DataRow> dataRows) Converts a list of DataRows to a List of Persistent registered with this DataContext.int[]performNonSelectingQuery(String queryName) Deprecated, for removal: This API element is subject to removal in a future version.useMappedExec.query(queryName).update(context)int[]performNonSelectingQuery(String queryName, Map<String, ?> parameters) Deprecated, for removal: This API element is subject to removal in a future version.useMappedExec.query(queryName).params(parameters).update(context)int[]performNonSelectingQuery(Query query) Deprecated, for removal: This API element is subject to removal in a future version.useexecute(Query), orSQLExec.update(ObjectContext)/MappedExec.update(ObjectContext)for a query that produces a single update countList<?> performQuery(String queryName, boolean expireCachedLists) Deprecated, for removal: This API element is subject to removal in a future version.useMappedSelect.query(queryName).select(context), callingforceNoCache()on the query to expire cached listsList<?> performQuery(String queryName, Map<String, ?> parameters, boolean expireCachedLists) Deprecated, for removal: This API element is subject to removal in a future version.useMappedSelect.query(queryName).params(parameters).select(context), callingforceNoCache()on the query to expire cached listsvoidprepareForAccess(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.voidregisterNewObject(Persistent object) Registers a transient object with the context, recursively registering all transient persistent objects attached to this object via relationships.voidremoveUserProperty(String key) Removes a user-defined property.voidReverts any changes that have occurred to objects registered with DataContext; also performs cascading rollback of all parent DataContexts.voidIf 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.<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.voidsetDelegate(DataContextDelegate delegate) Sets a DataContextDelegate for this context.voidsetUserProperty(String key, Object value) Sets a user-defined property.Returns a collection of all uncommitted registered objects.voidunregisterObjects(Collection<? extends Persistent> objects) Unregisters a Collection of Persistent objects from the DataContext and the underlying ObjectStore.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ObjectContext
deletedObjects, deleteObject, deleteObjects, hasChanges, modifiedObjects, newObject, newObjects, objectForPK, objectForPK, objectForPK, objectForPK, performQuery
-
Field Details
-
threadObjectContext
@Deprecated(since="5.0", forRemoval=true) protected static final ThreadLocal<ObjectContext> threadObjectContextDeprecated, for removal: This API element is subject to removal in a future version.A holder of a ObjectContext bound to the current thread.- Since:
- 3.0
-
channel
-
objectStore
-
queryCache
-
entityResolver
-
validatingObjectsOnCommit
protected final boolean validatingObjectsOnCommit -
userProperties
-
-
Constructor Details
-
DataContext
protected DataContext(DataChannel channel, DataRowStore snapshotCache, Map<ObjectId, Persistent> objectMap, boolean syncWithSnapshotCache, boolean usingSharedSnapshotCache, boolean validatingObjectsOnCommit, QueryCache queryCache) Creates a DataContext and its ObjectStore as a single unit. Both objects reference each other, so the store is created here rather than passed in, keeping the reference immutable on both sides. Usebuilder(DataChannel)to create instances.- Since:
- 5.0
-
-
Method Details
-
getThreadObjectContext
@Deprecated(since="5.0", forRemoval=true) public static ObjectContext getThreadObjectContext() throws IllegalStateExceptionDeprecated, for removal: This API element is subject to removal in a future version.if you are using thread context, you can create your own ThreadLocalReturns 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
@Deprecated(since="5.0", forRemoval=true) public static void bindThreadObjectContext(ObjectContext context) Deprecated, for removal: This API element is subject to removal in a future version.if you are using thread context, you can create your own ThreadLocalBinds 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
-
builder
Starts building a DataContext attached to the given channel. All builder settings are optional.- Since:
- 5.0
-
getChannel
Description copied from interface:ObjectContextReturns a DataChannel that this context uses to access the persistent store.- Specified by:
getChannelin interfaceObjectContext
-
getEntityResolver
Description copied from interface:ObjectContextReturns EntityResolver that stores all mapping information accessible by this ObjectContext.- Specified by:
getEntityResolverin interfaceObjectContext
-
getParentDataDomain
Deprecated, for removal: This API element is subject to removal in a future version.useDataChannel.getDataDomain()on the channel returned bygetChannel()- Returns:
- DataDomain that is a direct or indirect parent of this DataContext in the DataChannel hierarchy.
- Since:
- 1.1
-
setDelegate
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
Returns a delegate currently associated with this DataContext.- Since:
- 1.1
-
getObjectStore
Description copied from interface:ObjectContextReturns the store of persistent objects registered with this context.- Specified by:
getObjectStorein interfaceObjectContext
-
deleteObjects
Description copied from interface:ObjectContextSchedules deletion of one or more persistent objects.- Specified by:
deleteObjectsin interfaceObjectContext- Throws:
DeleteDenyException- if aDeleteRule.DENYdelete rule is applicable for object deletion.- Since:
- 3.1
-
uncommittedObjects
Returns a collection of all uncommitted registered objects.- Specified by:
uncommittedObjectsin interfaceObjectContext- Since:
- 1.2
-
getQueryCache
Returns a cache for query results, set viaDataContext.Builder.queryCache(QueryCache). -
fireDataChannelCommitted
-
fireDataChannelRolledback
-
objectForPK
Description copied from interface:ObjectContextReturns an object matching an ObjectId, or null if there's no such object. The id may be that of a superentity of the actual object entity.A resolved object registered in this context is returned without a query. Otherwise the object is looked up in the caches down the DataChannel stack, and then in the database.
- Specified by:
objectForPKin interfaceObjectContext- Since:
- 5.0
-
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
-
currentSnapshot
Returns aDataRowreflecting 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.
- Parameters:
object- persistent object to create snapshot for- Returns:
- current snapshot of the persistent object
- Since:
- 1.1
-
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
-
objectsFromDataRows
Converts a list of DataRows to a List of Persistent registered with this DataContext.- Since:
- 3.0
-
objectFromDataRow
Creates a Persistent from DataRow.- Since:
- 3.1
- See Also:
-
objectFromDataRow
Creates a Persistent from DataRow. This variety of the 'objectFromDataRow' method is normally used for generic classes.- Since:
- 3.1, 5.0 returns
Persistentinstead of the deprecated DataObject - See Also:
-
newObject
Description copied from interface:ObjectContextCreates a new persistent object of a given entity scheduled to be inserted to the database on next commit. The object class is determined from the mapped entity. In most casesObjectContext.newObject(Class)should be used instead, but this method is helpful when generic persistent classes are used.- Specified by:
newObjectin interfaceObjectContext
-
registerNewObject
Registers a transient object with the context, recursively registering all transient persistent objects attached to this object via relationships.- Specified by:
registerNewObjectin interfaceObjectContext- Parameters:
object- new object that needs to be made persistent.
-
unregisterObjects
Unregisters a Collection of Persistent objects from the DataContext and the underlying ObjectStore. This operation also unsets DataContext for each object and changes its state toPersistenceState.TRANSIENT- See Also:
-
invalidateObjects
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
-
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
-
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
-
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:
rollbackChangesLocallyin interfaceObjectContext- 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:
rollbackChangesin interfaceObjectContext
-
commitChangesToParent
public void commitChangesToParent()"Flushes" the changes to the parentDataChannel. 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 usecommitChanges().- Specified by:
commitChangesToParentin interfaceObjectContext- Since:
- 1.2
- See Also:
-
commitChanges
Synchronizes object graph with the database. Executes needed insert, update and delete queries (generated internally).- Specified by:
commitChangesin interfaceObjectContext- Throws:
CayenneRuntimeException
-
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
Performs a single database select query returning result as aResultIterator.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 openResultIteratoris 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.- Specified by:
iteratorin interfaceObjectContext
-
execute
Executes a query returning all of its result sets, update counts, iterators and OUT parameters in the order they were produced.- Specified by:
executein interfaceObjectContext- Since:
- 5.0
-
performNonSelectingQuery
Deprecated, for removal: This API element is subject to removal in a future version.useexecute(Query), orSQLExec.update(ObjectContext)/MappedExec.update(ObjectContext)for a query that produces a single update countPerforms a single database query that does not select rows. Returns an array of update counts.- Since:
- 1.1
-
performNonSelectingQuery
Deprecated, for removal: This API element is subject to removal in a future version.useMappedExec.query(queryName).update(context)Performs a named mapped query that does not select rows. Returns an array of update counts.- Since:
- 1.1
-
performNonSelectingQuery
@Deprecated(since="5.0", forRemoval=true) public int[] performNonSelectingQuery(String queryName, Map<String, ?> parameters) Deprecated, for removal: This API element is subject to removal in a future version.useMappedExec.query(queryName).params(parameters).update(context)Performs a named mapped non-selecting query using a map of parameters. Returns an array of update counts.- Since:
- 1.1
-
performQuery
@Deprecated(since="5.0", forRemoval=true) public List<?> performQuery(String queryName, boolean expireCachedLists) Deprecated, for removal: This API element is subject to removal in a future version.useMappedSelect.query(queryName).select(context), callingforceNoCache()on the query to expire cached listsReturns 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
@Deprecated(since="5.0", forRemoval=true) public List<?> performQuery(String queryName, Map<String, ?> parameters, boolean expireCachedLists) Deprecated, for removal: This API element is subject to removal in a future version.useMappedSelect.query(queryName).params(parameters).select(context), callingforceNoCache()on the query to expire cached listsReturns 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
-
fireDataChannelChanged
-
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
-
isValidatingObjectsOnCommit
public boolean isValidatingObjectsOnCommit()Returns whether this ObjectContext performs object validation before commit is executed. The flag is set viaDataContext.Builder.validatingObjectsOnCommit(boolean).- Since:
- 1.1
-
getUserProperties
-
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
-
removeUserProperty
Removes a user-defined property.- Specified by:
removeUserPropertyin interfaceObjectContext- Since:
- 5.0
-
clearUserProperties
public void clearUserProperties()Removes all user-defined properties.- Specified by:
clearUserPropertiesin interfaceObjectContext- Since:
- 5.0
-