Top Description Fields Methods
org.hibernate.engine.spi

public Interface PersistenceContext

Known Direct Implementers
org.hibernate.engine.internal.StatefulPersistenceContext
Imports
java.io.Serializable, java.util.Iterator, .Map, java.util.function.BiConsumer, .Consumer, .Supplier, org.hibernate.HibernateException, .Incubating, .Internal, .LockMode, org.hibernate.query.Query, org.hibernate.collection.spi.PersistentCollection, org.hibernate.internal.util.MarkerObject, org.hibernate.persister.collection.CollectionPersister, org.hibernate.persister.entity.EntityPersister, org.hibernate.sql.results.graph.entity.EntityInitializer, org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState, org.hibernate.sql.results.spi.LoadContexts, org.checkerframework.checker.nullness.qual.Nullable

Represents the state of "stuff" Hibernate is tracking, including (not exhaustive):

Often referred to as the "first level cache".

Authors
Gavin King, Steve Ebersole

Field Summary

Modifier and TypeField and Description
public static final Object
NO_ROW

Marker object used to indicate (via reference checking) that no row was returned.

Method Summary

Modifier and TypeMethod and Description
public void
addChildParent(Object
The child of the relationship
child
,
Object
The parent of the relationship
parent
)

Add a child/parent relation to cache for cascading op

public PersistentCollection<?>

Returns:

the previous collection, it the key was already mapped.
addCollectionByKey
(CollectionKey collectionKey, PersistentCollection<?> persistentCollection)

Adds a collection in the collections-by-key map.

public void
addCollectionHolder(PersistentCollection<?> holder)

Register a PersistentCollection object for an array.

public void
public void
addEntity(EntityKey
The key under which to add an entity
key
,
Object
The entity instance to add
entity
)

Add a canonical mapping from entity key to entity instance

public void
addEntity(EntityUniqueKey
The unique (non-primary) key under which to add an entity
euk
,
Object
The entity instance
entity
)

Add an entity to the cache by unique key

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

Adds an entity to the internal caches.

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

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

