public class

StatefulPersistenceContext

extends Object
implements PersistenceContext
java.lang.Object
   ↳ org.hibernate.engine.StatefulPersistenceContext

Class Overview

A PersistenceContext represents the state of persistent "stuff" which Hibernate is tracking. This includes persistent entities, collections, as well as proxies generated.

There is meant to be a one-to-one correspondence between a SessionImpl and a PersistentContext. The SessionImpl uses the PersistentContext to track the current state of its context. Event-listeners then use the PersistentContext to drive their processing.

Summary

Fields
public static final Object NO_ROW
Public Constructors
StatefulPersistenceContext(SessionImplementor session)
Constructs a PersistentContext, bound to the given session.
Public Methods
void addChildParent(Object child, Object parent)
Put child/parent relation to cache for cascading op
void addCollectionHolder(PersistentCollection holder)
Register a PersistentCollection object for an array.
void addEntity(EntityKey key, Object entity)
Add a canonical mapping from entity key to entity instance
EntityEntry addEntity(Object entity, Status status, Object[] loadedState, EntityKey entityKey, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement, boolean lazyPropertiesAreUnfetched)
Adds an entity to the internal caches.
void addEntity(EntityUniqueKey euk, Object entity)
Add an entity to the cache by unique key
EntityEntry addEntry(Object entity, Status status, Object[] loadedState, Object rowId, Serializable id, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement, boolean lazyPropertiesAreUnfetched)
Generates an appropriate EntityEntry instance and adds it to the event source's internal caches.
CollectionEntry addInitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id)
add a collection we just pulled out of the cache (does not need initializing)
void addInitializedDetachedCollection(CollectionPersister collectionPersister, PersistentCollection collection)
add an (initialized) collection that was created by another session and passed into update() (ie.
void addNewCollection(CollectionPersister persister, PersistentCollection collection)
Add a new collection (ie.
void addNonLazyCollection(PersistentCollection collection)
Register a collection for non-lazy loading at the end of the two-phase load
void addNullProperty(EntityKey ownerKey, String propertyName)
Record the fact that the association belonging to the keyed entity is null.
void addProxy(EntityKey key, Object proxy)
Add a proxy to the session cache
void addUninitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id)
add a collection we just loaded up (still needs initializing)
void addUninitializedDetachedCollection(CollectionPersister persister, PersistentCollection collection)
add a detached uninitialized collection
void addUnownedCollection(CollectionKey key, PersistentCollection collection)
Add a collection which has no owner loaded
void afterLoad()
Call this after finishing a two-phase load
void afterTransactionCompletion()
Called after transactions end
void beforeLoad()
Call this before begining a two-phase load
void checkUniqueness(EntityKey key, Object object)
Attempts to check whether the given key represents an entity already loaded within the current session.
void clear()
Clear the state of the persistence context
boolean containsCollection(PersistentCollection collection)
Is the given collection associated with this persistence context?
boolean containsEntity(EntityKey key)
Is there an entity with the given key in the persistence context
boolean containsProxy(Object entity)
Is the given proxy associated with this persistence context?
int decrementCascadeLevel()
Called after cascading
static StatefulPersistenceContext deserialize(ObjectInputStream ois, SessionImplementor session)
BatchFetchQueue getBatchFetchQueue()
Get the BatchFetchQueue, instantiating one if necessary.
Object[] getCachedDatabaseSnapshot(EntityKey key)
Retrieve the cached database snapshot for the requested entity key.
int getCascadeLevel()
Do we already know that the entity does not exist in the database?
PersistentCollection getCollection(CollectionKey collectionKey)
Get the collection instance associated with the CollectionKey
Map getCollectionEntries()
Get the mapping from collection instance to collection entry
CollectionEntry getCollectionEntry(PersistentCollection coll)
Get the collection entry for a persistent collection
CollectionEntry getCollectionEntryOrNull(Object collection)
Get the collection entry for a collection passed to filter, which might be a collection wrapper, an array, or an unwrapped collection.
PersistentCollection getCollectionHolder(Object array)
Get the PersistentCollection object for an array
Object getCollectionOwner(Serializable key, CollectionPersister collectionPersister)
Get the entity that owns this persistent collection
Map getCollectionsByKey()
Get the mapping from collection key to collection instance
Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister)
Get the current state of the entity as known to the underlying database, or null if there is no corresponding row
Map getEntitiesByKey()
Get the mapping from key value to entity instance
Object getEntity(EntityUniqueKey euk)
Get an entity cached by unique key
Object getEntity(EntityKey key)
Get the entity instance associated with the given EntityKey
Map getEntityEntries()
Get the mapping from entity instance to entity entry
EntityEntry getEntry(Object entity)
Retreive the EntityEntry representation of the given entity.
Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap)
Search the persistence context for an index of the child object, given a collection role
LoadContexts getLoadContexts()
Retrieve this persistence context's managed load context.
Serializable getLoadedCollectionOwnerIdOrNull(PersistentCollection collection)
Get the ID for the entity that owned this persistent collection when it was loaded
Object getLoadedCollectionOwnerOrNull(PersistentCollection collection)
Get the entity that owned this persistent collection when it was loaded
Object[] getNaturalIdSnapshot(Serializable id, EntityPersister persister)
Get the values of the natural id fields as known to the underlying database, or null if the entity has no natural id or there is no corresponding row.
HashSet getNullifiableEntityKeys()
Retrieve the set of EntityKeys representing nullifiable references
Serializable getOwnerId(String entityName, String propertyName, Object childEntity, Map mergeMap)
Search this persistence context for an associated entity instance which is considered the "owner" of the given childEntity, and return that owner's id value.
Map getProxiesByKey()
Object getProxy(EntityKey key)
Get an existing proxy by key
SessionImplementor getSession()
Get the session to which this persistence context is bound.
Serializable getSnapshot(PersistentCollection coll)
Get the snapshot of the pre-flush collection state
boolean hasNonReadOnlyEntities()
int incrementCascadeLevel()
Called before cascading
void initializeNonLazyCollections()
Force initialization of all non-lazy collections encountered during the current two-phase load (actually, this is a no-op, unless this is the "outermost" load)
boolean isDefaultReadOnly()
Will entities and proxies that are loaded into this persistence context be made read-only by default? To determine the read-only/modifiable setting for a particular entity or proxy:
boolean isEntryFor(Object entity)
Is there an EntityEntry for this instance?
boolean isFlushing()
Is a flush cycle currently in process?
boolean isLoadFinished()
Is in a two-phase load?
boolean isPropertyNull(EntityKey ownerKey, String propertyName)
Is the association property belonging to the keyed entity null?
boolean isReadOnly(Object entityOrProxy)
Is the entity or proxy read-only? To get the default read-only/modifiable setting used for entities and proxies that are loaded into the session:
boolean isStateless()
Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object)
If the existing proxy is insufficiently "narrow" (derived), instantiate a new proxy and overwrite the registration of the old one.
Object proxyFor(Object impl)
Return the existing proxy associated with the given EntityKey, or the argument (the entity associated with the key) if no proxy exists.
Object proxyFor(EntityPersister persister, EntityKey key, Object impl)
Return the existing proxy associated with the given EntityKey, or the third argument (the entity associated with the key) if no proxy exists.
boolean reassociateIfUninitializedProxy(Object value)
Takes the given object and, if it represents a proxy, reassociates it with this event source.
void reassociateProxy(Object value, Serializable id)
If a deleted entity instance is re-saved, and it has a proxy, we need to reset the identifier of the proxy
void registerInsertedKey(EntityPersister persister, Serializable id)
Register keys inserted during the current transaction
void removeChildParent(Object child)
Remove child/parent relation from cache
PersistentCollection removeCollectionHolder(Object array)
Remove the mapping of collection to holder during eviction of the owning entity
Object removeEntity(EntityKey key)
Remove an entity from the session cache, also clear up other state associated with the entity, all except for the EntityEntry
EntityEntry removeEntry(Object entity)
Remove an entity entry from the session cache
Object removeProxy(EntityKey key)
Remove a proxy from the session cache.
void replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Serializable generatedId)
void serialize(ObjectOutputStream oos)
Used by the owning session to explicitly control serialization of the persistence context.
void setDefaultReadOnly(boolean defaultReadOnly)
Change the default for entities and proxies loaded into this persistence context from modifiable to read-only mode, or from modifiable to read-only mode.
void setEntryStatus(EntityEntry entry, Status status)
Set the status of an entry
void setFlushing(boolean flushing)
Called before and after the flushcycle
void setReadOnly(Object object, boolean readOnly)
Set an unmodified persistent object to read-only mode, or a read-only object to modifiable mode.
String toString()
Returns a string representation of the object.
Object unproxy(Object maybeProxy)
Get the entity instance underlying the given proxy, throwing an exception if the proxy is uninitialized.
Object unproxyAndReassociate(Object maybeProxy)
Possibly unproxy the given reference and reassociate it with the current session.
PersistentCollection useUnownedCollection(CollectionKey key)
Get and remove a collection whose owner is not yet loaded, when its owner is being loaded
boolean wasInsertedDuringTransaction(EntityPersister persister, Serializable id)
Allows callers to check to see if the identified entity was inserted during the current transaction.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.hibernate.engine.PersistenceContext

