Package org.hibernate.collection.spi
Class PersistentBag<E>
- java.lang.Object
-
- org.hibernate.collection.spi.AbstractPersistentCollection<E>
-
- org.hibernate.collection.spi.PersistentBag<E>
-
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,List<E>
,LazyInitializable
,PersistentCollection<E>
@Incubating public class PersistentBag<E> extends AbstractPersistentCollection<E> implements List<E>
An unordered, un-keyed collection that can contain the same element multiple times. The Java Collections Framework, curiously, has noBag
interface. It is, however, common to useList
s to represent a collection with bag semantics, so Hibernate follows this practice.- See Also:
- Serialized Form
- API Note:
- Incubating in terms of making this non-internal. These contracts will be getting cleaned up in following releases.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.hibernate.collection.spi.AbstractPersistentCollection
AbstractPersistentCollection.AbstractValueDelayedOperation, AbstractPersistentCollection.DelayedOperation<E>, AbstractPersistentCollection.IteratorProxy<E>, AbstractPersistentCollection.LazyInitializationWork<T>, AbstractPersistentCollection.ListIteratorProxy, AbstractPersistentCollection.ListProxy, AbstractPersistentCollection.SetProxy<E>, AbstractPersistentCollection.ValueDelayedOperation<E>
-
-
Field Summary
Fields Modifier and Type Field Description protected List<E>
bag
-
Fields inherited from class org.hibernate.collection.spi.AbstractPersistentCollection
elementRemoved, UNKNOWN
-
-
Constructor Summary
Constructors Constructor Description PersistentBag()
Constructs a PersistentBag.PersistentBag(SharedSessionContractImplementor session)
Constructs a PersistentBagPersistentBag(SharedSessionContractImplementor session, Collection<E> coll)
Constructs a PersistentBag
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int i, E o)
boolean
add(E object)
boolean
addAll(int i, Collection<? extends E> c)
boolean
addAll(Collection<? extends E> values)
void
clear()
boolean
contains(Object object)
boolean
containsAll(Collection<?> c)
Object
disassemble(CollectionPersister persister)
Disassemble the collection to get it ready for the cacheboolean
empty()
Is the collection empty? (don't try to initialize the collection)Iterator<E>
entries(CollectionPersister persister)
Iterate all collection entries, during update of the databaseboolean
entryExists(Object entry, int i)
Does the given element/entry exist in the collection?boolean
equals(Object obj)
Bag does not respect the collection API and do an JVM instance comparison to do the equals.boolean
equalsSnapshot(CollectionPersister persister)
Does the current state exactly match the snapshot?E
get(int i)
Iterator<?>
getDeletes(CollectionPersister persister, boolean indexIsFormula)
Get all the elements that need deletingObject
getElement(Object entry)
Get the value of the given collection entry.Object
getIndex(Object entry, int i, CollectionPersister persister)
Get the index of the given collection entryCollection<E>
getOrphans(Serializable snapshot, String entityName)
get all "orphaned" elementsSerializable
getSnapshot(CollectionPersister persister)
Return a new snapshot of the current state of the collectionObject
getSnapshotElement(Object entry, int i)
Get the snapshot value of the given collection entryint
hashCode()
int
indexOf(Object o)
void
initializeEmptyCollection(CollectionPersister persister)
void
initializeFromCache(CollectionPersister collectionDescriptor, Object disassembled, Object owner)
Read the state of the collection from a disassembled cached valuevoid
injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState)
Inject the state loaded for a collection instance.boolean
isDirectlyProvidedCollection(Object collection)
Wascollection
provided directly to this PersistentCollection (i.e., provided as an argument to a constructor)?boolean
isEmpty()
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
isSnapshotEmpty(Serializable snapshot)
Is the snapshot empty?boolean
isWrapper(Object collection)
Is this the wrapper for the given collection instance?Iterator<E>
iterator()
int
lastIndexOf(Object o)
ListIterator<E>
listIterator()
ListIterator<E>
listIterator(int i)
boolean
needsInserting(Object entry, int i, Type elemType)
Do we need to insert this element?boolean
needsRecreate(CollectionPersister persister)
Do we need to completely recreate this collection when it changes?boolean
needsUpdating(Object entry, int i, Type elemType)
Do we need to update this element?int
occurrences(Object o)
Count how many times the given object occurs in the elementsE
remove(int i)
boolean
remove(Object o)
boolean
removeAll(Collection<?> c)
boolean
retainAll(Collection<?> c)
E
set(int i, E o)
int
size()
List<E>
subList(int start, int end)
Object[]
toArray()
<A> A[]
toArray(A[] a)
String
toString()
-
Methods inherited from class org.hibernate.collection.spi.AbstractPersistentCollection
afterInitialize, afterRowInsert, beforeInitialize, beginRead, clearDirty, clearOperationQueue, dirty, elementByIndex, elementExists, endRead, forceInitialization, getCachedSize, getIdentifier, getKey, getOrphans, getOwner, getQueuedOrphans, getRole, getSession, getSize, getSnapshot, getStoredSnapshot, getValue, hasQueuedOperations, identityRemove, initialize, isClearQueueEnabled, isConnectedToSession, isDirectlyAccessible, isDirty, isElementRemoved, isInitialized, isInitializing, isInverseCollection, isInverseCollectionNoOrphanDelete, isInverseOneToManyOrNoOrphanDelete, isOperationQueueEnabled, isPutQueueEnabled, isUnreferenced, performQueuedOperations, postAction, preInsert, prepareForPossibleLoadingOutsideTransaction, queuedAdditionIterator, queueOperation, read, readElementByIndex, readElementExistence, readIndexExistence, readSize, replaceQueuedOperationValues, setCurrentSession, setDirectlyAccessible, setInitialized, setOwner, setSnapshot, unsetSession, wasInitialized, write
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Methods inherited from interface org.hibernate.collection.spi.PersistentCollection
includeInInsert, includeInRecreate, isNewlyInstantiated, needsUpdating, render
-
-
-
-
Constructor Detail
-
PersistentBag
public PersistentBag()
Constructs a PersistentBag. Needed for SOAP libraries, etc
-
PersistentBag
public PersistentBag(SharedSessionContractImplementor session)
Constructs a PersistentBag- Parameters:
session
- The session
-
PersistentBag
public PersistentBag(SharedSessionContractImplementor session, Collection<E> coll)
Constructs a PersistentBag- Parameters:
session
- The sessioncoll
- The base elements.
-
-
Method Detail
-
isWrapper
public boolean isWrapper(Object collection)
Description copied from interface:PersistentCollection
Is this the wrapper for the given collection instance?- Specified by:
isWrapper
in interfacePersistentCollection<E>
- Parameters:
collection
- The collection to check whether this is wrapping it- Returns:
true
if this is a wrapper around that given collection instance.
-
isDirectlyProvidedCollection
public boolean isDirectlyProvidedCollection(Object collection)
Description copied from interface:PersistentCollection
Wascollection
provided 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.
- Specified by:
isDirectlyProvidedCollection
in interfacePersistentCollection<E>
- Parameters:
collection
- The collection- Returns:
- true, if
collection
was provided directly to this PersistentCollection; false, otherwise.
-
empty
public boolean empty()
Description copied from interface:PersistentCollection
Is the collection empty? (don't try to initialize the collection)- Specified by:
empty
in interfacePersistentCollection<E>
- Specified by:
empty
in classAbstractPersistentCollection<E>
- Returns:
false
if the collection is non-empty;true
otherwise.
-
entries
public Iterator<E> entries(CollectionPersister persister)
Description copied from interface:PersistentCollection
Iterate all collection entries, during update of the database- Specified by:
entries
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persister.- Returns:
- The iterator
-
injectLoadedState
public void injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState)
Description copied from interface:PersistentCollection
Inject the state loaded for a collection instance.- Specified by:
injectLoadedState
in interfacePersistentCollection<E>
-
equalsSnapshot
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException
Description copied from interface:PersistentCollection
Does the current state exactly match the snapshot?- Specified by:
equalsSnapshot
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persister- Returns:
true
if the current state and the snapshot state match.- Throws:
HibernateException
-
isSnapshotEmpty
public boolean isSnapshotEmpty(Serializable snapshot)
Description copied from interface:PersistentCollection
Is the snapshot empty?- Specified by:
isSnapshotEmpty
in interfacePersistentCollection<E>
- Parameters:
snapshot
- The snapshot to check- Returns:
true
if the given snapshot is empty
-
getSnapshot
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException
Description copied from interface:PersistentCollection
Return a new snapshot of the current state of the collection- Specified by:
getSnapshot
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persister- Returns:
- The snapshot
- Throws:
HibernateException
-
getOrphans
public Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException
Description copied from interface:PersistentCollection
get all "orphaned" elements- Specified by:
getOrphans
in interfacePersistentCollection<E>
- Specified by:
getOrphans
in classAbstractPersistentCollection<E>
- Parameters:
snapshot
- The snapshot stateentityName
- The name of the entity that are the elements of the collection- Returns:
- The orphans
- Throws:
HibernateException
-
initializeEmptyCollection
public void initializeEmptyCollection(CollectionPersister persister)
- Specified by:
initializeEmptyCollection
in interfacePersistentCollection<E>
-
disassemble
public Object disassemble(CollectionPersister persister)
Description copied from interface:PersistentCollection
Disassemble the collection to get it ready for the cache- Specified by:
disassemble
in interfacePersistentCollection<E>
- Parameters:
persister
- The collection persister- Returns:
- The disassembled state
-
initializeFromCache
public void initializeFromCache(CollectionPersister collectionDescriptor, Object disassembled, Object owner) throws HibernateException
Description copied from interface:PersistentCollection
Read the state of the collection from a disassembled cached value- Specified by:
initializeFromCache
in interfacePersistentCollection<E>
- Parameters:
collectionDescriptor
- The collection persisterdisassembled
- The disassembled cached stateowner
- The collection owner- Throws:
HibernateException
-
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>
- Overrides:
needsRecreate
in classAbstractPersistentCollection<E>
- Parameters:
persister
- The collection persister- Returns:
true
if a change requires a recreate.
-
getDeletes
public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException
Description copied from interface:PersistentCollection
Get all the elements that need deleting- Specified by:
getDeletes
in interfacePersistentCollection<E>
- 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
- Throws:
HibernateException
-
needsInserting
public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException
Description copied from interface:PersistentCollection
Do we need to insert this element?- Specified by:
needsInserting
in interfacePersistentCollection<E>
- Parameters:
entry
- The collection element to checki
- The index (for indexed collections)elemType
- The type for the element- Returns:
true
if the element needs inserting- Throws:
HibernateException
-
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>
- Overrides:
isRowUpdatePossible
in classAbstractPersistentCollection<E>
- Returns:
true
if the row for each element is known
-
needsUpdating
public boolean needsUpdating(Object entry, int i, Type elemType)
Description copied from interface:PersistentCollection
Do we need to update this element?- Specified by:
needsUpdating
in interfacePersistentCollection<E>
- Parameters:
entry
- The collection element to checki
- The index (for indexed collections)elemType
- The type for the element- Returns:
true
if the element needs updating
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(Object object)
-
toArray
public Object[] toArray()
-
toArray
public <A> A[] toArray(A[] a)
-
add
public boolean add(E object)
-
remove
public boolean remove(Object o)
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
-
addAll
public boolean addAll(Collection<? extends E> values)
-
removeAll
public boolean removeAll(Collection<?> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
clear
public void clear()
-
getIndex
public Object getIndex(Object entry, int i, CollectionPersister persister)
Description copied from interface:PersistentCollection
Get the index of the given collection entry- Specified by:
getIndex
in interfacePersistentCollection<E>
- Parameters:
entry
- The collection entry/elementi
- The assumed indexpersister
- it was more elegant before we added this...- Returns:
- The index value
-
getElement
public Object getElement(Object entry)
Description copied from interface:PersistentCollection
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.- Specified by:
getElement
in interfacePersistentCollection<E>
- 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
public Object getSnapshotElement(Object entry, int i)
Description copied from interface:PersistentCollection
Get the snapshot value of the given collection entry- Specified by:
getSnapshotElement
in interfacePersistentCollection<E>
- Parameters:
entry
- The entryi
- The index- Returns:
- The snapshot state for that element
-
occurrences
public int occurrences(Object o)
Count how many times the given object occurs in the elements- Parameters:
o
- The object to check- Returns:
- The number of occurrences.
-
addAll
public boolean addAll(int i, Collection<? extends E> c)
-
lastIndexOf
public int lastIndexOf(Object o)
- Specified by:
lastIndexOf
in interfaceList<E>
-
listIterator
public ListIterator<E> listIterator()
- Specified by:
listIterator
in interfaceList<E>
-
listIterator
public ListIterator<E> listIterator(int i)
- Specified by:
listIterator
in interfaceList<E>
-
entryExists
public boolean entryExists(Object entry, int i)
Description copied from interface:PersistentCollection
Does the given element/entry exist in the collection?- Specified by:
entryExists
in interfacePersistentCollection<E>
- Parameters:
entry
- The object to check if it exists as a collection elementi
- Unused- Returns:
true
if the given entry is a collection element
-
equals
public boolean equals(Object obj)
Bag does not respect the collection API and do an JVM instance comparison to do the equals. The semantic is broken not to have to initialize a collection for a simple equals() operation.
-
-