Skip to content

Commit 14ff439

Browse files
committed
Big clean up around natural id stuff
motivated by work on logging cleanup
1 parent 01dcf9f commit 14ff439

File tree

11 files changed

+293
-385
lines changed

11 files changed

+293
-385
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdLogging.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ public interface NaturalIdLogging extends BasicLogger {
5252
void removingLocallyCachedNaturalIdResolution(String entityName, Object naturalId, Object id);
5353

5454
@LogMessage(level = TRACE)
55-
@Message(value = "Resolved natural key [%s] -> primary key [%s] resolution in session cache for [%s]:", id = 90004)
55+
@Message(value = "Resolved natural key [%s] -> primary key [%s] resolution in session cache for [%s]", id = 90004)
5656
void resolvedNaturalIdInSessionCache(Object naturalId, Object pk, String entityName);
5757

5858
@LogMessage(level = TRACE)
5959
@Message(value = "Found natural key [%s] -> primary key [%s] xref in second-level cache for [%s]", id = 90005)
6060
void foundNaturalIdInSecondLevelCache(Object naturalId, Object pk, String entityName);
61+
62+
@LogMessage(level = TRACE)
63+
@Message(value = "Retrieving primary key of %s from database for natural id: %s", id = 90006)
64+
void retrievingIdForNaturalId(String entityName, Object naturalId);
6165
}

hibernate-core/src/main/java/org/hibernate/internal/NaturalIdHelper.java

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1111
import org.hibernate.engine.spi.Status;
1212
import org.hibernate.id.IdentifierGenerationException;
13-
import org.hibernate.loader.LoaderLogging;
1413
import org.hibernate.metamodel.mapping.EntityMappingType;
15-
import org.hibernate.metamodel.mapping.NaturalIdMapping;
1614
import org.hibernate.persister.entity.EntityPersister;
1715

1816
import java.util.Collection;
1917

18+
import static org.hibernate.engine.internal.NaturalIdLogging.NATURAL_ID_LOGGER;
19+
2020
/**
2121
* @author Gavin King
2222
*/
2323
public 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

Comments
 (0)