Fields

public static final Object NO_ROW

Public Constructors

public StatefulPersistenceContext (SessionImplementor session)

Constructs a PersistentContext, bound to the given session.

Parameters
session The session "owning" this context.

Public Methods

public void addChildParent (Object child, Object parent)

Put child/parent relation to cache for cascading op

public void addCollectionHolder (PersistentCollection holder)

Register a PersistentCollection object for an array. Associates a holder with an array - MUST be called after loading array, since the array instance is not created until endLoad().

public void addEntity (EntityKey key, Object entity)

Add a canonical mapping from entity key to entity instance

public EntityEntry addEntity (Object entity, Status status, Object[] loadedState, EntityKey entityKey, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement, boolean lazyPropertiesAreUnfetched)

Adds an entity to the internal caches.

public void addEntity (EntityUniqueKey euk, Object entity)

Add an entity to the cache by unique key

public EntityEntry addEntry (Object entity, Status status, Object[] loadedState, Object rowId, Serializable id, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement, boolean lazyPropertiesAreUnfetched)

Generates an appropriate EntityEntry instance and adds it to the event source's internal caches.

public CollectionEntry addInitializedCollection (CollectionPersister persister, PersistentCollection collection, Serializable id)

add a collection we just pulled out of the cache (does not need initializing)

