Package org.hibernate.engine.internal
Class NaturalIdResolutionsImpl
- java.lang.Object
-
- org.hibernate.engine.internal.NaturalIdResolutionsImpl
-
- All Implemented Interfaces:
Serializable
,NaturalIdResolutions
public class NaturalIdResolutionsImpl extends Object implements NaturalIdResolutions, Serializable
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from interface org.hibernate.engine.spi.NaturalIdResolutions
INVALID_NATURAL_ID_REFERENCE
-
-
Constructor Summary
Constructors Constructor Description NaturalIdResolutionsImpl(StatefulPersistenceContext persistenceContext)
Constructs a NaturalIdXrefDelegate
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
cacheResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Caches a natural-id-to-identifier resolution.void
cacheResolutionFromLoad(Object id, Object naturalId, EntityMappingType entityDescriptor)
void
cleanupFromSynchronizations()
void
clear()
Clear the resolution cacheObject
findCachedIdByNaturalId(Object naturalId, EntityMappingType entityDescriptor)
Find the cached identifier for the given natural-idObject
findCachedNaturalIdById(Object id, EntityMappingType entityDescriptor)
Find the cached natural-id for the given identifierCollection<?>
getCachedPkResolutions(EntityMappingType entityDescriptor)
Find all the locally cached primary key cross-reference entries for the given entity.void
handleEviction(Object id, Object object, EntityMappingType entityDescriptor)
Called onSession.evict(java.lang.Object)
to give a chance to clean up natural-id cross refs.void
handleSynchronization(Object pk, Object entity, EntityMappingType entityDescriptor)
Part of the "load synchronization process".protected EntityPersister
locatePersisterForKey(EntityPersister persister)
It is only valid to define natural ids at the root of an entity hierarchy.void
manageLocalResolution(Object id, Object naturalIdValue, EntityMappingType entityDescriptor, CachedNaturalIdValueSource source)
Ensures that the necessary local cross-reference exists.void
manageSharedResolution(Object id, Object naturalId, Object previousNaturalId, EntityMappingType entityDescriptor, CachedNaturalIdValueSource source)
Ensures that the necessary cross-reference exists in the L2 cacheObject
removeLocalResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Removes any local cross-reference, returning the previously cached value if one.Object
removeResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Removes a natural-id-to-identifier resolution.void
removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Removes any cross-reference from the L2 cacheboolean
sameAsCached(EntityPersister persister, Object pk, Object naturalIdValues)
Are the naturals id values cached here (if any) for the given persister+pk combo the same as the given values?protected SharedSessionContractImplementor
session()
Access to the session (via the PersistenceContext) to which this delegate ultimately belongs.void
stashInvalidNaturalIdReference(EntityPersister persister, Object invalidNaturalIdValues)
As part of "load synchronization process", if a particular natural id is found to have changed we need to track its invalidity until after the next flush.void
unStashInvalidNaturalIdReferences()
Again, as part of "load synchronization process" we need to also be able to clear references to these known-invalid natural-ids after flush.protected void
validateNaturalId(EntityMappingType entityDescriptor, Object naturalIdValues)
Invariant validate of the natural id.
-
-
-
Constructor Detail
-
NaturalIdResolutionsImpl
public NaturalIdResolutionsImpl(StatefulPersistenceContext persistenceContext)
Constructs a NaturalIdXrefDelegate- Parameters:
persistenceContext
- The persistence context that owns this delegate
-
-
Method Detail
-
session
protected SharedSessionContractImplementor session()
Access to the session (via the PersistenceContext) to which this delegate ultimately belongs.- Returns:
- The session
-
cacheResolution
public boolean cacheResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Caches a natural-id-to-identifier resolution. Handles both the local (transactional) and shared (second-level) caches.- Specified by:
cacheResolution
in interfaceNaturalIdResolutions
- Returns:
true
if a new entry was actually added;false
otherwise.
-
cacheResolutionFromLoad
public void cacheResolutionFromLoad(Object id, Object naturalId, EntityMappingType entityDescriptor)
- Specified by:
cacheResolutionFromLoad
in interfaceNaturalIdResolutions
-
removeResolution
public Object removeResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Removes a natural-id-to-identifier resolution.Handles both the local (transactional) and shared (second-level) caches.
- Specified by:
removeResolution
in interfaceNaturalIdResolutions
- Returns:
- The cached values, if any. May be different from incoming values.
-
manageLocalResolution
public void manageLocalResolution(Object id, Object naturalIdValue, EntityMappingType entityDescriptor, CachedNaturalIdValueSource source)
Description copied from interface:NaturalIdResolutions
Ensures that the necessary local cross-reference exists. Specifically, this only effects the persistence-context cache, not the L2 cache- Specified by:
manageLocalResolution
in interfaceNaturalIdResolutions
-
removeLocalResolution
public Object removeLocalResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Removes any local cross-reference, returning the previously cached value if one.Again, this only effects the persistence-context cache, not the L2 cache
- Specified by:
removeLocalResolution
in interfaceNaturalIdResolutions
-
manageSharedResolution
public void manageSharedResolution(Object id, Object naturalId, Object previousNaturalId, EntityMappingType entityDescriptor, CachedNaturalIdValueSource source)
Description copied from interface:NaturalIdResolutions
Ensures that the necessary cross-reference exists in the L2 cache- Specified by:
manageSharedResolution
in interfaceNaturalIdResolutions
-
removeSharedResolution
public void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Removes any cross-reference from the L2 cache- Specified by:
removeSharedResolution
in interfaceNaturalIdResolutions
-
handleSynchronization
public void handleSynchronization(Object pk, Object entity, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Part of the "load synchronization process".Responsible for maintaining cross-reference entries when natural-id values were found to have changed.
Also responsible for tracking the old values as no longer valid until the next flush because otherwise going to the database would just re-pull the old values as valid. In this responsibility,
NaturalIdResolutions.cleanupFromSynchronizations()
is the inverse process called after flush to clean up those entries.- Specified by:
handleSynchronization
in interfaceNaturalIdResolutions
- See Also:
NaturalIdResolutions.cleanupFromSynchronizations()
-
cleanupFromSynchronizations
public void cleanupFromSynchronizations()
Description copied from interface:NaturalIdResolutions
The clean up process ofNaturalIdResolutions.handleSynchronization(java.lang.Object, java.lang.Object, org.hibernate.metamodel.mapping.EntityMappingType)
. Responsible for cleaning up the tracking of old values as no longer valid.- Specified by:
cleanupFromSynchronizations
in interfaceNaturalIdResolutions
-
handleEviction
public void handleEviction(Object id, Object object, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Called onSession.evict(java.lang.Object)
to give a chance to clean up natural-id cross refs.- Specified by:
handleEviction
in interfaceNaturalIdResolutions
-
sameAsCached
public boolean sameAsCached(EntityPersister persister, Object pk, Object naturalIdValues)
Are the naturals id values cached here (if any) for the given persister+pk combo the same as the given values?- Parameters:
persister
- The persister representing the entity type.pk
- The primary key valuenaturalIdValues
- The natural id value(s) to check- Returns:
true
if the given naturalIdValues match the current cached values;false
otherwise.
-
locatePersisterForKey
protected EntityPersister locatePersisterForKey(EntityPersister persister)
It is only valid to define natural ids at the root of an entity hierarchy. This method makes sure we are using the root persister.- Parameters:
persister
- The persister representing the entity type.- Returns:
- The root persister.
-
validateNaturalId
protected void validateNaturalId(EntityMappingType entityDescriptor, Object naturalIdValues)
Invariant validate of the natural id. Checks include- that the entity defines a natural id
- the number of natural id values matches the expected number
- Parameters:
entityDescriptor
- The entity type descriptornaturalIdValues
- The natural id values
-
findCachedNaturalIdById
public Object findCachedNaturalIdById(Object id, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Find the cached natural-id for the given identifier- Specified by:
findCachedNaturalIdById
in interfaceNaturalIdResolutions
- Returns:
- The cross-referenced natural-id values or
null
-
findCachedIdByNaturalId
public Object findCachedIdByNaturalId(Object naturalId, EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Find the cached identifier for the given natural-id- Specified by:
findCachedIdByNaturalId
in interfaceNaturalIdResolutions
- Returns:
- The cross-referenced primary key,
NaturalIdResolutions.INVALID_NATURAL_ID_REFERENCE
ornull
.
-
getCachedPkResolutions
public Collection<?> getCachedPkResolutions(EntityMappingType entityDescriptor)
Description copied from interface:NaturalIdResolutions
Find all the locally cached primary key cross-reference entries for the given entity.- Specified by:
getCachedPkResolutions
in interfaceNaturalIdResolutions
- Returns:
- The primary keys
-
stashInvalidNaturalIdReference
public void stashInvalidNaturalIdReference(EntityPersister persister, Object invalidNaturalIdValues)
As part of "load synchronization process", if a particular natural id is found to have changed we need to track its invalidity until after the next flush. This method lets the "load synchronization process" indicate when it has encountered such changes.- Parameters:
persister
- The persister representing the entity type.invalidNaturalIdValues
- The "old" natural id values.- See Also:
NaturalIdLoadAccess.setSynchronizationEnabled(boolean)
-
unStashInvalidNaturalIdReferences
public void unStashInvalidNaturalIdReferences()
Again, as part of "load synchronization process" we need to also be able to clear references to these known-invalid natural-ids after flush. This method exposes that capability.
-
clear
public void clear()
Clear the resolution cache
-
-