org.apache.cayenne

## Class CayenneContext

- java.lang.Object
- - org.apache.cayenne.BaseContext
  - - org.apache.cayenne.CayenneContext

- All Implemented Interfaces:
    Serializable, DataChannel, ObjectContext

      public class CayenneContext
      extends BaseContext

  A default generic implementation of ObjectContext suitable for accessing Cayenne from either an ORM or a client tiers. Communicates with Cayenne via a `DataChannel`.
  Since:
    1.2
  See Also:
    Serialized Form

- - ### Field Summary

    - ### Fields inherited from class org.apache.cayenne.BaseContext

      `channel, entityResolver, graphAction, queryCache, threadObjectContext, userProperties, validatingObjectsOnCommit`

    - ### 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

    Constructors
    Constructor and Description
    `CayenneContext()` Creates a new CayenneContext with no channel and disabled graph events.
    `CayenneContext(DataChannel channel)` Creates a new CayenneContext, initializing it with a channel instance.
    `CayenneContext(DataChannel channel, boolean changeEventsEnabled, boolean lifecyleEventsEnabled)` Creates a new CayenneContext, initializing it with a channel.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `attachToChannel(DataChannel channel)` Attaches to a provided DataChannel.
    `void` | `commitChanges()` Commits changes to uncommitted objects.
    `void` | `commitChangesToParent()` Flushes all changes to objects in this context to the parent DataChannel.
    `ObjectContext` | `createChildContext()` Deprecated. Since 3.1 replaced by `CayenneRuntime.getContext(DataChannel)`
    `Collection<?>` | `deletedObjects()` Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.DELETED
    `GraphManager` | `getGraphManager()` Returns GraphManager that manages object graph associated with this context.
    `boolean` | `hasChanges()` Returns `true` if there are any modified, deleted or new objects registered with this CayenneContext, `false` otherwise.
    `boolean` | `isChangeEventsEnabled()` Returns true if this context posts individual object modification events.
    `boolean` | `isLifecycleEventsEnabled()` Returns true if this context posts lifecycle events.
    `Collection<?>` | `modifiedObjects()` Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.MODIFIED
    `<T> T` | `newObject(Class<T> persistentClass)` Creates and registers a new Persistent object instance.
    `Collection<?>` | `newObjects()` Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.NEW
    `protected GraphDiff` | `onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade)`
    `QueryResponse` | `onQuery(ObjectContext context, Query query)` Executes a query, using provided context to register persistent objects if query returns any objects.
    `QueryResponse` | `performGenericQuery(Query query)` Executes any kind of query providing the result in a form of QueryResponse.
    `List` | `performQuery(Query query)` Runs a query, returning result as list.
    `void` | `registerNewObject(Object object)` Registers a transient object with the context.
    `void` | `rollbackChanges()` Resets all uncommitted changes made to the objects in this ObjectContext, cascading rollback operation all the way through the stack.
    `void` | `rollbackChangesLocally()` Resets all uncommitted changes made to the objects in this ObjectContext.
    `Collection<?>` | `uncommittedObjects()` Returns a collection of MODIFIED, DELETED or NEW objects.

    - ### Methods inherited from class org.apache.cayenne.BaseContext

      `attachToRuntime, attachToRuntimeIfNeeded, bindThreadObjectContext, deleteObject, deleteObjects, deleteObjects, fireDataChannelChanged, fireDataChannelCommitted, fireDataChannelRolledback, getChannel, getEntityResolver, getEventManager, getQueryCache, getThreadObjectContext, getUserProperties, getUserProperty, injectInitialValue, invalidateObjects, invalidateObjects, isValidatingObjectsOnCommit, localObject, localObject, onSync, prepareForAccess, propertyChanged, setChannel, setEntityResolver, setQueryCache, setUserProperty, setValidatingObjectsOnCommit`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CayenneContext

          public CayenneContext()

      Creates a new CayenneContext with no channel and disabled graph events.

    - #### CayenneContext

          public CayenneContext(DataChannel channel)

      Creates a new CayenneContext, initializing it with a channel instance. CayenneContext created using this constructor WILL NOT broadcast graph change events.

    - #### CayenneContext

          public CayenneContext(DataChannel channel,
                                boolean changeEventsEnabled,
                                boolean lifecyleEventsEnabled)

      Creates a new CayenneContext, initializing it with a channel.

  - ### Method Detail

    - #### attachToChannel

          protected void attachToChannel(DataChannel channel)

      Description copied from class: `BaseContext`
      Attaches to a provided DataChannel.
      Overrides:
        `attachToChannel` in class `BaseContext`
      Since:
        3.1

    - #### isChangeEventsEnabled

          public boolean isChangeEventsEnabled()

      Returns true if this context posts individual object modification events. Subject used for these events is `ObjectContext.GRAPH_CHANGED_SUBJECT`.

    - #### isLifecycleEventsEnabled

          public boolean isLifecycleEventsEnabled()

      Returns true if this context posts lifecycle events. Subjects used for these events are `ObjectContext.GRAPH_COMMIT_STARTED_SUBJECT, ObjectContext.GRAPH_COMMITTED_SUBJECT, ObjectContext.GRAPH_COMMIT_ABORTED_SUBJECT, ObjectContext.GRAPH_ROLLEDBACK_SUBJECT.` .

    - #### getGraphManager

          public GraphManager getGraphManager()

      Description copied from interface: `ObjectContext`
      Returns GraphManager that manages object graph associated with this context.
      Specified by:
        `getGraphManager` in interface `ObjectContext`
      Specified by:
        `getGraphManager` in class `BaseContext`

    - #### commitChanges

          public void commitChanges()

      Commits changes to uncommitted objects. First checks if there are changes in this context and if any changes are detected, sends a commit message to remote Cayenne service via an internal instance of CayenneConnector.
      Specified by:
        `commitChanges` in interface `ObjectContext`
      Specified by:
        `commitChanges` in class `BaseContext`

    - #### commitChangesToParent

          public void commitChangesToParent()

      Description copied from interface: `ObjectContext`
      Flushes all changes to objects in this context to the parent DataChannel. Same as `ObjectContext.commitChanges()`, but no cascading flush occurs.
      Specified by:
        `commitChangesToParent` in interface `ObjectContext`
      Specified by:
        `commitChangesToParent` in class `BaseContext`

    - #### rollbackChanges

          public void rollbackChanges()

      Description copied from interface: `ObjectContext`
      Resets all uncommitted changes made to the objects in this ObjectContext, cascading rollback operation all the way through the stack.
      Specified by:
        `rollbackChanges` in interface `ObjectContext`
      Specified by:
        `rollbackChanges` in class `BaseContext`

    - #### rollbackChangesLocally

          public void rollbackChangesLocally()

      Description copied from interface: `ObjectContext`
      Resets all uncommitted changes made to the objects in this ObjectContext. Same as `ObjectContext.rollbackChanges()()`, but rollback is local to this context and no cascading changes undoing occurs.
      Specified by:
        `rollbackChangesLocally` in interface `ObjectContext`
      Specified by:
        `rollbackChangesLocally` in class `BaseContext`

    - #### newObject

          public <T> T newObject(Class<T> persistentClass)

      Creates and registers a new Persistent object instance.
      Specified by:
        `newObject` in interface `ObjectContext`
      Specified by:
        `newObject` in class `BaseContext`

    - #### registerNewObject

          public void registerNewObject(Object object)

      Description copied from interface: `ObjectContext`
      Registers a transient object with the context. The difference with `ObjectContext.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:
        `registerNewObject` in interface `ObjectContext`
      Specified by:
        `registerNewObject` in class `BaseContext`
      Parameters:
        `object` - new object that needs to be made persistent.
      Since:
        3.0

    - #### performQuery

          public List performQuery(Query query)

      Runs a query, returning result as list.
      Specified by:
        `performQuery` in interface `ObjectContext`
      Specified by:
        `performQuery` in class `BaseContext`

    - #### performGenericQuery

          public QueryResponse performGenericQuery(Query query)

      Description copied from interface: `ObjectContext`
      Executes any kind of query providing the result in a form of QueryResponse.
      Specified by:
        `performGenericQuery` in interface `ObjectContext`
      Specified by:
        `performGenericQuery` in class `BaseContext`

    - #### onQuery

          public QueryResponse onQuery(ObjectContext context,
                                       Query query)

      Description copied from interface: `DataChannel`
      Executes a query, using provided context to register persistent objects if query returns any objects.
      Parameters:
        `context` - an ObjectContext that originated the query, used to register result objects.
      Returns:
        a generic response object that encapsulates result of the execution.

    - #### uncommittedObjects

          public Collection<?> uncommittedObjects()

      Description copied from interface: `ObjectContext`
      Returns a collection of MODIFIED, DELETED or NEW objects.
      Specified by:
        `uncommittedObjects` in interface `ObjectContext`
      Specified by:
        `uncommittedObjects` in class `BaseContext`

    - #### deletedObjects

          public Collection<?> deletedObjects()

      Description copied from interface: `ObjectContext`
      Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.DELETED
      Specified by:
        `deletedObjects` in interface `ObjectContext`
      Specified by:
        `deletedObjects` in class `BaseContext`

    - #### modifiedObjects

          public Collection<?> modifiedObjects()

      Description copied from interface: `ObjectContext`
      Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.MODIFIED
      Specified by:
        `modifiedObjects` in interface `ObjectContext`
      Specified by:
        `modifiedObjects` in class `BaseContext`

    - #### newObjects

          public Collection<?> newObjects()

      Description copied from interface: `ObjectContext`
      Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.NEW
      Specified by:
        `newObjects` in interface `ObjectContext`
      Specified by:
        `newObjects` in class `BaseContext`

    - #### createChildContext

          @Deprecated
          public ObjectContext createChildContext()

      Deprecated. Since 3.1 replaced by `CayenneRuntime.getContext(DataChannel)`
      Creates and returns a new child ObjectContext.
      Since:
        3.0

    - #### onContextFlush

          protected GraphDiff onContextFlush(ObjectContext originatingContext,
                                             GraphDiff changes,
                                             boolean cascade)

      Specified by:
        `onContextFlush` in class `BaseContext`

    - #### hasChanges

          public boolean hasChanges()

      Returns `true` if there are any modified, deleted or new objects registered with this CayenneContext, `false` otherwise.

Copyright © 2001–2017 Apache Cayenne. All rights reserved.