public void addInitializedDetachedCollection (CollectionPersister collectionPersister, PersistentCollection collection)

add an (initialized) collection that was created by another session and passed into update() (ie. one with a snapshot and existing state on the database)

public void addNewCollection (CollectionPersister persister, PersistentCollection collection)

Add a new collection (ie. a newly created one, just instantiated by the application, with no database state or snapshot)

Parameters
collection The collection to be associated with the persistence context

public void addNonLazyCollection (PersistentCollection collection)

Register a collection for non-lazy loading at the end of the two-phase load

public void addNullProperty (EntityKey ownerKey, String propertyName)

Record the fact that the association belonging to the keyed entity is null.

public void addProxy (EntityKey key, Object proxy)

Add a proxy to the session cache

public void addUninitializedCollection (CollectionPersister persister, PersistentCollection collection, Serializable id)

add a collection we just loaded up (still needs initializing)

public void addUninitializedDetachedCollection (CollectionPersister persister, PersistentCollection collection)

add a detached uninitialized collection

public void addUnownedCollection (CollectionKey key, PersistentCollection collection)

Add a collection which has no owner loaded

public void afterLoad ()

Call this after finishing a two-phase load

public void afterTransactionCompletion ()

Called after transactions end

public void beforeLoad ()

Call this before begining a two-phase load

