Package org.hibernate.collection.spi
Class AbstractPersistentCollection<E>
- java.lang.Object
-
- org.hibernate.collection.spi.AbstractPersistentCollection<E>
-
- All Implemented Interfaces:
Serializable
,LazyInitializable
,PersistentCollection<E>
- Direct Known Subclasses:
PersistentArrayHolder
,PersistentBag
,PersistentIdentifierBag
,PersistentList
,PersistentMap
,PersistentSet
public abstract class AbstractPersistentCollection<E> extends Object implements Serializable, PersistentCollection<E>
Base class implementingPersistentCollection
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractPersistentCollection.AbstractValueDelayedOperation
protected static interface
AbstractPersistentCollection.DelayedOperation<E>
Contract for operations which are part of a collection's operation queue.protected class
AbstractPersistentCollection.IteratorProxy<E>
static interface
AbstractPersistentCollection.LazyInitializationWork<T>
TBH not sure why this is publicprotected class
AbstractPersistentCollection.ListIteratorProxy
protected class
AbstractPersistentCollection.ListProxy
protected class
AbstractPersistentCollection.SetProxy<E>
protected static interface
AbstractPersistentCollection.ValueDelayedOperation<E>
-
Field Summary
Fields Modifier and Type Field Description protected boolean
elementRemoved
protected static Object
UNKNOWN
-
Constructor Summary
Constructors Modifier Constructor Description AbstractPersistentCollection()
Not called by Hibernate, but used by non-JDK serialization, eg.protected
AbstractPersistentCollection(SharedSessionContractImplementor session)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
afterInitialize()
Called after initialization is complete.void
afterRowInsert(CollectionPersister persister, Object entry, int i)
Called after inserting a row, to fetch the natively generated idvoid
beforeInitialize(CollectionPersister persister, int anticipatedSize)
Called prior to the initialization of this yet-uninitialized collection.void
beginRead()
Called just before reading any rows from the JDBC result set.void
clearDirty()
Clear the dirty flag, after flushing changes to the database.void
clearOperationQueue()
void
dirty()
Mark the collection as dirtyObject
elementByIndex(Object index)
Obtain the element os this collection associated with the given index without initializing itboolean
elementExists(Object element)
Determine if the given element belongs to this collection without initializing itabstract boolean
empty()
Is the collection empty? (don't try to initialize the collection)boolean
endRead()
Called after reading all rows from the JDBC result set.void
forceInitialization()
To be called internally by the session, forcing immediate initialization.protected int
getCachedSize()
Object
getIdentifier(Object entry, int i)
Get the identifier of the given collection entry.@Nullable Object
getKey()
Get the current collection key valueabstract Collection<E>
getOrphans(Serializable snapshot, String entityName)
get all "orphaned" elementsprotected static <E> Collection<E>
getOrphans(Collection<E> oldElements, Collection<E> currentElements, String entityName, SharedSessionContractImplementor session)
Given a collection of entity instances that used to belong to the collection, and a collection of instances that currently belong, return a collection of orphans@Nullable Object
getOwner()
Get the owning entity.Collection<E>
getQueuedOrphans(String entityName)
Get the "queued" orphans@Nullable String
getRole()
Get the current role nameSharedSessionContractImplementor
getSession()
Get the session currently associated with this collection.int
getSize()
Obtain the size of this collection without initializing itprotected Serializable
getSnapshot()
Get the current snapshot from the session@Nullable Serializable
getStoredSnapshot()
Get the snapshot cached by the collection instanceObject
getValue()
Return the user-visible collection (or array) instanceboolean
hasQueuedOperations()
Does this instance have any "queued" operations?static void
identityRemove(Collection<?> list, Object entityInstance, String entityName, SharedSessionContractImplementor session)
Removes entity entries that have an equal identifier with the incoming entity instanceprotected void
initialize(boolean writing)
Initialize the collection, if possible, wrapping any exceptions in a runtime exceptionprotected boolean
isClearQueueEnabled()
Is this collection in a state that would allow us to "queue" clear? This is a special case, because of orphan delete.protected boolean
isConnectedToSession()
boolean
isDirectlyAccessible()
Could the application possibly have a direct reference to the underlying collection implementation?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.boolean
isElementRemoved()
protected boolean
isInitialized()
boolean
isInitializing()
Is this PersistentCollection in the process of being initialized?protected boolean
isInverseCollection()
Is this the "inverse" end of a bidirectional association?protected boolean
isInverseCollectionNoOrphanDelete()
Is this the "inverse" end of a bidirectional association with no orphan delete enabled?protected boolean
isInverseOneToManyOrNoOrphanDelete()
Is this the "inverse" end of a bidirectional one-to-many, or of a collection with no orphan delete?protected boolean
isOperationQueueEnabled()
Is this collection in a state that would allow us to "queue" operations?protected boolean
isPutQueueEnabled()
Is this collection in a state that would allow us to "queue" puts? This is a special case, because of orphan delete.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.boolean
isUnreferenced()
Is the collection unreferenced?boolean
needsRecreate(CollectionPersister persister)
Do we need to completely recreate this collection when it changes?protected void
performQueuedOperations()
After reading all existing elements from the database, add the queued elements to the underlying collection.void
postAction()
After flushing, clear any "queued" additions, since the database state is now synchronized with the memory state.void
preInsert(CollectionPersister persister)
Called before inserting rows, to ensure that any surrogate keys are fully generatedprotected void
prepareForPossibleLoadingOutsideTransaction()
Iterator<E>
queuedAdditionIterator()
Iterator over the "queued" additionsprotected void
queueOperation(AbstractPersistentCollection.DelayedOperation<E> operation)
Queue an additionprotected void
read()
Called by any read-only method of the collection interfaceprotected Object
readElementByIndex(Object index)
protected Boolean
readElementExistence(Object element)
protected Boolean
readIndexExistence(Object index)
protected boolean
readSize()
Called by theCollection.size()
methodvoid
replaceQueuedOperationValues(CollectionPersister persister, Map<Object,Object> copyCache)
Replace entity instances with copy incopyCache
.boolean
setCurrentSession(SharedSessionContractImplementor session)
Associate the collection with the given session.protected void
setDirectlyAccessible(boolean directlyAccessible)
protected void
setInitialized()
void
setOwner(Object owner)
Set the reference to the owning entityvoid
setSnapshot(Object key, String role, Serializable snapshot)
After flushing, re-init snapshot state.boolean
unsetSession(SharedSessionContractImplementor currentSession)
Disassociate this collection from the given session.boolean
wasInitialized()
Is this instance initialized?protected void
write()
Called by any writer method of the collection interface-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.hibernate.collection.spi.PersistentCollection
disassemble, entries, entryExists, equalsSnapshot, getDeletes, getElement, getIndex, getSnapshot, getSnapshotElement, includeInInsert, includeInRecreate, initializeEmptyCollection, initializeFromCache, injectLoadedState, isDirectlyProvidedCollection, isNewlyInstantiated, isSnapshotEmpty, isWrapper, needsInserting, needsUpdating, needsUpdating, render
-
-
-
-
Field Detail
-
elementRemoved
protected boolean elementRemoved
-
UNKNOWN
protected static final Object UNKNOWN
-
-
Constructor Detail
-
AbstractPersistentCollection
public AbstractPersistentCollection()
Not called by Hibernate, but used by non-JDK serialization, eg. SOAP libraries.
-
AbstractPersistentCollection
protected AbstractPersistentCollection(SharedSessionContractImplementor session)
-
-
Method Detail
-
getRole
public final @Nullable String getRole()
Description copied from interface:PersistentCollection
Get the current role name- Specified by:
getRole
in interfacePersistentCollection<E>
- Returns:
- the collection role name
-
getKey
public final @Nullable Object getKey()
Description copied from interface:PersistentCollection
Get the current collection key value- Specified by:
getKey
in interfacePersistentCollection<E>
- Returns:
- the current collection key value
-
isUnreferenced
public final boolean isUnreferenced()
Description copied from interface:PersistentCollection
Is the collection unreferenced?- Specified by:
isUnreferenced
in interfacePersistentCollection<E>
- Returns:
true
if the collection is no longer referenced by an owner
-
isDirty
public final boolean isDirty()
Description copied from interface:PersistentCollection
Is the collection dirty? Note that this is only reliable during the flush cycle, after the collection elements are dirty checked against the snapshot.- Specified by:
isDirty
in interfacePersistentCollection<E>
- Returns:
true
if the collection is dirty
-
isElementRemoved
public boolean isElementRemoved()
- Specified by:
isElementRemoved
in interfacePersistentCollection<E>
-
clearDirty
public final void clearDirty()
Description copied from interface:PersistentCollection
Clear the dirty flag, after flushing changes to the database.- Specified by:
clearDirty
in interfacePersistentCollection<E>
-
dirty
public final void dirty()
Description copied from interface:PersistentCollection
Mark the collection as dirty- Specified by:
dirty
in interfacePersistentCollection<E>
-
getStoredSnapshot
public final @Nullable Serializable getStoredSnapshot()
Description copied from interface:PersistentCollection
Get the snapshot cached by the collection instance- Specified by:
getStoredSnapshot
in interfacePersistentCollection<E>
- Returns:
- The internally stored snapshot state
-
empty
public abstract boolean empty()
Description copied from interface:PersistentCollection
Is the collection empty? (don't try to initialize the collection)- Specified by:
empty
in interfacePersistentCollection<E>
- Returns:
false
if the collection is non-empty;true
otherwise.
-
read
protected final void read()
Called by any read-only method of the collection interface
-
readSize
protected boolean readSize()
Called by theCollection.size()
method
-
getSize
public int getSize()
Description copied from interface:PersistentCollection
Obtain the size of this collection without initializing it- Specified by:
getSize
in interfacePersistentCollection<E>
-
elementExists
public boolean elementExists(Object element)
Description copied from interface:PersistentCollection
Determine if the given element belongs to this collection without initializing it- Specified by:
elementExists
in interfacePersistentCollection<E>
-
elementByIndex
public Object elementByIndex(Object index)
Description copied from interface:PersistentCollection
Obtain the element os this collection associated with the given index without initializing it- Specified by:
elementByIndex
in interfacePersistentCollection<E>
-
getCachedSize
protected int getCachedSize()
-
isConnectedToSession
protected boolean isConnectedToSession()
-
isInitialized
protected boolean isInitialized()
-
write
protected final void write()
Called by any writer method of the collection interface
-
isOperationQueueEnabled
protected boolean isOperationQueueEnabled()
Is this collection in a state that would allow us to "queue" operations?
-
isPutQueueEnabled
protected boolean isPutQueueEnabled()
Is this collection in a state that would allow us to "queue" puts? This is a special case, because of orphan delete.
-
isClearQueueEnabled
protected boolean isClearQueueEnabled()
Is this collection in a state that would allow us to "queue" clear? This is a special case, because of orphan delete.
-
isInverseCollection
protected boolean isInverseCollection()
Is this the "inverse" end of a bidirectional association?
-
isInverseCollectionNoOrphanDelete
protected boolean isInverseCollectionNoOrphanDelete()
Is this the "inverse" end of a bidirectional association with no orphan delete enabled?
-
isInverseOneToManyOrNoOrphanDelete
protected boolean isInverseOneToManyOrNoOrphanDelete()
Is this the "inverse" end of a bidirectional one-to-many, or of a collection with no orphan delete?
-
queueOperation
protected final void queueOperation(AbstractPersistentCollection.DelayedOperation<E> operation)
Queue an addition
-
replaceQueuedOperationValues
public final void replaceQueuedOperationValues(CollectionPersister persister, Map<Object,Object> copyCache)
Replace entity instances with copy incopyCache
.- Parameters:
copyCache
- - mapping from entity in the process of being merged to managed copy.
-
performQueuedOperations
protected final void performQueuedOperations()
After reading all existing elements from the database, add the queued elements to the underlying collection.
-
setSnapshot
public void setSnapshot(Object key, String role, Serializable snapshot)
Description copied from interface:PersistentCollection
After flushing, re-init snapshot state.- Specified by:
setSnapshot
in interfacePersistentCollection<E>
- Parameters:
key
- The collection instance key (fk value).role
- The collection rolesnapshot
- The snapshot state
-
postAction
public void postAction()
Description copied from interface:PersistentCollection
After flushing, clear any "queued" additions, since the database state is now synchronized with the memory state.- Specified by:
postAction
in interfacePersistentCollection<E>
-
clearOperationQueue
public final void clearOperationQueue()
-
getValue
public Object getValue()
Description copied from interface:PersistentCollection
Return the user-visible collection (or array) instance- Specified by:
getValue
in interfacePersistentCollection<E>
- Returns:
- The underlying collection/array
-
beginRead
public void beginRead()
Description copied from interface:PersistentCollection
Called just before reading any rows from the JDBC result set. Pairs withPersistentCollection.endRead()
- Specified by:
beginRead
in interfacePersistentCollection<E>
-
endRead
public boolean endRead()
Description copied from interface:PersistentCollection
Called after reading all rows from the JDBC result set. Pairs withPersistentCollection.beginRead()
- Specified by:
endRead
in interfacePersistentCollection<E>
- See Also:
PersistentCollection.injectLoadedState(org.hibernate.metamodel.mapping.PluralAttributeMapping, java.util.List<?>)
-
beforeInitialize
public void beforeInitialize(CollectionPersister persister, int anticipatedSize)
Description copied from interface:PersistentCollection
Called prior to the initialization of this yet-uninitialized collection. Pairs withPersistentCollection.afterInitialize()
- Specified by:
beforeInitialize
in interfacePersistentCollection<E>
-
afterInitialize
public boolean afterInitialize()
Description copied from interface:PersistentCollection
Called after initialization is complete. Pairs withPersistentCollection.beforeInitialize(org.hibernate.persister.collection.CollectionPersister, int)
- Specified by:
afterInitialize
in interfacePersistentCollection<E>
-
initialize
protected final void initialize(boolean writing)
Initialize the collection, if possible, wrapping any exceptions in a runtime exception- Parameters:
writing
- currently obsolete- Throws:
LazyInitializationException
- if we cannot initialize
-
setInitialized
protected final void setInitialized()
-
isInitializing
public boolean isInitializing()
Description copied from interface:PersistentCollection
Is this PersistentCollection in the process of being initialized?- Specified by:
isInitializing
in interfacePersistentCollection<E>
-
setDirectlyAccessible
protected final void setDirectlyAccessible(boolean directlyAccessible)
-
isDirectlyAccessible
public boolean isDirectlyAccessible()
Description copied from interface:PersistentCollection
Could the application possibly have a direct reference to the underlying collection implementation?- Specified by:
isDirectlyAccessible
in interfacePersistentCollection<E>
- Returns:
true
indicates that the application might have access to the underlying collection/array.
-
unsetSession
public final boolean unsetSession(SharedSessionContractImplementor currentSession)
Description copied from interface:PersistentCollection
Disassociate this collection from the given session.- Specified by:
unsetSession
in interfacePersistentCollection<E>
- Parameters:
currentSession
- The session we are disassociating from. Used for validations.- Returns:
- true if this was currently associated with the given session
-
prepareForPossibleLoadingOutsideTransaction
protected void prepareForPossibleLoadingOutsideTransaction()
-
setCurrentSession
public final boolean setCurrentSession(SharedSessionContractImplementor session) throws HibernateException
Description copied from interface:PersistentCollection
Associate the collection with the given session.- Specified by:
setCurrentSession
in interfacePersistentCollection<E>
- Parameters:
session
- The session to associate with- Returns:
- false if the collection was already associated with the session
- Throws:
HibernateException
- if the collection was already associated with another open session
-
needsRecreate
public boolean needsRecreate(CollectionPersister persister)
Description copied from interface:PersistentCollection
Do we need to completely recreate this collection when it changes?- Specified by:
needsRecreate
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persister- Returns:
true
if a change requires a recreate.
-
forceInitialization
public final void forceInitialization() throws HibernateException
Description copied from interface:LazyInitializable
To be called internally by the session, forcing immediate initialization.- Specified by:
forceInitialization
in interfaceLazyInitializable
- Throws:
HibernateException
- See Also:
Hibernate.initialize(Object)
-
getSnapshot
protected final Serializable getSnapshot()
Get the current snapshot from the session
-
wasInitialized
public final boolean wasInitialized()
Description copied from interface:LazyInitializable
Is this instance initialized?- Specified by:
wasInitialized
in interfaceLazyInitializable
- Returns:
- Was this collection initialized? Or is its data still not (fully) loaded?
- See Also:
Hibernate.isInitialized(Object)
-
isRowUpdatePossible
public boolean isRowUpdatePossible()
Description copied from interface:PersistentCollection
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.- Specified by:
isRowUpdatePossible
in interfacePersistentCollection<E>
- Returns:
true
if the row for each element is known
-
hasQueuedOperations
public final boolean hasQueuedOperations()
Description copied from interface:PersistentCollection
Does this instance have any "queued" operations?- Specified by:
hasQueuedOperations
in interfacePersistentCollection<E>
- Returns:
true
indicates there are pending, queued, delayed operations
-
queuedAdditionIterator
public final Iterator<E> queuedAdditionIterator()
Description copied from interface:PersistentCollection
Iterator over the "queued" additions- Specified by:
queuedAdditionIterator
in interfacePersistentCollection<E>
- Returns:
- The iterator
-
getQueuedOrphans
public final Collection<E> getQueuedOrphans(String entityName)
Description copied from interface:PersistentCollection
Get the "queued" orphans- Specified by:
getQueuedOrphans
in interfacePersistentCollection<E>
- Parameters:
entityName
- The name of the entity that makes up the elements- Returns:
- The orphaned elements
-
preInsert
public void preInsert(CollectionPersister persister) throws HibernateException
Description copied from interface:PersistentCollection
Called before inserting rows, to ensure that any surrogate keys are fully generated- Specified by:
preInsert
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persister- Throws:
HibernateException
-
afterRowInsert
public void afterRowInsert(CollectionPersister persister, Object entry, int i) throws HibernateException
Description copied from interface:PersistentCollection
Called after inserting a row, to fetch the natively generated id- Specified by:
afterRowInsert
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persisterentry
- The collection element just insertedi
- The element position/index- Throws:
HibernateException
-
getOrphans
public abstract Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException
Description copied from interface:PersistentCollection
get all "orphaned" elements- Specified by:
getOrphans
in interfacePersistentCollection<E>
- Parameters:
snapshot
- The snapshot stateentityName
- The name of the entity that are the elements of the collection- Returns:
- The orphans
- Throws:
HibernateException
-
getSession
public final SharedSessionContractImplementor getSession()
Get the session currently associated with this collection.- Returns:
- The session
-
getOrphans
protected static <E> Collection<E> getOrphans(Collection<E> oldElements, Collection<E> currentElements, String entityName, SharedSessionContractImplementor session) throws HibernateException
Given a collection of entity instances that used to belong to the collection, and a collection of instances that currently belong, return a collection of orphans- Throws:
HibernateException
-
identityRemove
public static void identityRemove(Collection<?> list, Object entityInstance, String entityName, SharedSessionContractImplementor session)
Removes entity entries that have an equal identifier with the incoming entity instance- Parameters:
list
- The list containing the entity instancesentityInstance
- The entity instance to match elements.entityName
- The entity namesession
- The session
-
getIdentifier
public Object getIdentifier(Object entry, int i)
Description copied from interface:PersistentCollection
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 theidbag
collection.- Specified by:
getIdentifier
in interfacePersistentCollection<E>
- Parameters:
entry
- The collection entry/elementi
- The assumed identifier (?)- Returns:
- The identifier value
-
getOwner
public @Nullable Object getOwner()
Description copied from interface:PersistentCollection
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.- Specified by:
getOwner
in interfacePersistentCollection<E>
- Returns:
- The owner
-
setOwner
public void setOwner(Object owner)
Description copied from interface:PersistentCollection
Set the reference to the owning entity- Specified by:
setOwner
in interfacePersistentCollection<E>
- Parameters:
owner
- The owner
-
-