org.hibernate.engine
Class PersistenceContext

java.lang.Object
  extended byorg.hibernate.engine.PersistenceContext
All Implemented Interfaces:
Serializable

public class PersistenceContext
extends Object
implements Serializable

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.

Author:
Steve Ebersole
See Also:
Serialized Form

Field Summary
static Object NO_ROW
           
 
Constructor Summary
PersistenceContext(SessionImplementor session)
          Constructs a PersistentContext, bound to the given session.
 
Method Summary
 void addCollectionHolder(PersistentCollection holder)
          Register a PersistentCollection object for an array.
 void addEntity(EntityKey key, Object object)
           
 void addEntity(EntityUniqueKey euk, Object entity)
          Add an entity to the cache by unique key
 EntityEntry addEntity(Object object, Status status, Object[] loadedState, Serializable id, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement)
          Adds an entity to the internal caches.
 EntityEntry addEntry(Object object, Status status, Object[] loadedState, Object rowId, Serializable id, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement)
          Generates an appropriate EntityEntry instance and adds it to the event source's internal caches.
 CollectionEntry addInitializedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id, EntityMode em)
          add a collection we just pulled out of the cache (does not need initializing)
 void addInitializedDetachedCollection(PersistentCollection collection, CollectionSnapshot cs, EntityMode em)
          add an (initialized) collection that was created by another session and passed into update() (ie.
 void addNewCollection(PersistentCollection collection, CollectionPersister persister)
          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(PersistentCollection collection, CollectionPersister persister, Serializable id, EntityMode em)
          add a collection we just loaded up (still needs initializing)
 void addUninitializedDetachedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id, EntityMode em)
          add a detached uninitialized collection
 void addUnownedCollection(CollectionKey key, PersistentCollection collection)
           
 void afterLoad()
          Call this after finishing a two-phase load
 void afterTransactionCompletion()
           
 void beforeLoad()
          Call this before begining a two-phase load
 void checkUniqueness(Serializable id, EntityPersister persister, Object object)
          Attempts to check whether the given key represents an entity already loaded within the current session.
 void clear()
           
 void clearNullProperties()
           
 boolean containsEntity(EntityKey key)
           
 boolean containsProxy(Object object)
           
 int decrementCascadeLevel()
           
 BatchFetchQueue getBatchFetchQueue()
          Get the BatchFetchQueue, instantiating one if necessary.
 Object[] getCachedDatabaseSnapshot(EntityKey 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()
           
 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
 CollectionLoadContext getCollectionLoadContext()
           
 Object getCollectionOwner(Serializable key, CollectionPersister collectionPersister)
          Get the entity that owns this persistent collection
 Map getCollectionsByKey()
           
 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()
           
 Object getEntity(EntityKey key)
          Get the entity instance associated with the given EntityKey
 Object getEntity(EntityUniqueKey euk)
          Get an entity cached by unique key
 Map getEntityEntries()
           
 EntityEntry getEntry(Object object)
          Retreive the EntityEntry representation of the given entity.
 Object getIndexInOwner(String role, Object childObject)
          Search the persistence context for an index of the child object, given a collection role
 Serializable getLoadedCollectionKey(PersistentCollection coll)
          Get the pre-flush identifier of the collection
 LockMode getLockMode(Object entity)
           
 HashSet getNullifiableEntityKeys()
          Retrieve the set of EntityKeys representing nullifiable references
 Serializable getOwnerId(String role, Object childObject)
          Search the persistence context for an owner for the child object, given a collection role
 Object getProxy(EntityKey key)
          Get an existing proxy by key
 SessionImplementor getSession()
           
 Serializable getSnapshot(PersistentCollection coll)
          Get the snapshot of the pre-flush collection state
 boolean hasNonReadOnlyEntities()
           
 int incrementCascadeLevel()
           
 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 isEntryFor(Object object)
          Is there an EntityEntry for this instance?
 boolean isFlushing()
           
 boolean isInverseCollection(PersistentCollection collection)
          Is this the "inverse" end of a bidirectional association?
 boolean isPropertyNull(EntityKey ownerKey, String propertyName)
          Is the association property belonging to the keyed entity null?
 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(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.
 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.
 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
 PersistentCollection removeCollectionHolder(Object array)
           
 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 object)
          Remove an entity entry from the session cache
 Object removeProxy(EntityKey key)
          Remove a proxy from the session cache
 void setEntryStatus(EntityEntry entry, Status status)
           
 void setFlushing(boolean flushing)
           
 void setLockMode(Object entity, LockMode lockMode)
           
 void setNullifiableEntityKeys(HashSet nullifiables)
           
 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_ROW

public static final Object NO_ROW
Constructor Detail

PersistenceContext

public PersistenceContext(SessionImplementor session)
Constructs a PersistentContext, bound to the given session.

Parameters:
session - The session "owning" this context.
Method Detail

getSession

public SessionImplementor getSession()

getCollectionLoadContext

public CollectionLoadContext getCollectionLoadContext()

addUnownedCollection

public void addUnownedCollection(CollectionKey key,
                                 PersistentCollection collection)

useUnownedCollection

public PersistentCollection useUnownedCollection(CollectionKey key)

getBatchFetchQueue

public BatchFetchQueue getBatchFetchQueue()
Get the BatchFetchQueue, instantiating one if necessary.


clear

public void clear()

hasNonReadOnlyEntities

public boolean hasNonReadOnlyEntities()

setEntryStatus

public void setEntryStatus(EntityEntry entry,
                           Status status)

afterTransactionCompletion

public void afterTransactionCompletion()

getDatabaseSnapshot

public Object[] getDatabaseSnapshot(Serializable id,
                                    EntityPersister persister)
                             throws HibernateException
Get the current state of the entity as known to the underlying database, or null if there is no corresponding row

Throws:
HibernateException

getCachedDatabaseSnapshot

public Object[] getCachedDatabaseSnapshot(EntityKey key)

addEntity

public void addEntity(EntityKey key,
                      Object object)

getEntity

public Object getEntity(EntityKey key)
Get the entity instance associated with the given EntityKey


containsEntity

public boolean containsEntity(EntityKey key)

removeEntity

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


getEntity

public Object getEntity(EntityUniqueKey euk)
Get an entity cached by unique key


addEntity

public void addEntity(EntityUniqueKey euk,
                      Object entity)
Add an entity to the cache by unique key


getEntry

public EntityEntry getEntry(Object object)
Retreive the EntityEntry representation of the given entity.

Parameters:
object - The entity for which to locate the EntityEntry.
Returns:
The EntityEntry for the given entity.

removeEntry

public EntityEntry removeEntry(Object object)
Remove an entity entry from the session cache


isEntryFor

public boolean isEntryFor(Object object)
Is there an EntityEntry for this instance?


getCollectionEntry

public CollectionEntry getCollectionEntry(PersistentCollection coll)
Get the collection entry for a persistent collection


addEntity

public EntityEntry addEntity(Object object,
                             Status status,
                             Object[] loadedState,
                             Serializable id,
                             Object version,
                             LockMode lockMode,
                             boolean existsInDatabase,
                             EntityPersister persister,
                             boolean disableVersionIncrement)
Adds an entity to the internal caches.


addEntry

public EntityEntry addEntry(Object object,
                            Status status,
                            Object[] loadedState,
                            Object rowId,
                            Serializable id,
                            Object version,
                            LockMode lockMode,
                            boolean existsInDatabase,
                            EntityPersister persister,
                            boolean disableVersionIncrement)
Generates an appropriate EntityEntry instance and adds it to the event source's internal caches.


getLockMode

public LockMode getLockMode(Object entity)

setLockMode

public void setLockMode(Object entity,
                        LockMode lockMode)

containsProxy

public boolean containsProxy(Object object)

reassociateIfUninitializedProxy

public boolean reassociateIfUninitializedProxy(Object value)
                                        throws MappingException
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.
Throws:
MappingException

reassociateProxy

public void reassociateProxy(Object value,
                             Serializable id)
                      throws MappingException
If a deleted entity instance is re-saved, and it has a proxy, we need to reset the identifier of the proxy

Throws:
MappingException

unproxy

public Object unproxy(Object maybeProxy)
               throws HibernateException
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.

Throws:
HibernateException

unproxyAndReassociate

public Object unproxyAndReassociate(Object maybeProxy)
                             throws HibernateException
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.
Throws:
HibernateException

checkUniqueness

public void checkUniqueness(Serializable id,
                            EntityPersister persister,
                            Object object)
                     throws HibernateException
Attempts to check whether the given key represents an entity already loaded within the current session.

Parameters:
id - The key to be checked.
object - The entity reference against which to perform the uniqueness check.
Throws:
HibernateException

narrowProxy

public Object narrowProxy(Object proxy,
                          EntityPersister persister,
                          EntityKey key,
                          Object object)
                   throws HibernateException
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.
Throws:
HibernateException

proxyFor

public Object proxyFor(EntityPersister persister,
                       EntityKey key,
                       Object impl)
                throws HibernateException
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.

Throws:
HibernateException

proxyFor

public Object proxyFor(Object impl)
                throws HibernateException
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)