public void checkUniqueness (EntityKey key, Object object)

Attempts to check whether the given key represents an entity already loaded within the current session.

Parameters
object The entity reference against which to perform the uniqueness check.

public void clear ()

Clear the state of the persistence context

public boolean containsCollection (PersistentCollection collection)

Is the given collection associated with this persistence context?

public boolean containsEntity (EntityKey key)

Is there an entity with the given key in the persistence context

public boolean containsProxy (Object entity)

Is the given proxy associated with this persistence context?

public int decrementCascadeLevel ()

Called after cascading

public BatchFetchQueue getBatchFetchQueue ()

Get the BatchFetchQueue, instantiating one if necessary.

public Object[] getCachedDatabaseSnapshot (EntityKey key)

Retrieve the cached database snapshot for the requested entity key.

This differs from getDatabaseSnapshot(Serializable, EntityPersister) is two important respects:

  1. no snapshot is obtained from the database if not already cached
  2. an entry of NO_ROW here is interpretet as an exception

Parameters
key The entity key for which to retrieve the cached snapshot
Returns
  • The cached snapshot
Throws
IllegalStateException if the cached snapshot was == NO_ROW.

public int getCascadeLevel ()

Do we already know that the entity does not exist in the database?

public PersistentCollection getCollection (CollectionKey collectionKey)

Get the collection instance associated with the CollectionKey

public Map getCollectionEntries ()

Get the mapping from collection instance to collection entry

public CollectionEntry getCollectionEntry (PersistentCollection coll)

Get the collection entry for a persistent collection

public CollectionEntry getCollectionEntryOrNull (Object collection)

Get the collection entry for a collection passed to filter, which might be a collection wrapper, an array, or an unwrapped collection. Return null if there is no entry.

public PersistentCollection getCollectionHolder (Object array)

Get the PersistentCollection object for an array

public Object getCollectionOwner (Serializable key, CollectionPersister collectionPersister)

Get the entity that owns this persistent collection

public Map getCollectionsByKey ()

Get the mapping from collection key to collection instance

public Object[] getDatabaseSnapshot (Serializable id, EntityPersister persister)

Get the current state of the entity as known to the underlying database, or null if there is no corresponding row

public Map getEntitiesByKey ()

Get the mapping from key value to entity instance

public Object getEntity (EntityUniqueKey euk)

Get an entity cached by unique key

public Object getEntity (EntityKey key)

Get the entity instance associated with the given EntityKey

public Map getEntityEntries ()

Get the mapping from entity instance to entity entry

public EntityEntry getEntry (Object entity)

Retreive the EntityEntry representation of the given entity.

Parameters
entity The entity for which to locate the EntityEntry.
Returns
  • The EntityEntry for the given entity.

public Object getIndexInOwner (String entity, String property, Object childEntity, Map mergeMap)

Search the persistence context for an index of the child object, given a collection role

public LoadContexts getLoadContexts ()

Retrieve this persistence context's managed load context.

Returns
  • The load context

public Serializable getLoadedCollectionOwnerIdOrNull (PersistentCollection collection)

Get the ID for the entity that owned this persistent collection when it was loaded

Parameters
collection The persistent collection
Returns
  • the owner ID if available from the collection's loaded key; otherwise, returns null

public Object getLoadedCollectionOwnerOrNull (PersistentCollection collection)

Get the entity that owned this persistent collection when it was loaded

Parameters
collection The persistent collection
Returns
  • the owner, if its entity ID is available from the collection's loaded key and the owner entity is in the persistence context; otherwise, returns null

