10
10
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
11
11
import org .hibernate .engine .spi .Status ;
12
12
import org .hibernate .id .IdentifierGenerationException ;
13
- import org .hibernate .loader .LoaderLogging ;
14
13
import org .hibernate .metamodel .mapping .EntityMappingType ;
15
- import org .hibernate .metamodel .mapping .NaturalIdMapping ;
16
14
import org .hibernate .persister .entity .EntityPersister ;
17
15
18
16
import java .util .Collection ;
19
17
18
+ import static org .hibernate .engine .internal .NaturalIdLogging .NATURAL_ID_LOGGER ;
19
+
20
20
/**
21
21
* @author Gavin King
22
22
*/
23
23
public class NaturalIdHelper {
24
+
24
25
public static String [] getNaturalIdPropertyNames (EntityPersister persister ) {
25
26
final int [] naturalIdPropertyIndices = persister .getNaturalIdentifierProperties ();
26
27
if ( naturalIdPropertyIndices == null ) {
@@ -43,58 +44,41 @@ public static void performAnyNeededCrossReferenceSynchronizations(
43
44
boolean synchronizationEnabled ,
44
45
EntityMappingType entityMappingType ,
45
46
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
+ }
83
80
}
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 ;
94
81
}
95
-
96
- persistenceContext .getNaturalIdResolutions ().handleSynchronization ( pk , entity , entityPersister );
97
82
}
98
83
}
99
-
100
84
}
0 commit comments