Top Description Inners Fields Constructors Methods
org.hibernate.engine.internal

public Class NaturalIdXrefDelegate

extends Object
Class Inheritance
Imports
java.io.Serializable, java.util.ArrayList, .Arrays, .Collection, .List, .Map, java.util.concurrent.ConcurrentHashMap, org.hibernate.AssertionFailure, org.hibernate.cache.spi.access.NaturalIdDataAccess, org.hibernate.engine.spi.PersistenceContext, .SessionFactoryImplementor, .SharedSessionContractImplementor, org.hibernate.persister.entity.EntityPersister, org.hibernate.stat.internal.StatsHelper, org.hibernate.stat.spi.StatisticsImplementor, org.hibernate.type.Type, org.jboss.logging.Logger

Maintains a org.hibernate.engine.spi.PersistenceContext-level 2-way cross-reference (xref) between the identifiers and natural ids of entities associated with the PersistenceContext.

Most operations resolve the proper NaturalIdResolutionCache to use based on the persister and simply delegate calls there.

Author
Steve Ebersole

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
NaturalIdXrefDelegate.CachedNaturalId

Used to put natural id values into collections.

private static class
NaturalIdXrefDelegate.NaturalIdResolutionCache

Represents the persister-specific cross-reference cache.

Field Summary

Modifier and TypeField and Description
private static final Logger
private final ConcurrentHashMap<EntityPersister, NaturalIdXrefDelegate.NaturalIdResolutionCache>
private final StatefulPersistenceContext

Constructor Summary

AccessConstructor and Description
public
NaturalIdXrefDelegate(StatefulPersistenceContext
The persistence context that owns this delegate
persistenceContext
)

Constructs a NaturalIdXrefDelegate

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if a new entry was actually added; false otherwise.
cacheNaturalIdCrossReference
(EntityPersister
The persister representing the entity type.
persister
,
Serializable
The primary key value
pk
,
Object[]
The natural id value(s)
naturalIdValues
)

Creates needed cross-reference entries between the given primary (pk) and natural (naturalIdValues) key values for the given persister.

public void
clear()

Clear the resolution cache

public Object[]

Returns:

The corresponding cross-referenced natural id values, or null if none
findCachedNaturalId
(EntityPersister
The persister representing the entity type.
persister
,
Serializable
The entity primary key
pk
)

Given a persister and primary key, find the locally cross-referenced natural id.

public Serializable

Returns:

The corresponding cross-referenced primary key, PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE, or null if none
findCachedNaturalIdResolution
(EntityPersister
The persister representing the entity type.
persister
,
Object[]
The natural id value(s)
naturalIdValues
)

Given a persister and natural-id value(s), find the locally cross-referenced primary key.

public Collection<Serializable>

Returns:

The primary keys
getCachedPkResolutions
(EntityPersister
The persister representing the entity type.
persister
)

Return all locally cross-referenced primary keys for the given persister.

protected EntityPersister

Returns:

The root persister.
locatePersisterForKey
(EntityPersister
The persister representing the entity type.
persister
)

It is only valid to define natural ids at the root of an entity hierarchy.

public Object[]

Returns:

The cached values, if any. May be different from incoming values.
removeNaturalIdCrossReference
(EntityPersister
The persister representing the entity type.
persister
,
Serializable
The primary key value
pk
,
Object[]
The natural id value(s)
naturalIdValues
)

Handle removing cross reference entries for the given natural-id/pk combo

public boolean

Returns:

true if the given naturalIdValues match the current cached values; false otherwise.
sameAsCached
(EntityPersister
The persister representing the entity type.
persister
,
Serializable
The primary key value
pk
,
Object[]
The natural id value(s) to check
naturalIdValues
)

Are the naturals id values cached here (if any) for the given persister+pk combo the same as the given values?

protected SharedSessionContractImplementor

Returns:

The session
session
()

Access to the session (via the PersistenceContext) to which this delegate ultimately belongs.

