diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java index 29dee5d946..906de573de 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java @@ -55,7 +55,7 @@ public class NaturalIdXrefDelegate { private static final Logger LOG = Logger.getLogger( NaturalIdXrefDelegate.class ); private final StatefulPersistenceContext persistenceContext; - private final Map naturalIdResolutionCacheMap = new ConcurrentHashMap(); + private final ConcurrentHashMap naturalIdResolutionCacheMap = new ConcurrentHashMap(); /** * Constructs a NaturalIdXrefDelegate @@ -92,7 +92,10 @@ public class NaturalIdXrefDelegate { NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get( persister ); if ( entityNaturalIdResolutionCache == null ) { entityNaturalIdResolutionCache = new NaturalIdResolutionCache( persister ); - naturalIdResolutionCacheMap.put( persister, entityNaturalIdResolutionCache ); + NaturalIdResolutionCache previousInstance = naturalIdResolutionCacheMap.putIfAbsent( persister, entityNaturalIdResolutionCache ); + if ( previousInstance != null ) { + entityNaturalIdResolutionCache = previousInstance; + } } return entityNaturalIdResolutionCache.cache( pk, naturalIdValues ); } @@ -279,7 +282,10 @@ public class NaturalIdXrefDelegate { if ( entityNaturalIdResolutionCache == null ) { entityNaturalIdResolutionCache = new NaturalIdResolutionCache( persister ); - naturalIdResolutionCacheMap.put( persister, entityNaturalIdResolutionCache ); + NaturalIdResolutionCache existingCache = naturalIdResolutionCacheMap.putIfAbsent( persister, entityNaturalIdResolutionCache ); + if ( existingCache != null ) { + entityNaturalIdResolutionCache = existingCache; + } } entityNaturalIdResolutionCache.pkToNaturalIdMap.put( pk, cachedNaturalId );