Class MergeContext
MergeContext
is a specialized Map
implementation used by a
merge event listener
to keep track of each entity being merged and its corresponding managed result.
An entity to be merged may to be added to the MergeContext
before the
merge operation cascades to the entity.
- A merge entity (the
mergeEntity
method parameter) is an entity being merged viaSession.merge(Object mergeEntity)
. - A managed entity (the
managedEntity
method parameter) is the managed entity that is the result of merging an entity.
A merge entity can be transient, detached, or managed. If it is managed, then it is identical to its resulting managed entity.
If put(Object mergeEntity, Object managedEntity)
is called, and this
MergeContext
already contains an entry with a different entity as the
key, but with the same (managed entity) value, this means that multiple entity
representations for the same persistent entity are being merged. In this
situation, EntityCopyObserver.entityCopyDetected(Object managedEntity, Object mergeEntity1, Object mergeEntity2, EventSource)
is called to determine an appropriate resolution.
There are several restrictions.
- Methods that return collections (e.g.,
keySet()
,values()
,entrySet()
) return an unmodifiable view of the collection. - If
managedEntity
orput(Object mergeEntity, Object managedEntity, boolean isOperatedOn)
is executed and thisMergeContext
already contains a cross reference formergeEntity
, thenmanagedEntity
must be identical to the entity already associated withmergeEntity
. - If
putAll(Map map)
is executed, the previous restriction applies to each entry in theMap
. - The
remove(Object)
operation is not supported; the only way to remove data from aMergeContext
is by callingclear()
. - The map returned by
invertMap()
will only contain the "newest" (most recently added) managed-to-merge cross-reference to its merge entity.
The following method is intended to be used by an implementation of
MergeEventListener
to add a merge entity and its
corresponding managed entity to a MergeContext
and indicate if the merge
operation is being performed on the merge entity yet.
put(Object mergeEntity, Object managedEntity, boolean isOperatedOn)
The following method is intended to be used by a MergeEventListener
to
indicate whether the merge operation is being performed on a merge entity already
in the MergeContext
:
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the MergeContext.boolean
containsKey
(Object mergeEntity) Returns true if this MergeContext contains a cross-reference for the specified merge entity to a managed entity result.boolean
containsValue
(Object managedEntity) Returns true if this MergeContext contains a cross-reference from the specified managed entity to a merge entity.entrySet()
Returns an unmodifiable set view of the merge-to-managed entity cross-references contained in this MergeContext.Returns the managed entity associated with the specified merge Entity.Returns an unmodifiable map view of the managed-to-merge entity cross-references.boolean
isEmpty()
Returns true if this MergeContext contains no merge-to-managed entity cross-references.boolean
isOperatedOn
(Object mergeEntity) Returns true if the listener is performing the merge operation on the specified merge entity.keySet()
Returns an unmodifiable set view of the merge entities contained in this MergeContextAssociates the specified merge entity with the specified managed entity result in this MergeContext.Associates the specified merge entity with the specified managed entity in this MergeContext.void
Copies all of the mappings from the specified Map to this MergeContext.The remove operation is not supported.void
setOperatedOn
(Object mergeEntity, boolean isOperatedOn) Set flag to indicate if the listener is performing the merge operation on the specified merge entity.int
size()
Returns the number of merge-to-managed entity cross-references in this MergeContextvalues()
Returns an unmodifiable Set view of managed entities contained in this MergeContext.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
MergeContext
-
-
Method Details
-
clear
public void clear()Clears the MergeContext. -
containsKey
Returns true if this MergeContext contains a cross-reference for the specified merge entity to a managed entity result.- Specified by:
containsKey
in interfaceMap<Object,
Object> - Parameters:
mergeEntity
- must be non-null- Returns:
- true if this MergeContext contains a cross-reference for the specified merge entity
- Throws:
NullPointerException
- if mergeEntity is null
-
containsValue
Returns true if this MergeContext contains a cross-reference from the specified managed entity to a merge entity.- Specified by:
containsValue
in interfaceMap<Object,
Object> - Parameters:
managedEntity
- must be non-null- Returns:
- true if this MergeContext contains a cross-reference from the specified managed entity to a merge entity
- Throws:
NullPointerException
- if managedEntity is null
-
entrySet
Returns an unmodifiable set view of the merge-to-managed entity cross-references contained in this MergeContext. -
get
Returns the managed entity associated with the specified merge Entity.- Specified by:
get
in interfaceMap<Object,
Object> - Parameters:
mergeEntity
- the merge entity; must be non-null- Returns:
- the managed entity associated with the specified merge Entity
- Throws:
NullPointerException
- if mergeEntity is null
-
isEmpty
public boolean isEmpty()Returns true if this MergeContext contains no merge-to-managed entity cross-references. -
keySet
Returns an unmodifiable set view of the merge entities contained in this MergeContext -
put
Associates the specified merge entity with the specified managed entity result in this MergeContext. If this MergeContext already contains a cross-reference formergeEntity
when this method is called, thenmanagedEntity
must be the same as what is already associated withmergeEntity
.This method assumes that the merge process is not yet operating on
mergeEntity
. Later whenmergeEntity
enters the merge process,setOperatedOn(Object, boolean)
should be called.- Specified by:
put
in interfaceMap<Object,
Object> - Parameters:
mergeEntity
- the merge entity; must be non-nullmanagedEntity
- the managed entity result; must be non-null- Returns:
- previous managed entity associated with specified merge entity, or null if there was no mapping for mergeEntity.
- Throws:
NullPointerException
- if mergeEntity or managedEntity is nullIllegalArgumentException
- ifmanagedEntity
is not the same as the previous managed entity associated withmerge entity
IllegalStateException
- if internal cross-references are out of sync,
-
put
Associates the specified merge entity with the specified managed entity in this MergeContext. If this MergeContext already contains a cross-reference formergeEntity
when this method is called, thenmanagedEntity
must be the same as what is already associated withmergeEntity
.- Parameters:
mergeEntity
- the merge entity; must be non-nullmanagedEntity
- the managed entity; must be non-nullisOperatedOn
- indicates if the merge operation is performed on the mergeEntity.- Returns:
- previous managed entity associated with specified merge entity, or null if there was no mapping for mergeEntity.
- Throws:
NullPointerException
- if mergeEntity or managedEntity is nullIllegalArgumentException
- ifmanagedEntity
is not the same as the previous managed entity associated withmergeEntity
IllegalStateException
- if internal cross-references are out of sync,
-
putAll
Copies all of the mappings from the specified Map to this MergeContext. The key and value for each entry inmap
is subject to the same restrictions asput(Object mergeEntity, Object managedEntity)
. This method assumes that the merge process is not yet operating on any merge entity- Specified by:
putAll
in interfaceMap<Object,
Object> - Parameters:
map
- keys and values must be non-null- Throws:
NullPointerException
- if any key or value is nullIllegalArgumentException
- if a key inmap
was already in this MergeContext but associated value inmap
is different from the previous value in this MergeContext.IllegalStateException
- if internal cross-references are out of sync,
-
remove
The remove operation is not supported.- Specified by:
remove
in interfaceMap<Object,
Object> - Parameters:
mergeEntity
- the merge entity.- Throws:
UnsupportedOperationException
- if called.
-
size
public int size()Returns the number of merge-to-managed entity cross-references in this MergeContext -
values
Returns an unmodifiable Set view of managed entities contained in this MergeContext. -
isOperatedOn
Returns true if the listener is performing the merge operation on the specified merge entity.- Parameters:
mergeEntity
- the merge entity; must be non-null- Returns:
- true if the listener is performing the merge operation on the specified merge entity; false, if there is no mapping for mergeEntity.
- Throws:
NullPointerException
- if mergeEntity is null
-
setOperatedOn
Set flag to indicate if the listener is performing the merge operation on the specified merge entity.- Parameters:
mergeEntity
- must be non-null and this MergeContext must contain a cross-reference for mergeEntity to a managed entity- Throws:
NullPointerException
- if mergeEntity is nullIllegalStateException
- if this MergeContext does not contain a a cross-reference for mergeEntity
-
invertMap
Returns an unmodifiable map view of the managed-to-merge entity cross-references. The returned Map will contain a cross-reference from each managed entity to the most recently associated merge entity that was most recently put in the MergeContext.- Returns:
- an unmodifiable map view of the managed-to-merge entity cross-references.
- See Also:
-