public CollectionEntry
addInitializedCollection(CollectionPersister persister, PersistentCollection<?> collection, Object 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.

public void
addNewCollection(CollectionPersister persister, PersistentCollection<?>
The collection to be associated with the persistence context
collection
)

Add a new collection (ie.

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, Object 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
afterLoad()

Call this after finishing a two-phase load

public void
afterTransactionCompletion()

Called after transactions end

public void
beforeLoad()

Call this before beginning a two-phase load

public void
checkUniqueness(EntityKey key, Object
The entity reference against which to perform the uniqueness check.
object
)

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

public EntityHolder
claimEntityHolderIfPossible(EntityKey
The key under which to add an entity
key
,
@Nullable Object
The entity instance to add
entity
,
JdbcValuesSourceProcessingState
The processing state which initializes the entity if successfully claimed
processingState
,
EntityInitializer
The initializer to claim the entity instance
initializer
)

Return an existing entity holder for the entity key, possibly creating one if necessary.

public void
clear()

Clear the state of the persistence context

public void
clearCollectionsByKey()

Remove all state of the collections-by-key map.

public boolean
containsCollection(PersistentCollection<?> collection)

Is the given collection associated with this persistence context?

public boolean
public boolean
public boolean

Returns:

true indicates an entity was found; otherwise false
containsEntity
(EntityKey
The key under which to look for an entity
key
)

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

public boolean
public boolean

Returns:

true if the EntityKey had been registered before using registerNullifiableEntityKey(EntityKey)
containsNullifiableEntityKey
(Supplier<EntityKey>
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.
sek
)

Checks if a certain EntityKey was registered as nullifiable on this PersistenceContext.

public boolean
containsProxy(Object proxy)

Is the given proxy associated with this persistence context?

public int
decrementCascadeLevel()

Called after cascading

public void
forEachCollectionEntry(BiConsumer<PersistentCollection<?>, CollectionEntry>
the lambda to apply on each PersistentCollection,CollectionEntry map entry of the PersistenceContext.
action
,
boolean
set this to false for improved efficiency, but that would make it illegal to make changes to the underlying collectionEntries map.
concurrent
)

Execute some action on each entry of the collectionEntries map, optionally iterating on a defensive copy.

public BatchFetchQueue

Returns:

The batch fetch queue in effect for this persistence context
getBatchFetchQueue
()

Get the BatchFetchQueue, instantiating one if necessary.

public Object[]

Returns:

The cached snapshot
getCachedDatabaseSnapshot
(EntityKey
The entity key for which to retrieve the cached snapshot
key
)

Retrieve the cached database snapshot for the requested entity key.

public int
getCascadeLevel()

How deep are we cascaded?

public PersistentCollection<?>
getCollection(CollectionKey collectionKey)

Get the collection instance associated with the CollectionKey

public Map<PersistentCollection<?>, CollectionEntry>
getCollectionEntries()

Doubly internal

public int

Returns:

the size
getCollectionEntriesSize
()

The size of the internal map storing all collection entries.

public CollectionEntry

Returns:

The matching collection entry
getCollectionEntry
(PersistentCollection<?>
The persistent collection instance for which to locate the collection entry
coll
)

Get the collection entry for a persistent collection

public PersistentCollection<?>
getCollectionHolder(Object array)

Get the PersistentCollection object for an array

public Object
getCollectionOwner(Object key, CollectionPersister collectionPersister)

Get the entity that owns this persistent collection

public Map<CollectionKey, PersistentCollection<?>>
getCollectionsByKey()

Deprecated this method should be removed; alternative methods are available that better express the intent, allowing for better optimisations.
Get the mapping from collection key to collection instance
public Object[]

Returns:

The entity's (non-cached) snapshot
getDatabaseSnapshot
(Object
The identifier of the entity for which to grab a snapshot
id
,
EntityPersister
The persister of the entity.
persister
)

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

public Map<EntityKey, Object>
getEntitiesByKey()

Doubly internal

public Object

Returns:

The matching entity, or null
getEntity
(EntityKey
The key under which to look for an entity
key
)

Get the entity instance associated with the given key

public Object

Returns:

The located entity
getEntity
(EntityUniqueKey
The unique (non-primary) key under which to look for an entity
euk
)

Get an entity cached by unique key

public EntityHolder
public Map<EntityKey, EntityHolder>
getEntityHoldersByKey()

Doubly internal

public EntityEntry

Returns:

The entry
getEntry
(Object
The entity instance for which to locate the corresponding entry
entity
)

Retrieve the EntityEntry representation of the given entity.

public Object
getIndexInOwner(String entity, String property, Object childObject, Map<K, V> mergeMap)

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

public LoadContexts

Returns:

The load context
getLoadContexts
()

Retrieve this persistence context's managed load context.

public Object

Returns:

the owner ID if available from the collection's loaded key; otherwise, returns null
getLoadedCollectionOwnerIdOrNull
(PersistentCollection<?>
The persistent collection
collection
)

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

public Object

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
getLoadedCollectionOwnerOrNull
(PersistentCollection<?>
The persistent collection
collection
)

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

public NaturalIdResolutions

Returns:

This persistence context's natural-id helper
getNaturalIdResolutions
()

Access to the natural-id helper for this persistence context

public Object

Returns:

The current (non-cached) snapshot of the entity's natural id state.
getNaturalIdSnapshot
(Object
The identifier of the entity for which to grab a snapshot
id
,
EntityPersister
The persister of the entity.
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 int
public Object

Returns:

The id of the entityName instance which is said to own the child; null if an appropriate owner not located.
getOwnerId
(String
The entity name for the entity type which would own the child
entityName
,
String
The name of the property on the owning entity type which would name this child association.
propertyName
,
Object
The child entity instance for which to locate the owner instance id.
childEntity
,
Map<K, V>
A map of non-persistent instances from an on-going merge operation (possibly null).
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.

public Object
getProxy(EntityKey key)

Get an existing proxy by key

public SharedSessionContractImplementor

Returns:

The session.
getSession
()

Get the session to which this persistence context is bound.

public Serializable
getSnapshot(PersistentCollection<?> coll)

Get the snapshot of the pre-flush collection state

public default boolean
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

Returns:

true, loaded entities/proxies will be made read-only by default; false, loaded entities/proxies will be made modifiable by default.
isDefaultReadOnly
()

Will entities and proxies that are loaded into this persistence context be made read-only by default?

public boolean

Returns:

true indicates a matching entry was found.
isEntryFor
(Object
The entity instance for which to check for an entry
entity
)

Is there an EntityEntry registration for this entity instance?

public boolean
isFlushing()

Is a flush cycle currently in process?

public boolean
isLoadFinished()

Is in a two-phase load?

public boolean

Returns:

true if no EntityKey was registered as nullifiable on this PersistenceContext.
isNullifiableEntityKeysEmpty
()

public boolean
isPropertyNull(EntityKey ownerKey, String propertyName)

Is the association property belonging to the keyed entity null?

public boolean

Returns:

true if the object is read-only; otherwise false to indicate that the object is modifiable.
isReadOnly
(Object
an entity or proxy
entityOrProxy
)

Is the entity or proxy read-only?

public boolean
public Iterator<Object>
managedEntitiesIterator()

A read-only iterator on all entities managed by this persistence context

public Object

Returns:

An appropriately narrowed instance.
narrowProxy
(Object
The proxy instance to be narrowed.
proxy
,
EntityPersister
The persister for the proxied entity.
persister
,
EntityKey
The internal cache key for the proxied entity.
key
,
Object
(optional) the actual proxied entity instance.
object
)

If the existing proxy is insufficiently "narrow" (derived), instantiate a new proxy and overwrite the registration of the old one.

public void
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.

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.

public Object
proxyFor(EntityHolder holder, EntityPersister persister)

Return the existing proxy associated with the given EntityHolder, or the entity if no proxy exists.

public Object
proxyFor(EntityHolder holder)

Deprecated for removal. Use proxyFor(EntityHolder, EntityPersister) instead.
Return the existing proxy associated with the given EntityHolder, or the entity if it contains no proxy.
public boolean

Returns:

Whether the passed value represented an actual proxy which got initialized.
reassociateIfUninitializedProxy
(Object
The possible proxy to be reassociated.
value
)

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

public 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 proxy

public Map.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.

public void
public void
registerInsertedKey(EntityPersister
The entity persister
persister
,
Object
The id
id
)

Register keys inserted during the current transaction

public void
registerNullifiableEntityKey(EntityKey key)

Registers an EntityKey as nullifiable on this PersistenceContext.

public void
removeChildParent(Object
The child to be removed.
child
)

Remove child/parent relation from cache

public void
removeCollectionByKey(CollectionKey
the key to clear
collectionKey
)

Remove a collection-by-key mapping.

public CollectionEntry

Returns:

the matching CollectionEntry, if any was removed.
removeCollectionEntry
(PersistentCollection<?>
the collection to remove
collection
)

Remove a PersistentCollection from the PersistenceContext.

public PersistentCollection<?>
removeCollectionHolder(Object array)

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

public void
public Object

Returns:

The matching entity
removeEntity
(EntityKey
The key whose matching entity should be removed
key
)

Remove an entity.

public EntityHolder
public EntityEntry

Returns:

The matching entry
removeEntry
(Object
The entity instance for which to remove the corresponding entry
entity
)

Remove an entity entry from the session cache

public Object

Returns:

The proxy reference.
removeProxy
(EntityKey
The key of the entity proxy to be removed
key
)

Remove a proxy from the session cache.

public void
public void
setDefaultReadOnly(boolean
true, the default for loaded entities/proxies is read-only; false, the default for loaded entities/proxies is modifiable
readOnly
)

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.

public void
setEntryStatus(EntityEntry
The entry for which to set the status
entry
,
Status
The new status
status
)

Set the status of an entry

public void
setFlushing(boolean flushing)

Called before and after the flush cycle

public void
setReadOnly(Object
an entity or proxy
entityOrProxy
,
boolean
if true, the entity or proxy is made read-only; otherwise, the entity or proxy is made modifiable.
readOnly
)

Set an unmodified persistent object to read-only mode, or a read-only object to modifiable mode.

public String

Returns:

a string representation of the object.
toString
()

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.

public Object

Returns:

The unproxied instance.
unproxyAndReassociate
(Object
The reference to be unproxied if it currently represents a proxy.
maybeProxy
)

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

public PersistentCollection<?>

Returns:

The unowned collection, or null
useUnownedCollection
(CollectionKey
The collection key for which to locate a collection collection
key
)

Take ownership of a previously unowned collection, if one.

public boolean

Returns:

True if inserted during this transaction, false otherwise.
wasInsertedDuringTransaction
(EntityPersister
The entity persister
persister
,
Object
The id
id
)

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