Class StatefulPersistenceContext
- java.lang.Object
-
- org.hibernate.engine.internal.StatefulPersistenceContext
-
- All Implemented Interfaces:
PersistenceContext
public class StatefulPersistenceContext extends Object implements PersistenceContext
A stateful implementation of thePersistenceContext
contract, meaning that we maintain this state throughout the life of the persistence context.- Implementation Note:
- There is meant to be a one-to-one correspondence between a
SessionImpl
and aPersistenceContext
. Event listeners and other session collaborators then use thePersistenceContext
to drive their processing.
-
-
Field Summary
-
Fields inherited from interface org.hibernate.engine.spi.PersistenceContext
NO_ROW
-
-
Constructor Summary
Constructors Constructor Description StatefulPersistenceContext(SharedSessionContractImplementor session)
Constructs a PersistentContext, bound to the given session.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addChildParent(Object child, Object parent)
Add a child/parent relation to cache for cascading opPersistentCollection<?>
addCollectionByKey(CollectionKey collectionKey, PersistentCollection<?> persistentCollection)
Adds a collection in the collections-by-key map.void
addCollectionHolder(PersistentCollection<?> holder)
Register aPersistentCollection
object for an array.void
addEnhancedProxy(EntityKey key, PersistentAttributeInterceptable entity)
Cross betweenPersistenceContext.addEntity(EntityKey, Object)
andPersistenceContext.addProxy(EntityKey, Object)
for use with enhancement-as-proxyEntityEntry
addEntity(Object entity, Status status, Object[] loadedState, EntityKey entityKey, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement)
Adds an entity to the internal caches.void
addEntity(EntityKey key, Object entity)
Add a canonical mapping from entity key to entity instancevoid
addEntity(EntityUniqueKey euk, Object entity)
Add an entity to the cache by unique keyEntityEntry
addEntry(Object entity, Status status, Object[] loadedState, Object rowId, Object 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(CollectionPersister persister, PersistentCollection<?> collection, Object 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 loadvoid
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 cachevoid
addReferenceEntry(Object entity, Status status)
void
addUninitializedCollection(CollectionPersister persister, PersistentCollection<?> collection, Object id)
add a collection we just loaded up (still needs initializing)void
addUninitializedDetachedCollection(CollectionPersister persister, PersistentCollection<?> collection)
add a detached uninitialized collectionvoid
afterLoad()
Call this after finishing a two-phase loadvoid
afterTransactionCompletion()
Called after transactions endvoid
beforeLoad()
Call this before beginning a two-phase loadvoid
beginRemoveOrphanBeforeUpdates()
void
checkUniqueness(EntityKey key, Object object)
Attempts to check whether the given key represents an entity already loaded within the current session.EntityHolder
claimEntityHolderIfPossible(EntityKey key, Object entity, JdbcValuesSourceProcessingState processingState, EntityInitializer initializer)
Return an existing entity holder for the entity key, possibly creating one if necessary.void
clear()
Clear the state of the persistence contextvoid
clearCollectionsByKey()
Remove all state of the collections-by-key map.boolean
containsCollection(PersistentCollection<?> collection)
Is the given collection associated with this persistence context?boolean
containsDeletedUnloadedEntityKey(EntityKey ek)
boolean
containsDeletedUnloadedEntityKeys()
boolean
containsEntity(EntityKey key)
Is there an entity with the given key in the persistence contextboolean
containsEntityHolder(EntityKey key)
boolean
containsNullifiableEntityKey(Supplier<EntityKey> sek)
Checks if a certainEntityKey
was registered as nullifiable on thisPersistenceContext
.boolean
containsProxy(Object entity)
Is the given proxy associated with this persistence context?int
decrementCascadeLevel()
Called after cascadingstatic StatefulPersistenceContext
deserialize(ObjectInputStream ois, SessionImplementor session)
Used by the owning session to explicitly control deserialization of the persistence context.void
endRemoveOrphanBeforeUpdates()
void
forEachCollectionEntry(BiConsumer<PersistentCollection<?>,CollectionEntry> action, boolean concurrent)
Execute some action on each entry of the collectionEntries map, optionally iterating on a defensive copy.BatchFetchQueue
getBatchFetchQueue()
Get theBatchFetchQueue
, instantiating one if necessary.Object[]
getCachedDatabaseSnapshot(EntityKey key)
Retrieve the cached database snapshot for the requested entity key.int
getCascadeLevel()
How deep are we cascaded?PersistentCollection<?>
getCollection(CollectionKey collectionKey)
Get the collection instance associated with theCollectionKey
Map<PersistentCollection<?>,CollectionEntry>
getCollectionEntries()
Deprecated.We should not expose this directly: the other accessors that have been created as a replacement have better chances of skipping initializing this map, which is a good performance improvement.int
getCollectionEntriesSize()
The size of the internal map storing all collection entries.CollectionEntry
getCollectionEntry(PersistentCollection<?> coll)
Get the collection entry for a persistent collectionPersistentCollection<?>
getCollectionHolder(Object array)
Get thePersistentCollection
object for an arrayObject
getCollectionOwner(Object key, CollectionPersister collectionPersister)
Get the entity that owns this persistent collectionMap<CollectionKey,PersistentCollection<?>>
getCollectionsByKey()
Get the mapping from collection key to collection instanceObject[]
getDatabaseSnapshot(Object id, EntityPersister persister)
Get the current state of the entity as known to the underlying database, or null if there is no corresponding rowMap<EntityKey,Object>
getEntitiesByKey()
Deprecated.this will be removed: it provides too wide access, making it hard to optimise the internals for specific access needs.Object
getEntity(EntityKey key)
Get the entity instance associated with the given keyObject
getEntity(EntityUniqueKey euk)
Get an entity cached by unique keyorg.hibernate.engine.internal.StatefulPersistenceContext.EntityHolderImpl
getEntityHolder(EntityKey key)
Map<EntityKey,EntityHolder>
getEntityHoldersByKey()
Doubly internalEntityEntry
getEntry(Object entity)
Retrieve theEntityEntry
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 roleLoadContexts
getLoadContexts()
Retrieve this persistence context's managed load context.Object
getLoadedCollectionOwnerIdOrNull(PersistentCollection<?> collection)
Get the ID for the entity that owned this persistent collection when it was loadedObject
getLoadedCollectionOwnerOrNull(PersistentCollection<?> collection)
Get the entity that owned this persistent collection when it was loadedNaturalIdResolutions
getNaturalIdResolutions()
Access to the natural-id helper for this persistence contextObject
getNaturalIdSnapshot(Object 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.int
getNumberOfManagedEntities()
Object
getOwnerId(String entityName, String propertyName, Object childEntity, Map mergeMap)
Searchthis
persistence context for an associated entity instance which is considered the "owner" of the givenchildEntity
, and return that owner's id value.Object
getProxy(EntityKey key)
Get an existing proxy by keySharedSessionContractImplementor
getSession()
Get the session to which this persistence context is bound.Serializable
getSnapshot(PersistentCollection<?> coll)
Get the snapshot of the pre-flush collection stateboolean
hasLoadContext()
int
incrementCascadeLevel()
Called before cascadingvoid
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)protected void
initializeNonLazyCollections(Consumer<PersistentCollection<?>> initializeAction)
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 anEntityEntry
registration for this entity instance?boolean
isFlushing()
Is a flush cycle currently in process?boolean
isLoadFinished()
Is in a two-phase load?boolean
isNullifiableEntityKeysEmpty()
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?boolean
isRemovingOrphanBeforeUpates()
boolean
isStateless()
Iterator<Object>
managedEntitiesIterator()
A read-only iterator on all entities managed by this persistence contextObject
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.void
postLoad(JdbcValuesSourceProcessingState processingState, Consumer<EntityHolder> holderConsumer)
Object
proxyFor(Object impl)
Return the existing proxy associated with the givenEntityKey
, or the argument (the entity associated with the key) if no proxy exists.Object
proxyFor(EntityHolder holder)
Return the existing proxy associated with the givenEntityHolder
, or the entity if it contains no proxy.Object
proxyFor(EntityHolder holder, EntityPersister persister)
Object
proxyFor(EntityPersister persister, EntityKey key, Object impl)
Return the existing proxy associated with the givenEntityKey
, 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, Object id)
If a deleted entity instance is re-saved, and it has a proxy, we need to reset the identifier of the proxyMap.Entry<Object,EntityEntry>[]
reentrantSafeEntityEntries()
Provides access to the entity/EntityEntry combos associated with the persistence context in a manner that is safe from reentrant access.void
registerDeletedUnloadedEntityKey(EntityKey key)
void
registerInsertedKey(EntityPersister persister, Object id)
Register keys inserted during the current transactionvoid
registerNullifiableEntityKey(EntityKey key)
Registers anEntityKey
as nullifiable on thisPersistenceContext
.void
removeChildParent(Object child)
Remove child/parent relation from cachevoid
removeCollectionByKey(CollectionKey collectionKey)
Remove a collection-by-key mapping.CollectionEntry
removeCollectionEntry(PersistentCollection<?> collection)
Remove aPersistentCollection
from thePersistenceContext
.PersistentCollection<?>
removeCollectionHolder(Object array)
Remove the mapping of collection to holder during eviction of the owning entityvoid
removeDeletedUnloadedEntityKey(EntityKey key)
Object
removeEntity(EntityKey key)
Remove an entity.org.hibernate.engine.internal.StatefulPersistenceContext.EntityHolderImpl
removeEntityHolder(EntityKey key)
EntityEntry
removeEntry(Object entity)
Remove an entity entry from the session cacheObject
removeProxy(EntityKey key)
Remove a proxy from the session cache.void
replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Object 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 read-only mode to modifiable.void
setEntryStatus(EntityEntry entry, Status status)
Set the status of an entryvoid
setFlushing(boolean flushing)
Called before and after the flush cyclevoid
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)
Take ownership of a previously unowned collection, if one.boolean
wasInsertedDuringTransaction(EntityPersister persister, Object id)
Allows callers to check to see if the identified entity was inserted during the current transaction.
-
-
-
Constructor Detail
-
StatefulPersistenceContext
public StatefulPersistenceContext(SharedSessionContractImplementor session)
Constructs a PersistentContext, bound to the given session.- Parameters:
session
- The session "owning" this context.
-
-
Method Detail
-
isStateless
public boolean isStateless()
- Specified by:
isStateless
in interfacePersistenceContext
-
getSession
public SharedSessionContractImplementor getSession()
Description copied from interface:PersistenceContext
Get the session to which this persistence context is bound.- Specified by:
getSession
in interfacePersistenceContext
- Returns:
- The session.
-
getLoadContexts
public LoadContexts getLoadContexts()
Description copied from interface:PersistenceContext
Retrieve this persistence context's managed load context.- Specified by:
getLoadContexts
in interfacePersistenceContext
- Returns:
- The load context
-
hasLoadContext
public boolean hasLoadContext()
- Specified by:
hasLoadContext
in interfacePersistenceContext
-
useUnownedCollection
public PersistentCollection<?> useUnownedCollection(CollectionKey key)
Description copied from interface:PersistenceContext
Take ownership of a previously unowned collection, if one. This method returnsnull
if no such collection was previously added () or was previously removed.This should indicate the owner is being loaded and we are ready to "link" them.
- Specified by:
useUnownedCollection
in interfacePersistenceContext
- Parameters:
key
- The collection key for which to locate a collection collection- Returns:
- The unowned collection, or
null
-
getBatchFetchQueue
public BatchFetchQueue getBatchFetchQueue()
Description copied from interface:PersistenceContext
Get theBatchFetchQueue
, instantiating one if necessary.- Specified by:
getBatchFetchQueue
in interfacePersistenceContext
- Returns:
- The batch fetch queue in effect for this persistence context
-
clear
public void clear()
Description copied from interface:PersistenceContext
Clear the state of the persistence context- Specified by:
clear
in interfacePersistenceContext
-
isDefaultReadOnly
public boolean isDefaultReadOnly()
Description copied from interface:PersistenceContext
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:- Specified by:
isDefaultReadOnly
in interfacePersistenceContext
- Returns:
- true, loaded entities/proxies will be made read-only by default; false, loaded entities/proxies will be made modifiable by default.
- See Also:
PersistenceContext.isReadOnly(Object)
,Session.isReadOnly(Object)
,Session.isDefaultReadOnly()
-
setDefaultReadOnly
public void setDefaultReadOnly(boolean defaultReadOnly)
Description copied from interface:PersistenceContext
Change the default for entities and proxies loaded into this persistence context from modifiable to read-only mode, or from read-only mode to modifiable. 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)- Specified by:
setDefaultReadOnly
in interfacePersistenceContext
- Parameters:
defaultReadOnly
- true, the default for loaded entities/proxies is read-only; false, the default for loaded entities/proxies is modifiable- See Also:
To override this session's read-only/modifiable setting for entities and proxies loaded by a Query:
,Query.setReadOnly(boolean)
,Session.setDefaultReadOnly(boolean)
-
setEntryStatus
public void setEntryStatus(EntityEntry entry, Status status)
Description copied from interface:PersistenceContext
Set the status of an entry- Specified by:
setEntryStatus
in interfacePersistenceContext
- Parameters:
entry
- The entry for which to set the statusstatus
- The new status
-
afterTransactionCompletion
public void afterTransactionCompletion()
Description copied from interface:PersistenceContext
Called after transactions end- Specified by:
afterTransactionCompletion
in interfacePersistenceContext
-
getDatabaseSnapshot
public Object[] getDatabaseSnapshot(Object 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 rowGet the current state of the entity as known to the underlying database, or null if there is no corresponding row
- Specified by:
getDatabaseSnapshot
in interfacePersistenceContext
- Parameters:
id
- The identifier of the entity for which to grab a snapshotpersister
- The persister of the entity.- Returns:
- The entity's (non-cached) snapshot
- Throws:
HibernateException
- See Also:
PersistenceContext.getCachedDatabaseSnapshot(org.hibernate.engine.spi.EntityKey)
-
getNaturalIdSnapshot
public Object getNaturalIdSnapshot(Object id, EntityPersister persister) throws HibernateException
Description copied from interface:PersistenceContext
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.- Specified by:
getNaturalIdSnapshot
in interfacePersistenceContext
- Parameters:
id
- The identifier of the entity for which to grab a snapshotpersister
- The persister of the entity.- Returns:
- The current (non-cached) snapshot of the entity's natural id state.
- Throws:
HibernateException
-
getCachedDatabaseSnapshot
public Object[] getCachedDatabaseSnapshot(EntityKey key)
Description copied from interface:PersistenceContext
Retrieve the cached database snapshot for the requested entity key.This differs from
PersistenceContext.getDatabaseSnapshot(java.lang.Object, org.hibernate.persister.entity.EntityPersister)
in two important respects:- no snapshot is obtained from the database if not already cached
- an entry of
PersistenceContext.NO_ROW
here is interpreted as an exception
- Specified by:
getCachedDatabaseSnapshot
in interfacePersistenceContext
- Parameters:
key
- The entity key for which to retrieve the cached snapshot- Returns:
- The cached snapshot
-
claimEntityHolderIfPossible
public EntityHolder claimEntityHolderIfPossible(EntityKey key, Object entity, JdbcValuesSourceProcessingState processingState, EntityInitializer initializer)
Description copied from interface:PersistenceContext
Return an existing entity holder for the entity key, possibly creating one if necessary. Will claim the entity holder by registering the given entity initializer, if it isn't claimed yet.- Specified by:
claimEntityHolderIfPossible
in interfacePersistenceContext
- Parameters:
key
- The key under which to add an entityentity
- The entity instance to addprocessingState
- The processing state which initializes the entity if successfully claimedinitializer
- The initializer to claim the entity instance
-
getEntityHolder
public org.hibernate.engine.internal.StatefulPersistenceContext.EntityHolderImpl getEntityHolder(EntityKey key)
- Specified by:
getEntityHolder
in interfacePersistenceContext
-
containsEntityHolder
public boolean containsEntityHolder(EntityKey key)
- Specified by:
containsEntityHolder
in interfacePersistenceContext
-
postLoad
public void postLoad(JdbcValuesSourceProcessingState processingState, Consumer<EntityHolder> holderConsumer)
- Specified by:
postLoad
in interfacePersistenceContext
-
addEntity
public void addEntity(EntityKey key, Object entity)
Description copied from interface:PersistenceContext
Add a canonical mapping from entity key to entity instance- Specified by:
addEntity
in interfacePersistenceContext
- Parameters:
key
- The key under which to add an entityentity
- The entity instance to add
-
getEntity
public Object getEntity(EntityKey key)
Description copied from interface:PersistenceContext
Get the entity instance associated with the given key- Specified by:
getEntity
in interfacePersistenceContext
- Parameters:
key
- The key under which to look for an entity- Returns:
- The matching entity, or
null
-
containsEntity
public boolean containsEntity(EntityKey key)
Description copied from interface:PersistenceContext
Is there an entity with the given key in the persistence context- Specified by:
containsEntity
in interfacePersistenceContext
- Parameters:
key
- The key under which to look for an entity- Returns:
true
indicates an entity was found; otherwisefalse
-
removeEntity
public Object removeEntity(EntityKey key)
Description copied from interface:PersistenceContext
Remove an entity. Also clears up all other state associated with the entity aside from theEntityEntry
- Specified by:
removeEntity
in interfacePersistenceContext
- Parameters:
key
- The key whose matching entity should be removed- Returns:
- The matching entity
-
removeEntityHolder
public org.hibernate.engine.internal.StatefulPersistenceContext.EntityHolderImpl removeEntityHolder(EntityKey key)
- Specified by:
removeEntityHolder
in interfacePersistenceContext
-
getEntity
public Object getEntity(EntityUniqueKey euk)
Description copied from interface:PersistenceContext
Get an entity cached by unique key- Specified by:
getEntity
in interfacePersistenceContext
- Parameters:
euk
- The unique (non-primary) key under which to look for an entity- Returns:
- The located entity
-
addEntity
public void addEntity(EntityUniqueKey euk, Object entity)
Description copied from interface:PersistenceContext
Add an entity to the cache by unique key- Specified by:
addEntity
in interfacePersistenceContext
- Parameters:
euk
- The unique (non-primary) key under which to add an entityentity
- The entity instance
-
getEntry
public EntityEntry getEntry(Object entity)
Description copied from interface:PersistenceContext
Retrieve theEntityEntry
representation of the given entity.- Specified by:
getEntry
in interfacePersistenceContext
- Parameters:
entity
- The entity instance for which to locate the corresponding entry- Returns:
- The entry
-
removeEntry
public EntityEntry removeEntry(Object entity)
Description copied from interface:PersistenceContext
Remove an entity entry from the session cache- Specified by:
removeEntry
in interfacePersistenceContext
- Parameters:
entity
- The entity instance for which to remove the corresponding entry- Returns:
- The matching entry
-
isEntryFor
public boolean isEntryFor(Object entity)
Description copied from interface:PersistenceContext
Is there anEntityEntry
registration for this entity instance?- Specified by:
isEntryFor
in interfacePersistenceContext
- Parameters:
entity
- The entity instance for which to check for an entry- Returns:
true
indicates a matching entry was found.
-
getCollectionEntry
public CollectionEntry getCollectionEntry(PersistentCollection<?> coll)
Description copied from interface:PersistenceContext
Get the collection entry for a persistent collection- Specified by:
getCollectionEntry
in interfacePersistenceContext
- Parameters:
coll
- The persistent collection instance for which to locate the collection entry- Returns:
- The matching collection entry
-
addEntity
public EntityEntry addEntity(Object entity, Status status, Object[] loadedState, EntityKey entityKey, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement)
Description copied from interface:PersistenceContext
Adds an entity to the internal caches.- Specified by:
addEntity
in interfacePersistenceContext
-
addEntry
public EntityEntry addEntry(Object entity, Status status, Object[] loadedState, Object rowId, Object id, Object version, LockMode lockMode, boolean existsInDatabase, EntityPersister persister, boolean disableVersionIncrement)
Description copied from interface:PersistenceContext
Generates an appropriate EntityEntry instance and adds it to the event source's internal caches.- Specified by:
addEntry
in interfacePersistenceContext
-
containsCollection
public boolean containsCollection(PersistentCollection<?> collection)
Description copied from interface:PersistenceContext
Is the given collection associated with this persistence context?- Specified by:
containsCollection
in interfacePersistenceContext
-
containsProxy
public boolean containsProxy(Object entity)
Description copied from interface:PersistenceContext
Is the given proxy associated with this persistence context?- Specified by:
containsProxy
in interfacePersistenceContext
-
reassociateIfUninitializedProxy
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException
Description copied from interface:PersistenceContext
Takes the given object and, if it represents a proxy, reassociates it with this event source.- Specified by:
reassociateIfUninitializedProxy
in interfacePersistenceContext
- 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, Object id) throws MappingException
Description copied from interface:PersistenceContext
If a deleted entity instance is re-saved, and it has a proxy, we need to reset the identifier of the proxy- Specified by:
reassociateProxy
in interfacePersistenceContext
- Throws:
MappingException
-
unproxy
public Object unproxy(Object maybeProxy) throws HibernateException
Description copied from interface:PersistenceContext
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.- Specified by:
unproxy
in interfacePersistenceContext
- Throws:
HibernateException
-
unproxyAndReassociate
public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException
Description copied from interface:PersistenceContext
Possibly unproxy the given reference and reassociate it with the current session.- Specified by:
unproxyAndReassociate
in interfacePersistenceContext
- Parameters:
maybeProxy
- The reference to be unproxied if it currently represents a proxy.- Returns:
- The unproxied instance.
- Throws:
HibernateException
-
checkUniqueness
public void checkUniqueness(EntityKey key, Object object) throws HibernateException
Description copied from interface:PersistenceContext
Attempts to check whether the given key represents an entity already loaded within the current session.- Specified by:
checkUniqueness
in interfacePersistenceContext
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
Description copied from interface:PersistenceContext
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.- Specified by:
narrowProxy
in interfacePersistenceContext
- 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
Description copied from interface:PersistenceContext
Return the existing proxy associated with the givenEntityKey
, or the third argument (the entity associated with the key) if no proxy exists. Init the proxy to the target implementation, if necessary.- Specified by:
proxyFor
in interfacePersistenceContext
- Throws:
HibernateException
-
proxyFor
public Object proxyFor(Object impl) throws HibernateException
Description copied from interface:PersistenceContext
Return the existing proxy associated with the givenEntityKey
, or the argument (the entity associated with the key) if no proxy exists. (slower than the form above)- Specified by:
proxyFor
in interfacePersistenceContext
- Throws:
HibernateException
-
proxyFor
public Object proxyFor(EntityHolder holder) throws HibernateException
Description copied from interface:PersistenceContext
Return the existing proxy associated with the givenEntityHolder
, or the entity if it contains no proxy.- Specified by:
proxyFor
in interfacePersistenceContext
- Throws:
HibernateException
-
proxyFor
public Object proxyFor(EntityHolder holder, EntityPersister persister)
Description copied from interface:PersistenceContext
- Specified by:
proxyFor
in interfacePersistenceContext
-
addEnhancedProxy
public void addEnhancedProxy(EntityKey key, PersistentAttributeInterceptable entity)
Description copied from interface:PersistenceContext
Cross betweenPersistenceContext.addEntity(EntityKey, Object)
andPersistenceContext.addProxy(EntityKey, Object)
for use with enhancement-as-proxy- Specified by:
addEnhancedProxy
in interfacePersistenceContext
-
getCollectionOwner
public Object getCollectionOwner(Object key, CollectionPersister collectionPersister) throws MappingException
Description copied from interface:PersistenceContext
Get the entity that owns this persistent collection- Specified by:
getCollectionOwner
in interfacePersistenceContext
- Throws:
MappingException
-
getLoadedCollectionOwnerOrNull
public Object getLoadedCollectionOwnerOrNull(PersistentCollection<?> collection)
Description copied from interface:PersistenceContext
Get the entity that owned this persistent collection when it was loaded- Specified by:
getLoadedCollectionOwnerOrNull
in interfacePersistenceContext
- 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
-
getLoadedCollectionOwnerIdOrNull
public Object getLoadedCollectionOwnerIdOrNull(PersistentCollection<?> collection)
Description copied from interface:PersistenceContext
Get the ID for the entity that owned this persistent collection when it was loaded- Specified by:
getLoadedCollectionOwnerIdOrNull
in interfacePersistenceContext
- Parameters:
collection
- The persistent collection- Returns:
- the owner ID if available from the collection's loaded key; otherwise, returns null
-
addUninitializedCollection
public void addUninitializedCollection(CollectionPersister persister, PersistentCollection<?> collection, Object id)
Description copied from interface:PersistenceContext
add a collection we just loaded up (still needs initializing)- Specified by:
addUninitializedCollection
in interfacePersistenceContext
-
addUninitializedDetachedCollection
public void addUninitializedDetachedCollection(CollectionPersister persister, PersistentCollection<?> collection)
Description copied from interface:PersistenceContext
add a detached uninitialized collection- Specified by:
addUninitializedDetachedCollection
in interfacePersistenceContext
-
addNewCollection
public void addNewCollection(CollectionPersister persister, PersistentCollection<?> collection) throws HibernateException
Description copied from interface:PersistenceContext
Add a new collection (ie. a newly created one, just instantiated by the application, with no database state or snapshot)- Specified by:
addNewCollection
in interfacePersistenceContext
collection
- The collection to be associated with the persistence context- Throws:
HibernateException
-
addInitializedDetachedCollection
public void addInitializedDetachedCollection(CollectionPersister collectionPersister, PersistentCollection<?> collection) throws HibernateException
Description copied from interface:PersistenceContext
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)- Specified by:
addInitializedDetachedCollection
in interfacePersistenceContext
- Throws:
HibernateException
-
addInitializedCollection
public CollectionEntry addInitializedCollection(CollectionPersister persister, PersistentCollection<?> collection, Object id) throws HibernateException
Description copied from interface:PersistenceContext
add a collection we just pulled out of the cache (does not need initializing)- Specified by:
addInitializedCollection
in interfacePersistenceContext
- Throws:
HibernateException
-
getCollection
public PersistentCollection<?> getCollection(CollectionKey collectionKey)
Description copied from interface:PersistenceContext
Get the collection instance associated with theCollectionKey
- Specified by:
getCollection
in interfacePersistenceContext
-
addNonLazyCollection
public void addNonLazyCollection(PersistentCollection<?> collection)
Description copied from interface:PersistenceContext
Register a collection for non-lazy loading at the end of the two-phase load- Specified by:
addNonLazyCollection
in interfacePersistenceContext
-
initializeNonLazyCollections
public void initializeNonLazyCollections() throws HibernateException
Description copied from interface:PersistenceContext
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)- Specified by:
initializeNonLazyCollections
in interfacePersistenceContext
- Throws:
HibernateException
-
initializeNonLazyCollections
protected void initializeNonLazyCollections(Consumer<PersistentCollection<?>> initializeAction)
-
getCollectionHolder
public PersistentCollection<?> getCollectionHolder(Object array)
Description copied from interface:PersistenceContext
Get thePersistentCollection
object for an array- Specified by:
getCollectionHolder
in interfacePersistenceContext
-
addCollectionHolder
public void addCollectionHolder(PersistentCollection<?> holder)
Description copied from interface:PersistenceContext
Register aPersistentCollection
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().- Specified by:
addCollectionHolder
in interfacePersistenceContext
-
removeCollectionHolder
public PersistentCollection<?> removeCollectionHolder(Object array)
Description copied from interface:PersistenceContext
Remove the mapping of collection to holder during eviction of the owning entity- Specified by:
removeCollectionHolder
in interfacePersistenceContext
-
getSnapshot
public Serializable getSnapshot(PersistentCollection<?> coll)
Description copied from interface:PersistenceContext
Get the snapshot of the pre-flush collection state- Specified by:
getSnapshot
in interfacePersistenceContext
-
getProxy
public Object getProxy(EntityKey key)
Description copied from interface:PersistenceContext
Get an existing proxy by key- Specified by:
getProxy
in interfacePersistenceContext
-
addProxy
public void addProxy(EntityKey key, Object proxy)
Description copied from interface:PersistenceContext
Add a proxy to the session cache- Specified by:
addProxy
in interfacePersistenceContext
-
removeProxy
public Object removeProxy(EntityKey key)
Description copied from interface:PersistenceContext
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.
- Specified by:
removeProxy
in interfacePersistenceContext
- Parameters:
key
- The key of the entity proxy to be removed- Returns:
- The proxy reference.
-
getEntitiesByKey
@Deprecated public Map<EntityKey,Object> getEntitiesByKey()
Deprecated.this will be removed: it provides too wide access, making it hard to optimise the internals for specific access needs. Consider using #iterateEntities instead.Description copied from interface:PersistenceContext
Doubly internal- Specified by:
getEntitiesByKey
in interfacePersistenceContext
-
getEntityHoldersByKey
public Map<EntityKey,EntityHolder> getEntityHoldersByKey()
Description copied from interface:PersistenceContext
Doubly internal- Specified by:
getEntityHoldersByKey
in interfacePersistenceContext
-
managedEntitiesIterator
public Iterator<Object> managedEntitiesIterator()
Description copied from interface:PersistenceContext
A read-only iterator on all entities managed by this persistence context- Specified by:
managedEntitiesIterator
in interfacePersistenceContext
-
getNumberOfManagedEntities
public int getNumberOfManagedEntities()
- Specified by:
getNumberOfManagedEntities
in interfacePersistenceContext
-
getCollectionEntries
@Deprecated public Map<PersistentCollection<?>,CollectionEntry> getCollectionEntries()
Deprecated.We should not expose this directly: the other accessors that have been created as a replacement have better chances of skipping initializing this map, which is a good performance improvement.Description copied from interface:PersistenceContext
Doubly internal- Specified by:
getCollectionEntries
in interfacePersistenceContext
- Returns:
- the map of managed collection entries.
-
forEachCollectionEntry
public void forEachCollectionEntry(BiConsumer<PersistentCollection<?>,CollectionEntry> action, boolean concurrent)
Description copied from interface:PersistenceContext
Execute some action on each entry of the collectionEntries map, optionally iterating on a defensive copy.- Specified by:
forEachCollectionEntry
in interfacePersistenceContext
- Parameters:
action
- the lambda to apply on each PersistentCollection,CollectionEntry map entry of the PersistenceContext.concurrent
- set this to false for improved efficiency, but that would make it illegal to make changes to the underlying collectionEntries map.
-
getCollectionsByKey
public Map<CollectionKey,PersistentCollection<?>> getCollectionsByKey()
Description copied from interface:PersistenceContext
Get the mapping from collection key to collection instance- Specified by:
getCollectionsByKey
in interfacePersistenceContext
-
getCascadeLevel
public int getCascadeLevel()
Description copied from interface:PersistenceContext
How deep are we cascaded?- Specified by:
getCascadeLevel
in interfacePersistenceContext
-
incrementCascadeLevel
public int incrementCascadeLevel()
Description copied from interface:PersistenceContext
Called before cascading- Specified by:
incrementCascadeLevel
in interfacePersistenceContext
-
decrementCascadeLevel
public int decrementCascadeLevel()
Description copied from interface:PersistenceContext
Called after cascading- Specified by:
decrementCascadeLevel
in interfacePersistenceContext
-
isFlushing
public boolean isFlushing()
Description copied from interface:PersistenceContext
Is a flush cycle currently in process?- Specified by:
isFlushing
in interfacePersistenceContext
-
setFlushing
public void setFlushing(boolean flushing)
Description copied from interface:PersistenceContext
Called before and after the flush cycle- Specified by:
setFlushing
in interfacePersistenceContext
-
isRemovingOrphanBeforeUpates
public boolean isRemovingOrphanBeforeUpates()
-
beginRemoveOrphanBeforeUpdates
public void beginRemoveOrphanBeforeUpdates()
-
endRemoveOrphanBeforeUpdates
public void endRemoveOrphanBeforeUpdates()
-
beforeLoad
public void beforeLoad()
Call this before beginning a two-phase load- Specified by:
beforeLoad
in interfacePersistenceContext
-
afterLoad
public void afterLoad()
Call this after finishing a two-phase load- Specified by:
afterLoad
in interfacePersistenceContext
-
isLoadFinished
public boolean isLoadFinished()
Description copied from interface:PersistenceContext
Is in a two-phase load?- Specified by:
isLoadFinished
in interfacePersistenceContext
-
toString
public String toString()
Description copied from interface:PersistenceContext
Returns a string representation of the object.- Specified by:
toString
in interfacePersistenceContext
- Overrides:
toString
in classObject
- Returns:
- a string representation of the object.
-
reentrantSafeEntityEntries
public Map.Entry<Object,EntityEntry>[] reentrantSafeEntityEntries()
Description copied from interface:PersistenceContext
Provides access to the entity/EntityEntry combos associated with the persistence context in a manner that is safe from reentrant access. Specifically, it is safe from additions/removals while iterating.- Specified by:
reentrantSafeEntityEntries
in interfacePersistenceContext
-
getOwnerId
public Object getOwnerId(String entityName, String propertyName, Object childEntity, Map mergeMap)
Description copied from interface:PersistenceContext
Searchthis
persistence context for an associated entity instance which is considered the "owner" of the givenchildEntity
, 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.- Specified by:
getOwnerId
in interfacePersistenceContext
- Parameters:
entityName
- The entity name for the entity type which would own the childpropertyName
- 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.
-
getIndexInOwner
public Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap)
Description copied from interface:PersistenceContext
Search the persistence context for an index of the child object, given a collection role- Specified by:
getIndexInOwner
in interfacePersistenceContext
-
addNullProperty
public void addNullProperty(EntityKey ownerKey, String propertyName)
Description copied from interface:PersistenceContext
Record the fact that the association belonging to the keyed entity is null.- Specified by:
addNullProperty
in interfacePersistenceContext
-
isPropertyNull
public boolean isPropertyNull(EntityKey ownerKey, String propertyName)
Description copied from interface:PersistenceContext
Is the association property belonging to the keyed entity null?- Specified by:
isPropertyNull
in interfacePersistenceContext
-
isReadOnly
public boolean isReadOnly(Object entityOrProxy)
Description copied from interface:PersistenceContext
Is the entity or proxy read-only?To determine the default read-only/modifiable setting used for entities and proxies that are loaded into the session use
Session.isDefaultReadOnly()
- Specified by:
isReadOnly
in interfacePersistenceContext
- Parameters:
entityOrProxy
- an entity or proxy- Returns:
true
if the object is read-only; otherwisefalse
to indicate that the object is modifiable.
-
setReadOnly
public void setReadOnly(Object object, boolean readOnly)
Description copied from interface:PersistenceContext
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.- Specified by:
setReadOnly
in interfacePersistenceContext
- Parameters:
object
- an entity or proxyreadOnly
- iftrue
, the entity or proxy is made read-only; otherwise, the entity or proxy is made modifiable.- See Also:
Session.setDefaultReadOnly(boolean)
,Session.setReadOnly(java.lang.Object, boolean)
,Query.setReadOnly(boolean)
-
replaceDelayedEntityIdentityInsertKeys
public void replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Object generatedId)
- Specified by:
replaceDelayedEntityIdentityInsertKeys
in interfacePersistenceContext
-
serialize
public void serialize(ObjectOutputStream oos) throws IOException
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.
-
deserialize
public static StatefulPersistenceContext deserialize(ObjectInputStream ois, SessionImplementor session) throws IOException, ClassNotFoundException
Used by the owning session to explicitly control deserialization of the persistence context.- Parameters:
ois
- The stream from which the persistence context should be readsession
- The owning session- Returns:
- The deserialized StatefulPersistenceContext
- Throws:
IOException
- deserialization errors.ClassNotFoundException
- deserialization errors.
-
addChildParent
public void addChildParent(Object child, Object parent)
Description copied from interface:PersistenceContext
Add a child/parent relation to cache for cascading op- Specified by:
addChildParent
in interfacePersistenceContext
- Parameters:
child
- The child of the relationshipparent
- The parent of the relationship
-
removeChildParent
public void removeChildParent(Object child)
Description copied from interface:PersistenceContext
Remove child/parent relation from cache- Specified by:
removeChildParent
in interfacePersistenceContext
- Parameters:
child
- The child to be removed.
-
registerInsertedKey
public void registerInsertedKey(EntityPersister persister, Object id)
Description copied from interface:PersistenceContext
Register keys inserted during the current transaction- Specified by:
registerInsertedKey
in interfacePersistenceContext
- Parameters:
persister
- The entity persisterid
- The id
-
wasInsertedDuringTransaction
public boolean wasInsertedDuringTransaction(EntityPersister persister, Object id)
Description copied from interface:PersistenceContext
Allows callers to check to see if the identified entity was inserted during the current transaction.- Specified by:
wasInsertedDuringTransaction
in interfacePersistenceContext
- Parameters:
persister
- The entity persisterid
- The id- Returns:
- True if inserted during this transaction, false otherwise.
-
containsNullifiableEntityKey
public boolean containsNullifiableEntityKey(Supplier<EntityKey> sek)
Description copied from interface:PersistenceContext
Checks if a certainEntityKey
was registered as nullifiable on thisPersistenceContext
.- Specified by:
containsNullifiableEntityKey
in interfacePersistenceContext
- Parameters:
sek
- a supplier for the EntityKey; this allows to not always needing to create the key; for example if the map is known to be empty there is no need to create one to check.- Returns:
- true if the EntityKey had been registered before using
PersistenceContext.registerNullifiableEntityKey(EntityKey)
- See Also:
PersistenceContext.registerNullifiableEntityKey(EntityKey)
-
registerNullifiableEntityKey
public void registerNullifiableEntityKey(EntityKey key)
Description copied from interface:PersistenceContext
Registers anEntityKey
as nullifiable on thisPersistenceContext
.- Specified by:
registerNullifiableEntityKey
in interfacePersistenceContext
-
isNullifiableEntityKeysEmpty
public boolean isNullifiableEntityKeysEmpty()
- Specified by:
isNullifiableEntityKeysEmpty
in interfacePersistenceContext
- Returns:
- true if no
EntityKey
was registered as nullifiable on thisPersistenceContext
. - See Also:
PersistenceContext.registerNullifiableEntityKey(EntityKey)
-
containsDeletedUnloadedEntityKey
public boolean containsDeletedUnloadedEntityKey(EntityKey ek)
- Specified by:
containsDeletedUnloadedEntityKey
in interfacePersistenceContext
-
registerDeletedUnloadedEntityKey
public void registerDeletedUnloadedEntityKey(EntityKey key)
- Specified by:
registerDeletedUnloadedEntityKey
in interfacePersistenceContext
-
removeDeletedUnloadedEntityKey
public void removeDeletedUnloadedEntityKey(EntityKey key)
- Specified by:
removeDeletedUnloadedEntityKey
in interfacePersistenceContext
-
containsDeletedUnloadedEntityKeys
public boolean containsDeletedUnloadedEntityKeys()
- Specified by:
containsDeletedUnloadedEntityKeys
in interfacePersistenceContext
-
getCollectionEntriesSize
public int getCollectionEntriesSize()
Description copied from interface:PersistenceContext
The size of the internal map storing all collection entries. (The map is not exposed directly, but the size is often useful)- Specified by:
getCollectionEntriesSize
in interfacePersistenceContext
- Returns:
- the size
-
removeCollectionEntry
public CollectionEntry removeCollectionEntry(PersistentCollection<?> collection)
Description copied from interface:PersistenceContext
Remove aPersistentCollection
from thePersistenceContext
.- Specified by:
removeCollectionEntry
in interfacePersistenceContext
- Parameters:
collection
- the collection to remove- Returns:
- the matching
CollectionEntry
, if any was removed.
-
clearCollectionsByKey
public void clearCollectionsByKey()
Description copied from interface:PersistenceContext
Remove all state of the collections-by-key map.- Specified by:
clearCollectionsByKey
in interfacePersistenceContext
-
addCollectionByKey
public PersistentCollection<?> addCollectionByKey(CollectionKey collectionKey, PersistentCollection<?> persistentCollection)
Description copied from interface:PersistenceContext
Adds a collection in the collections-by-key map.- Specified by:
addCollectionByKey
in interfacePersistenceContext
- Returns:
- the previous collection, it the key was already mapped.
-
removeCollectionByKey
public void removeCollectionByKey(CollectionKey collectionKey)
Description copied from interface:PersistenceContext
Remove a collection-by-key mapping.- Specified by:
removeCollectionByKey
in interfacePersistenceContext
- Parameters:
collectionKey
- the key to clear
-
getNaturalIdResolutions
public NaturalIdResolutions getNaturalIdResolutions()
Description copied from interface:PersistenceContext
Access to the natural-id helper for this persistence context- Specified by:
getNaturalIdResolutions
in interfacePersistenceContext
- Returns:
- This persistence context's natural-id helper
-
-