public Object[] getNaturalIdSnapshot (Serializable id, EntityPersister persister)

Get the values of the natural id fields as known to the underlying database, or null if the entity has no natural id or there is no corresponding row.

public HashSet getNullifiableEntityKeys ()

Retrieve the set of EntityKeys representing nullifiable references

public Serializable getOwnerId (String entityName, String propertyName, Object childEntity, Map mergeMap)

Search this persistence context for an associated entity instance which is considered the "owner" of the given childEntity, and return that owner's id value. This is performed in the scenario of a uni-directional, non-inverse one-to-many collection (which means that the collection elements do not maintain a direct reference to the owner).

As such, the processing here is basically to loop over every entity currently associated with this persistence context and for those of the correct entity (sub) type to extract its collection role property value and see if the child is contained within that collection. If so, we have found the owner; if not, we go on.

Also need to account for mergeMap which acts as a local copy cache managed for the duration of a merge operation. It represents a map of the detached entity instances pointing to the corresponding managed instance.

Parameters
entityName The entity name for the entity type which would own the child
propertyName The name of the property on the owning entity type which would name this child association.
childEntity The child entity instance for which to locate the owner instance id.
mergeMap A map of non-persistent instances from an on-going merge operation (possibly null).
Returns
  • The id of the entityName instance which is said to own the child; null if an appropriate owner not located.

public Map getProxiesByKey ()

public Object getProxy (EntityKey key)

Get an existing proxy by key

public SessionImplementor getSession ()

Get the session to which this persistence context is bound.

Returns
  • The session.

public Serializable getSnapshot (PersistentCollection coll)

Get the snapshot of the pre-flush collection state

public boolean hasNonReadOnlyEntities ()

public int incrementCascadeLevel ()

Called before cascading

public void initializeNonLazyCollections ()

Force initialization of all non-lazy collections encountered during the current two-phase load (actually, this is a no-op, unless this is the "outermost" load)

public boolean isDefaultReadOnly ()

Will entities and proxies that are loaded into this persistence context be made read-only by default? To determine the read-only/modifiable setting for a particular entity or proxy:

Returns
  • true, loaded entities/proxies will be made read-only by default; false, loaded entities/proxies will be made modifiable by default.

public boolean isEntryFor (Object entity)

Is there an EntityEntry for this instance?

public boolean isFlushing ()

Is a flush cycle currently in process?

public boolean isLoadFinished ()

Is in a two-phase load?

public boolean isPropertyNull (EntityKey ownerKey, String propertyName)

Is the association property belonging to the keyed entity null?

public boolean isReadOnly (Object entityOrProxy)

Is the entity or proxy read-only? To get the default read-only/modifiable setting used for entities and proxies that are loaded into the session:

Returns
  • true, the object is read-only; false, the object is modifiable.

public boolean isStateless ()

public Object narrowProxy (Object proxy, EntityPersister persister, EntityKey key, Object object)

If the existing proxy is insufficiently "narrow" (derived), instantiate a new proxy and overwrite the registration of the old one. This breaks == and occurs only for "class" proxies rather than "interface" proxies. Also init the proxy to point to the given target implementation if necessary.

Parameters
proxy The proxy instance to be narrowed.
persister The persister for the proxied entity.
key The internal cache key for the proxied entity.
object (optional) the actual proxied entity instance.
Returns
  • An appropriately narrowed instance.

public Object proxyFor (Object impl)

Return the existing proxy associated with the given EntityKey, or the argument (the entity associated with the key) if no proxy exists. (slower than the form above)

public Object proxyFor (EntityPersister persister, EntityKey key, Object impl)

Return the existing proxy associated with the given EntityKey, or the third argument (the entity associated with the key) if no proxy exists. Init the proxy to the target implementation, if necessary.

public boolean reassociateIfUninitializedProxy (Object value)

