1010import org .hibernate .engine .spi .SharedSessionContractImplementor ;
1111import org .hibernate .engine .spi .Status ;
1212import org .hibernate .id .IdentifierGenerationException ;
13- import org .hibernate .loader .LoaderLogging ;
1413import org .hibernate .metamodel .mapping .EntityMappingType ;
15- import org .hibernate .metamodel .mapping .NaturalIdMapping ;
1614import org .hibernate .persister .entity .EntityPersister ;
1715
1816import java .util .Collection ;
1917
18+ import static org .hibernate .engine .internal .NaturalIdLogging .NATURAL_ID_LOGGER ;
19+
2020/**
2121 * @author Gavin King
2222 */
2323public class NaturalIdHelper {
24+
2425 public static String [] getNaturalIdPropertyNames (EntityPersister persister ) {
2526 final int [] naturalIdPropertyIndices = persister .getNaturalIdentifierProperties ();
2627 if ( naturalIdPropertyIndices == null ) {
@@ -43,58 +44,41 @@ public static void performAnyNeededCrossReferenceSynchronizations(
4344 boolean synchronizationEnabled ,
4445 EntityMappingType entityMappingType ,
4546 SharedSessionContractImplementor session ) {
46-
47- if ( !synchronizationEnabled ) {
48- // synchronization (this process) was disabled
49- return ;
50- }
51-
52- final NaturalIdMapping naturalIdMapping = entityMappingType .getNaturalIdMapping ();
53-
54- if ( !naturalIdMapping .isMutable () ) {
55- // only mutable natural-ids need this processing
56- return ;
57- }
58-
59- if ( ! session .isTransactionInProgress () ) {
60- // not in a transaction so skip synchronization
61- return ;
62- }
63-
64- final EntityPersister entityPersister = entityMappingType .getEntityPersister ();
65-
66- final PersistenceContext persistenceContext = session .getPersistenceContextInternal ();
67- final Collection <?> cachedPkResolutions =
68- persistenceContext .getNaturalIdResolutions ()
69- .getCachedPkResolutions ( entityPersister );
70- final boolean loggerDebugEnabled = LoaderLogging .LOADER_LOGGER .isDebugEnabled ();
71- for ( Object pk : cachedPkResolutions ) {
72- final EntityKey entityKey = session .generateEntityKey ( pk , entityPersister );
73- final Object entity = persistenceContext .getEntity ( entityKey );
74- final EntityEntry entry = persistenceContext .getEntry ( entity );
75-
76- if ( entry == null ) {
77- if ( loggerDebugEnabled ) {
78- LoaderLogging .LOADER_LOGGER .debugf (
79- "Cached natural-id/pk resolution linked to null EntityEntry in persistence context: %s#%s" ,
80- entityMappingType .getEntityName (),
81- pk
82- );
47+ // first check if synchronization (this process) was disabled
48+ if ( synchronizationEnabled
49+ // only mutable natural-ids need this processing
50+ && entityMappingType .getNaturalIdMapping ().isMutable ()
51+ // skip synchronization when not in a transaction
52+ && session .isTransactionInProgress () ) {
53+ final EntityPersister entityPersister = entityMappingType .getEntityPersister ();
54+ final PersistenceContext persistenceContext = session .getPersistenceContextInternal ();
55+ final Collection <?> cachedResolutions =
56+ persistenceContext .getNaturalIdResolutions ()
57+ .getCachedPkResolutions ( entityPersister );
58+ final boolean loggerDebugEnabled = NATURAL_ID_LOGGER .isDebugEnabled ();
59+ for ( Object id : cachedResolutions ) {
60+ final EntityKey entityKey = session .generateEntityKey ( id , entityPersister );
61+ final Object entity = persistenceContext .getEntity ( entityKey );
62+ final EntityEntry entry = persistenceContext .getEntry ( entity );
63+ if ( entry != null ) {
64+ if ( entry .requiresDirtyCheck ( entity )
65+ // MANAGED is the only status we care about here
66+ && entry .getStatus () == Status .MANAGED ) {
67+ persistenceContext .getNaturalIdResolutions ()
68+ .handleSynchronization ( id , entity , entityPersister );
69+ }
70+ }
71+ else {
72+ // no entry
73+ if ( loggerDebugEnabled ) {
74+ NATURAL_ID_LOGGER .debugf (
75+ "Cached natural-id/pk resolution linked to missing EntityEntry in persistence context: %s#%s" ,
76+ entityMappingType .getEntityName (),
77+ id
78+ );
79+ }
8380 }
84- continue ;
85- }
86-
87- if ( !entry .requiresDirtyCheck ( entity ) ) {
88- continue ;
89- }
90-
91- // MANAGED is the only status we care about here...
92- if ( entry .getStatus () != Status .MANAGED ) {
93- continue ;
9481 }
95-
96- persistenceContext .getNaturalIdResolutions ().handleSynchronization ( pk , entity , entityPersister );
9782 }
9883 }
99-
10084}
0 commit comments