Throws:
HibernateException

getCollectionOwner

public Object getCollectionOwner(Serializable key,
                                 CollectionPersister collectionPersister)
                          throws MappingException
Get the entity that owns this persistent collection

Throws:
MappingException

addUninitializedCollection

public void addUninitializedCollection(PersistentCollection collection,
                                       CollectionPersister persister,
                                       Serializable id,
                                       EntityMode em)
add a collection we just loaded up (still needs initializing)


addUninitializedDetachedCollection

public void addUninitializedDetachedCollection(PersistentCollection collection,
                                               CollectionPersister persister,
                                               Serializable id,
                                               EntityMode em)
add a detached uninitialized collection


addNewCollection

public void addNewCollection(PersistentCollection collection,
                             CollectionPersister persister)
                      throws HibernateException
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
Throws:
HibernateException

addInitializedDetachedCollection

public void addInitializedDetachedCollection(PersistentCollection collection,
                                             CollectionSnapshot cs,
                                             EntityMode em)
                                      throws HibernateException
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)

Throws:
HibernateException

addInitializedCollection

public CollectionEntry addInitializedCollection(PersistentCollection collection,
                                                CollectionPersister persister,
                                                Serializable id,
                                                EntityMode em)
                                         throws HibernateException
add a collection we just pulled out of the cache (does not need initializing)