Takes the given object and, if it represents a proxy, reassociates it with this event source.

Parameters
value The possible proxy to be reassociated.
Returns
  • Whether the passed value represented an actual proxy which got initialized.

public void reassociateProxy (Object value, Serializable id)

If a deleted entity instance is re-saved, and it has a proxy, we need to reset the identifier of the proxy

public void registerInsertedKey (EntityPersister persister, Serializable id)

Register keys inserted during the current transaction

Parameters
persister The entity persister
id The id

public void removeChildParent (Object child)

Remove child/parent relation from cache

public PersistentCollection removeCollectionHolder (Object array)

Remove the mapping of collection to holder during eviction of the owning entity

public Object removeEntity (EntityKey key)

Remove an entity from the session cache, also clear up other state associated with the entity, all except for the EntityEntry

public EntityEntry removeEntry (Object entity)

Remove an entity entry from the session cache

public Object removeProxy (EntityKey key)

Remove a proxy from the session cache.

Additionally, ensure that any load optimization references such as batch or subselect loading get cleaned up as well.

Parameters
key The key of the entity proxy to be removed
Returns
  • The proxy reference.

public void replaceDelayedEntityIdentityInsertKeys (EntityKey oldKey, Serializable generatedId)

public void serialize (ObjectOutputStream oos)

Used by the owning session to explicitly control serialization of the persistence context.

Parameters
oos The stream to which the persistence context should get written
Throws
IOException serialization errors.

public void setDefaultReadOnly (boolean defaultReadOnly)

Change the default for entities and proxies loaded into this persistence context from modifiable to read-only mode, or from modifiable to read-only mode. Read-only entities are not dirty-checked and snapshots of persistent state are not maintained. Read-only entities can be modified, but changes are not persisted. When a proxy is initialized, the loaded entity will have the same read-only/modifiable setting as the uninitialized proxy has, regardless of the persistence context's current setting. To change the read-only/modifiable setting for a particular entity or proxy that is already in this session: + * @see PersistenceContext#setReadOnly(Object,boolean)

Parameters
defaultReadOnly true, the default for loaded entities/proxies is read-only; false, the default for loaded entities/proxies is modifiable

public void setEntryStatus (EntityEntry entry, Status status)

Set the status of an entry

public void setFlushing (boolean flushing)

Called before and after the flushcycle

public void setReadOnly (Object object, boolean readOnly)

Set an unmodified persistent object to read-only mode, or a read-only object to modifiable mode. Read-only entities are not dirty-checked and snapshots of persistent state are not maintained. Read-only entities can be modified, but changes are not persisted. When a proxy is initialized, the loaded entity will have the same read-only/modifiable setting as the uninitialized proxy has, regardless of the session's current setting. If the entity or proxy already has the specified read-only/modifiable setting, then this method does nothing. To set the default read-only/modifiable setting used for entities and proxies that are loaded into this persistence context:

Parameters
readOnly if true, the entity or proxy is made read-only; if false, the entity or proxy is made modifiable.

public String toString ()

Returns a string representation of the object.

Returns
  • a string representation of the object.

public Object unproxy (Object maybeProxy)

Get the entity instance underlying the given proxy, throwing an exception if the proxy is uninitialized. If the given object is not a proxy, simply return the argument.

public Object unproxyAndReassociate (Object maybeProxy)

Possibly unproxy the given reference and reassociate it with the current session.

Parameters
maybeProxy The reference to be unproxied if it currently represents a proxy.
Returns
  • The unproxied instance.

public PersistentCollection useUnownedCollection (CollectionKey key)

Get and remove a collection whose owner is not yet loaded, when its owner is being loaded

public boolean wasInsertedDuringTransaction (EntityPersister persister, Serializable id)

Allows callers to check to see if the identified entity was inserted during the current transaction.

Parameters
persister The entity persister
id The id
Returns
  • True if inserted during this transaction, false otherwise.