public void
stashInvalidNaturalIdReference(EntityPersister
The persister representing the entity type.
persister
,
Object[]
The "old" natural id values.
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.

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.

protected void
validateNaturalId(EntityPersister
The persister representing the entity type.
persister
,
Object[]
The natural id values
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
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LOGback to summary
private static final Logger LOG
naturalIdResolutionCacheMapback to summary
private final ConcurrentHashMap<EntityPersister, NaturalIdXrefDelegate.NaturalIdResolutionCache> naturalIdResolutionCacheMap
persistenceContextback to summary
private final StatefulPersistenceContext persistenceContext

Constructor Detail

NaturalIdXrefDelegateback to summary
public NaturalIdXrefDelegate(StatefulPersistenceContext persistenceContext)

Constructs a NaturalIdXrefDelegate

Parameters
persistenceContext:StatefulPersistenceContext

The persistence context that owns this delegate

Method Detail

cacheNaturalIdCrossReferenceback to summary
public boolean cacheNaturalIdCrossReference(EntityPersister persister, Serializable pk, Object[] naturalIdValues)

Creates needed cross-reference entries between the given primary (pk) and natural (naturalIdValues) key values for the given persister. Returns an indication of whether entries were actually made. If those values already existed as an entry, false would be returned here.

Parameters
persister:EntityPersister

The persister representing the entity type.

pk:Serializable

The primary key value

naturalIdValues:Object[]

The natural id value(s)

Returns:boolean

true if a new entry was actually added; false otherwise.

clearback to summary
public void clear()

Clear the resolution cache

findCachedNaturalIdback to summary
public Object[] findCachedNaturalId(EntityPersister persister, Serializable pk)

Given a persister and primary key, find the locally cross-referenced natural id.

Parameters
persister:EntityPersister

The persister representing the entity type.

pk:Serializable

The entity primary key

Returns:Object[]

The corresponding cross-referenced natural id values, or null if none

findCachedNaturalIdResolutionback to summary
public Serializable findCachedNaturalIdResolution(EntityPersister persister, Object[] naturalIdValues)

Given a persister and natural-id value(s), find the locally cross-referenced primary key. Will return PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE if the given natural ids are known to be invalid (see stashInvalidNaturalIdReference).

Parameters
persister:EntityPersister

The persister representing the entity type.

naturalIdValues:Object[]

The natural id value(s)

Returns:Serializable

The corresponding cross-referenced primary key, PersistenceContext.NaturalIdHelper#INVALID_NATURAL_ID_REFERENCE, or null if none

getCachedPkResolutionsback to summary
public Collection<Serializable> getCachedPkResolutions(EntityPersister persister)

Return all locally cross-referenced primary keys for the given persister. Used as part of load synchronization process.

Parameters
persister:EntityPersister

The persister representing the entity type.

Returns:Collection<Serializable>

The primary keys

See Also
org.hibernate.NaturalIdLoadAccess#setSynchronizationEnabled
locatePersisterForKeyback to summary
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:EntityPersister

The persister representing the entity type.

Returns:EntityPersister

The root persister.

removeNaturalIdCrossReferenceback to summary
public Object[] removeNaturalIdCrossReference(EntityPersister persister, Serializable pk, Object[] naturalIdValues)

Handle removing cross reference entries for the given natural-id/pk combo

Parameters
persister:EntityPersister

The persister representing the entity type.

pk:Serializable

The primary key value

naturalIdValues:Object[]

The natural id value(s)

Returns:Object[]

The cached values, if any. May be different from incoming values.

sameAsCachedback to summary
public boolean sameAsCached(EntityPersister persister, Serializable 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:EntityPersister

The persister representing the entity type.

pk:Serializable

The primary key value

naturalIdValues:Object[]

The natural id value(s) to check

Returns:boolean

true if the given naturalIdValues match the current cached values; false otherwise.

sessionback to summary
protected SharedSessionContractImplementor session()

Access to the session (via the PersistenceContext) to which this delegate ultimately belongs.

Returns:SharedSessionContractImplementor

The session

stashInvalidNaturalIdReferenceback to summary
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:EntityPersister

The persister representing the entity type.

invalidNaturalIdValues:Object[]

The "old" natural id values.

See Also
org.hibernate.NaturalIdLoadAccess#setSynchronizationEnabled
unStashInvalidNaturalIdReferencesback to summary
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.

validateNaturalIdback to summary
protected void validateNaturalId(EntityPersister persister, 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
persister:EntityPersister

The persister representing the entity type.

naturalIdValues:Object[]

The natural id values

org.hibernate.engine.internal back to summary

private Class NaturalIdXrefDelegate.CachedNaturalId

extends Object
implements Serializable
Class Inheritance
  • java.lang.Object
  • org.hibernate.engine.internal.NaturalIdXrefDelegate.CachedNaturalId
All Implemented Interfaces
java.io.Serializable

Used to put natural id values into collections. Useful mainly to apply equals/hashCode implementations.

Field Summary

Modifier and TypeField and Description
private int
private final Type[]
private final EntityPersister
private final Object[]

Constructor Summary

AccessConstructor and Description
public
CachedNaturalId(EntityPersister persister, Object[] values)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public Object[]
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
private boolean
isSame(Object[] otherValues)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

hashCodeback to summary
private int hashCode
naturalIdTypesback to summary
private final Type[] naturalIdTypes
persisterback to summary
private final EntityPersister persister
valuesback to summary
private final Object[] values

Constructor Detail

CachedNaturalIdback to summary
public CachedNaturalId(EntityPersister persister, Object[] values)

Method Detail

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getValuesback to summary
public Object[] getValues()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isSameback to summary
private boolean isSame(Object[] otherValues)
org.hibernate.engine.internal back to summary

private Class NaturalIdXrefDelegate.NaturalIdResolutionCache

extends Object
implements Serializable
Class Inheritance
  • java.lang.Object
  • org.hibernate.engine.internal.NaturalIdXrefDelegate.NaturalIdResolutionCache
All Implemented Interfaces
java.io.Serializable

Represents the persister-specific cross-reference cache.

Field Summary

Modifier and TypeField and Description
private List<NaturalIdXrefDelegate.CachedNaturalId>
private Map<NaturalIdXrefDelegate.CachedNaturalId, Serializable>
private final EntityPersister
private Map<Serializable, NaturalIdXrefDelegate.CachedNaturalId>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
cache(Serializable pk, Object[] naturalIdValues)

public boolean
public EntityPersister
public boolean
sameAsCached(Serializable pk, Object[] naturalIdValues)

public void
stashInvalidNaturalIdReference(Object[] invalidNaturalIdValues)

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

invalidNaturalIdListback to summary
private List<NaturalIdXrefDelegate.CachedNaturalId> invalidNaturalIdList
naturalIdToPkMapback to summary
private Map<NaturalIdXrefDelegate.CachedNaturalId, Serializable> naturalIdToPkMap
persisterback to summary
private final EntityPersister persister
pkToNaturalIdMapback to summary
private Map<Serializable, NaturalIdXrefDelegate.CachedNaturalId> pkToNaturalIdMap

Constructor Detail

NaturalIdResolutionCacheback to summary
private NaturalIdResolutionCache(EntityPersister persister)

Method Detail

cacheback to summary
public boolean cache(Serializable pk, Object[] naturalIdValues)
containsInvalidNaturalIdReferenceback to summary
public boolean containsInvalidNaturalIdReference(Object[] naturalIdValues)
getPersisterback to summary
public EntityPersister getPersister()
sameAsCachedback to summary
public boolean sameAsCached(Serializable pk, Object[] naturalIdValues)
stashInvalidNaturalIdReferenceback to summary
public void stashInvalidNaturalIdReference(Object[] invalidNaturalIdValues)
unStashInvalidNaturalIdReferencesback to summary
public void unStashInvalidNaturalIdReferences()