Throws:
HibernateException

getCollection

public PersistentCollection getCollection(CollectionKey collectionKey)
Get the collection instance associated with the CollectionKey


addNonLazyCollection

public void addNonLazyCollection(PersistentCollection collection)
Register a collection for non-lazy loading at the end of the two-phase load


initializeNonLazyCollections

public void initializeNonLazyCollections()
                                  throws HibernateException
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)

Throws:
HibernateException

getCollectionHolder

public PersistentCollection getCollectionHolder(Object array)
Get the PersistentCollection object for an array


addCollectionHolder

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().


removeCollectionHolder

public PersistentCollection removeCollectionHolder(Object array)

getSnapshot

public Serializable getSnapshot(PersistentCollection coll)
Get the snapshot of the pre-flush collection state


getLoadedCollectionKey

public Serializable getLoadedCollectionKey(PersistentCollection coll)
Get the pre-flush identifier of the collection


isInverseCollection

public boolean isInverseCollection(PersistentCollection collection)
Is this the "inverse" end of a bidirectional association?


getCollectionEntryOrNull

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.


getProxy

public Object getProxy(EntityKey key)
Get an existing proxy by key


addProxy

public void addProxy(EntityKey key,
                     Object proxy)
Add a proxy to the session cache


removeProxy

public Object removeProxy(EntityKey key)
Remove a proxy from the session cache


getNullifiableEntityKeys

public HashSet getNullifiableEntityKeys()
Retrieve the set of EntityKeys representing nullifiable references


setNullifiableEntityKeys

public void setNullifiableEntityKeys(HashSet nullifiables)

getEntitiesByKey

public Map getEntitiesByKey()

getEntityEntries

public Map getEntityEntries()

getCollectionEntries

public Map getCollectionEntries()

getCollectionsByKey

public Map getCollectionsByKey()

getCascadeLevel

public int getCascadeLevel()
Do we already know that the entity does not exist in the database?


incrementCascadeLevel

public int incrementCascadeLevel()

decrementCascadeLevel

public int decrementCascadeLevel()

isFlushing

public boolean isFlushing()

setFlushing

public void setFlushing(boolean flushing)

beforeLoad

public void beforeLoad()
Call this before begining a two-phase load


afterLoad

public void afterLoad()
Call this after finishing a two-phase load


toString

public String toString()
Returns a string representation of the object.

Returns:
a string representation of the object.

getOwnerId

public Serializable getOwnerId(String role,
                               Object childObject)
Search the persistence context for an owner for the child object, given a collection role


getIndexInOwner

public Object getIndexInOwner(String role,
                              Object childObject)
Search the persistence context for an index of the child object, given a collection role


addNullProperty

public void addNullProperty(EntityKey ownerKey,
                            String propertyName)
Record the fact that the association belonging to the keyed entity is null.


isPropertyNull

public boolean isPropertyNull(EntityKey ownerKey,
                              String propertyName)
Is the association property belonging to the keyed entity null?


clearNullProperties

public void clearNullProperties()