Interface PersistentCollection<E>
- Type Parameters:
E- the collection element type, or map value type
- All Superinterfaces:
InstanceIdentity,LazyInitializable
- All Known Implementing Classes:
AbstractPersistentCollection,PersistentArrayHolder,PersistentBag,PersistentIdentifierBag,PersistentList,PersistentMap,PersistentSet,PersistentSortedMap,PersistentSortedSet
Hibernate "wraps" a Java collection in an instance of
PersistentCollection. This mechanism is allows for
tracking of changes to the persistent state of the collection and
lazy fetching of the collection elements. The downside is that
only certain abstract collection types are supported and any
extra semantics associated with the particular implementation of
the generic collection type are lost. For example, every
List behaves like an ArrayList, and
every SortedMap behaves like a TreeMap.
Applications should never use classes in this package directly, unless extending the "framework" here.
Changes to structure of the collection are recorded by the collection calling back to the session. Changes to mutable elements (composite elements) are discovered by cloning their state when the collection is initialized and comparing at flush time.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanCalled after initialization is complete.voidafterRowInsert(CollectionPersister persister, Object entry, int i) Called after inserting a row, to fetch the natively generated idvoidbeforeInitialize(CollectionPersister persister, int anticipatedSize) Called prior to the initialization of this yet-uninitialized collection.voidCalled just before reading any rows from the JDBC result set.voidClear the dirty flag, after flushing changes to the database.voiddirty()Mark the collection as dirtydisassemble(CollectionPersister persister) Disassemble the collection to get it ready for the cacheelementByIndex(Object index) Obtain the element os this collection associated with the given index without initializing itbooleanelementExists(Object element) Determine if the given element belongs to this collection without initializing itbooleanempty()Is the collection empty? (don't try to initialize the collection)booleanendRead()Called after reading all rows from the JDBC result set.Iterator<?> entries(CollectionPersister persister) Iterate all collection entries, during update of the databasebooleanentryExists(Object entry, int i) Does the given element/entry exist in the collection?booleanequalsSnapshot(CollectionPersister persister) Does the current state exactly match the snapshot?Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) Get all the elements that need deletinggetElement(Object entry) Get the value of the given collection entry.getIdentifier(Object entry, int i) Get the identifier of the given collection entry.getIndex(Object entry, int i, CollectionPersister persister) Get the index of the given collection entry@Nullable ObjectgetKey()Get the current collection key valuegetOrphans(Serializable snapshot, String entityName) get all "orphaned" elements@Nullable ObjectgetOwner()Get the owning entity.getQueuedOrphans(String entityName) Get the "queued" orphans@Nullable StringgetRole()Get the current role nameGet the session currently associated with this collection.intgetSize()Obtain the size of this collection without initializing itgetSnapshot(CollectionPersister persister) Return a new snapshot of the current state of the collectiongetSnapshotElement(Object entry, int i) Get the snapshot value of the given collection entry@Nullable SerializableGet the snapshot cached by the collection instancegetValue()Return the user-visible collection (or array) instancebooleanhasDeletes(CollectionPersister persister) Does this collection have any elements which must be deleted?booleanDoes this instance have any "queued" operations?default booleanincludeInInsert(Object entry, int entryPosition, PersistentCollection<?> collection, PluralAttributeMapping attributeDescriptor) Whether to include the entry for insertion operationsdefault booleanincludeInRecreate(Object entry, int i, PersistentCollection<?> collection, PluralAttributeMapping attributeDescriptor) Whether the given entry should be included in recreation eventsvoidinitializeEmptyCollection(CollectionPersister persister) voidinitializeFromCache(CollectionPersister persister, Object disassembled, Object owner) Read the state of the collection from a disassembled cached valuevoidinjectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState) Inject the state loaded for a collection instance.booleanCould the application possibly have a direct reference to the underlying collection implementation?default booleanisDirectlyProvidedCollection(Object collection) Wascollectionprovided directly to this PersistentCollection (i.e., provided as an argument to a constructor)?booleanisDirty()Is the collection dirty? Note that this is only reliable during the flush cycle, after the collection elements are dirty checked against the snapshot.default booleanbooleanIs this PersistentCollection in the process of being initialized?default booleanIs the collection newly instantiated?booleanCan each element in the collection be mapped unequivocally to a single row in the database? Generally bags and sets are the only collections that cannot be.booleanisSnapshotEmpty(Serializable snapshot) Is the snapshot empty?booleanIs the collection unreferenced?booleanIs this the wrapper for the given collection instance?booleanneedsInserting(Object entry, int i, Type elemType) Do we need to insert this element?booleanneedsRecreate(CollectionPersister persister) Do we need to completely recreate this collection when it changes?default booleanneedsUpdating(Object entry, int entryPosition, PluralAttributeMapping attributeDescriptor) Do we need to update this element?booleanneedsUpdating(Object entry, int i, Type elemType) Do we need to update this element?voidAfter flushing, clear any "queued" additions, since the database state is now synchronized with the memory state.voidpreInsert(CollectionPersister persister) Called before inserting rows, to ensure that any surrogate keys are fully generatedIterator<?> Iterator over the "queued" additionsdefault Stringrender()LikeObject.toString()but without the silliness of rendering the elementsbooleanAssociate the collection with the given session.voidSet the reference to the owning entityvoidsetSnapshot(@Nullable Object key, @Nullable String role, @Nullable Serializable snapshot) After flushing, re-init snapshot state.booleanunsetSession(SharedSessionContractImplementor currentSession) Disassociate this collection from the given session.Methods inherited from interface org.hibernate.engine.spi.InstanceIdentity
$$_hibernate_getInstanceId, $$_hibernate_setInstanceIdMethods inherited from interface org.hibernate.collection.spi.LazyInitializable
forceInitialization, wasInitialized
-
Method Details
-
getOwner
@Nullable Object getOwner()Get the owning entity. Note that the owner is only set during the flush cycle, and when a new collection wrapper is created while loading an entity.- Returns:
- The owner
-
setOwner
Set the reference to the owning entity- Parameters:
entity- The owner
-
empty
boolean empty()Is the collection empty? (don't try to initialize the collection)- Returns:
falseif the collection is non-empty;trueotherwise.
-
setSnapshot
After flushing, re-init snapshot state.- Parameters:
key- The collection instance key (fk value).role- The collection rolesnapshot- The snapshot state
-
postAction
void postAction()After flushing, clear any "queued" additions, since the database state is now synchronized with the memory state. -
getValue
Object getValue()Return the user-visible collection (or array) instance- Returns:
- The underlying collection/array
-
isDirectlyAccessible
boolean isDirectlyAccessible()Could the application possibly have a direct reference to the underlying collection implementation?- Returns:
trueindicates that the application might have access to the underlying collection/array.
-
entries
Iterate all collection entries, during update of the database- Parameters:
persister- The collection persister.- Returns:
- The iterator
-
getIdentifier
Get the identifier of the given collection entry. This refers to the collection identifier, not the identifier of the (possibly) entity elements. This is only valid for invocation on theidbagcollection.- Parameters:
entry- The collection entry/elementi- The assumed identifier (?)- Returns:
- The identifier value
-
getIndex
Get the index of the given collection entry- Parameters:
entry- The collection entry/elementi- The assumed indexpersister- it was more elegant before we added this...- Returns:
- The index value
-
getElement
Get the value of the given collection entry. Generally the given entry parameter value will just be returned. Might get a different value for a duplicate entries in a Set.- Parameters:
entry- The object instance for which to get the collection element instance.- Returns:
- The corresponding object that is part of the collection elements.
-
getSnapshotElement
Get the snapshot value of the given collection entry- Parameters:
entry- The entryi- The index- Returns:
- The snapshot state for that element
-
equalsSnapshot
Does the current state exactly match the snapshot?- Parameters:
persister- The collection persister- Returns:
trueif the current state and the snapshot state match.
-
isSnapshotEmpty
Is the snapshot empty?- Parameters:
snapshot- The snapshot to check- Returns:
trueif the given snapshot is empty
-
needsRecreate
Do we need to completely recreate this collection when it changes?- Parameters:
persister- The collection persister- Returns:
trueif a change requires a recreate.
-
getSnapshot
Return a new snapshot of the current state of the collection- Parameters:
persister- The collection persister- Returns:
- The snapshot
-
entryExists
Does the given element/entry exist in the collection?- Parameters:
entry- The object to check if it exists as a collection elementi- Unused- Returns:
trueif the given entry is a collection element
-
includeInRecreate
default boolean includeInRecreate(Object entry, int i, PersistentCollection<?> collection, PluralAttributeMapping attributeDescriptor) Whether the given entry should be included in recreation events -
needsInserting
Do we need to insert this element?- Parameters:
entry- The collection element to checki- The index (for indexed collections)elemType- The type for the element- Returns:
trueif the element needs inserting
-
includeInInsert
default boolean includeInInsert(Object entry, int entryPosition, PersistentCollection<?> collection, PluralAttributeMapping attributeDescriptor) Whether to include the entry for insertion operations -
needsUpdating
default boolean needsUpdating(Object entry, int entryPosition, PluralAttributeMapping attributeDescriptor) Do we need to update this element?- Parameters:
entry- The collection element to checkentryPosition- The index (for indexed collections)attributeDescriptor- The type for the element- Returns:
trueif the element needs updating
-
needsUpdating
Do we need to update this element?- Parameters:
entry- The collection element to checki- The index (for indexed collections)elemType- The type for the element- Returns:
trueif the element needs updating
-
isRowUpdatePossible
boolean isRowUpdatePossible()Can each element in the collection be mapped unequivocally to a single row in the database? Generally bags and sets are the only collections that cannot be.- Returns:
trueif the row for each element is known
-
getDeletes
Get all the elements that need deleting- Parameters:
persister- The collection persisterindexIsFormula- For indexed collections, tells whether the index is a formula (calculated value) mapping- Returns:
- An iterator over the elements to delete
-
hasDeletes
Does this collection have any elements which must be deleted?- Parameters:
persister- The collection persister- Returns:
trueif elements were removed- Since:
- 7
-
isWrapper
Is this the wrapper for the given collection instance?- Parameters:
collection- The collection to check whether this is wrapping it- Returns:
trueif this is a wrapper around that given collection instance.
-
isInitializing
boolean isInitializing()Is this PersistentCollection in the process of being initialized? -
beforeInitialize
Called prior to the initialization of this yet-uninitialized collection. Pairs withafterInitialize() -
initializeFromCache
Read the state of the collection from a disassembled cached value- Parameters:
persister- The collection persisterdisassembled- The disassembled cached stateowner- The collection owner
-
beginRead
void beginRead()Called just before reading any rows from the JDBC result set. Pairs withendRead() -
injectLoadedState
Inject the state loaded for a collection instance. -
endRead
boolean endRead()Called after reading all rows from the JDBC result set. Pairs withbeginRead()- See Also:
-
afterInitialize
boolean afterInitialize()Called after initialization is complete. Pairs withbeforeInitialize(org.hibernate.persister.collection.CollectionPersister, int) -
disassemble
Disassemble the collection to get it ready for the cache- Parameters:
persister- The collection persister- Returns:
- The disassembled state
-
hasQueuedOperations
boolean hasQueuedOperations()Does this instance have any "queued" operations?- Returns:
trueindicates there are pending, queued, delayed operations
-
queuedAdditionIterator
Iterator<?> queuedAdditionIterator()Iterator over the "queued" additions- Returns:
- The iterator
-
getQueuedOrphans
Get the "queued" orphans- Parameters:
entityName- The name of the entity that makes up the elements- Returns:
- The orphaned elements
-
getKey
@Nullable Object getKey()Get the current collection key value- Returns:
- the current collection key value
-
getRole
@Nullable String getRole()Get the current role name- Returns:
- the collection role name
-
isUnreferenced
boolean isUnreferenced()Is the collection unreferenced?- Returns:
trueif the collection is no longer referenced by an owner
-
isDirty
boolean isDirty()Is the collection dirty? Note that this is only reliable during the flush cycle, after the collection elements are dirty checked against the snapshot.- Returns:
trueif the collection is dirty
-
isElementRemoved
default boolean isElementRemoved() -
isDirectlyProvidedCollection
Wascollectionprovided directly to this PersistentCollection (i.e., provided as an argument to a constructor)?Implementors that can copy elements out of a directly provided collection into the wrapped collection should override this method.
- Parameters:
collection- The collection- Returns:
- true, if
collectionwas provided directly to this PersistentCollection; false, otherwise.
-
clearDirty
void clearDirty()Clear the dirty flag, after flushing changes to the database. -
getStoredSnapshot
@Nullable Serializable getStoredSnapshot()Get the snapshot cached by the collection instance- Returns:
- The internally stored snapshot state
-
dirty
void dirty()Mark the collection as dirty -
preInsert
Called before inserting rows, to ensure that any surrogate keys are fully generated- Parameters:
persister- The collection persister
-
afterRowInsert
Called after inserting a row, to fetch the natively generated id- Parameters:
persister- The collection persisterentry- The collection element just insertedi- The element position/index
-
getOrphans
get all "orphaned" elements- Parameters:
snapshot- The snapshot stateentityName- The name of the entity that are the elements of the collection- Returns:
- The orphans
-
getSize
int getSize()Obtain the size of this collection without initializing it -
elementExists
Determine if the given element belongs to this collection without initializing it -
elementByIndex
Obtain the element os this collection associated with the given index without initializing it -
initializeEmptyCollection
-
getSession
Get the session currently associated with this collection. Declared here for use by Hibernate Reactive. -
isNewlyInstantiated
default boolean isNewlyInstantiated()Is the collection newly instantiated?- Returns:
trueif the collection is newly instantiated
-
render
LikeObject.toString()but without the silliness of rendering the elements
-