Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.hibernate.action.spi.AfterTransactionCompletionProcess;
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.access.CollectionDataAccess;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.ComparableExecutable;
Expand Down Expand Up @@ -69,11 +68,11 @@ public void afterDeserialize(EventSource session) {

@Override
public final void beforeExecutions() throws CacheException {
// we need to obtain the lock before any actions are executed, since this may be an inverse="true"
// We need to obtain the lock before any actions are executed, since this may be an inverse="true"
// bidirectional association, and it is one of the earlier entity actions which actually updates
// the database (this action is responsible for second-level cache invalidation only)
// the database. This action is responsible for second-level cache invalidation only.
if ( persister.hasCache() ) {
final CollectionDataAccess cache = persister.getCacheAccessStrategy();
final var cache = persister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(
key,
persister,
Expand Down Expand Up @@ -108,16 +107,9 @@ protected final CollectionPersister getPersister() {
}

protected final Object getKey() {
Object finalKey = key;
if ( key instanceof DelayedPostInsertIdentifier ) {
// need to look it up from the persistence-context
finalKey = session.getPersistenceContextInternal().getEntry( collection.getOwner() ).getId();
// if ( finalKey == key ) {
// we may be screwed here since the collection action is about to execute
// and we do not know the final owner key value
// }
}
return finalKey;
return key instanceof DelayedPostInsertIdentifier
? session.getPersistenceContextInternal().getEntry( collection.getOwner() ).getId()
: key;
}

@Override
Expand Down Expand Up @@ -177,7 +169,7 @@ private CacheCleanupProcess(Object key, CollectionPersister persister, SoftLock

@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
final CollectionDataAccess cache = persister.getCacheAccessStrategy();
final var cache = persister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(
key,
persister,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public DelayedPostInsertIdentifier() {
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
public boolean equals(Object object) {
if ( this == object ) {
return true;
}
else if ( !(o instanceof DelayedPostInsertIdentifier that) ) {
else if ( !(object instanceof DelayedPostInsertIdentifier that) ) {
return false;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,7 @@ public void execute() throws HibernateException {

final boolean veto = isInstanceLoaded() && preDelete();

final var naturalIdMapping = persister.getNaturalIdMapping();
if ( naturalIdMapping != null ) {
naturalIdValues = session.getPersistenceContextInternal().getNaturalIdResolutions()
.removeLocalResolution(
getId(),
naturalIdMapping.extractNaturalIdFromEntityState( state ),
persister
);
}
handleNaturalIdResolutions( persister, session );

final Object ck = lockCacheItem();

Expand Down Expand Up @@ -145,6 +137,18 @@ public void execute() throws HibernateException {
}
}

private void handleNaturalIdResolutions(EntityPersister persister, EventSource session) {
final var naturalIdMapping = persister.getNaturalIdMapping();
if ( naturalIdMapping != null ) {
naturalIdValues = session.getPersistenceContextInternal().getNaturalIdResolutions()
.removeLocalResolution(
getId(),
naturalIdMapping.extractNaturalIdFromEntityState( state ),
persister
);
}
}

protected Object getCurrentVersion() {
final var persister = getPersister();
return persister.isVersionPropertyGenerated()
Expand Down Expand Up @@ -176,7 +180,7 @@ protected void postDeleteLoaded(
persistenceContext.removeEntityHolder( key );
removeCacheItem( ck );
persistenceContext.getNaturalIdResolutions()
.removeSharedResolution( id, naturalIdValues, persister, true);
.removeSharedResolution( id, naturalIdValues, persister, true );
postDelete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ protected void putCacheIfNecessary() {
final var session = getSession();
if ( isCachePutEnabled( persister, session ) ) {
final var factory = session.getFactory();
final var ce = persister.buildCacheEntry( getInstance(), getState(), version, session );
cacheEntry = persister.getCacheEntryStructure().structure( ce );
final var cacheEntry = persister.buildCacheEntry( getInstance(), getState(), version, session );
this.cacheEntry = persister.getCacheEntryStructure().structure( cacheEntry );
final var cache = persister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey( getId(), persister, factory, session.getTenantIdentifier() );
final boolean put = cacheInsert( persister, ck );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.engine.spi.CachedNaturalIdValueSource;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.event.monitor.spi.EventMonitor;
Expand Down Expand Up @@ -82,13 +83,15 @@ public EntityUpdateAction(
this.hasDirtyCollection = hasDirtyCollection;
this.rowId = rowId;

this.naturalIdMapping = persister.getNaturalIdMapping();
naturalIdMapping = persister.getNaturalIdMapping();
if ( naturalIdMapping == null ) {
previousNaturalIdValues = null;
}
else {
previousNaturalIdValues =
determinePreviousNaturalIdValues( persister, naturalIdMapping, id, previousState, session );
previousState == null
? session.getPersistenceContextInternal().getNaturalIdSnapshot( id, persister )
: naturalIdMapping.extractNaturalIdFromEntityState( previousState );
session.getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution(
id,
naturalIdMapping.extractNaturalIdFromEntityState( state ),
Expand All @@ -98,17 +101,6 @@ public EntityUpdateAction(
}
}

private static Object determinePreviousNaturalIdValues(
EntityPersister persister,
NaturalIdMapping naturalIdMapping,
Object id,
Object[] previousState,
SharedSessionContractImplementor session) {
return previousState == null
? session.getPersistenceContextInternal().getNaturalIdSnapshot( id, persister )
: naturalIdMapping.extractNaturalIdFromEntityState( previousState );
}

protected Object[] getState() {
return state;
}
Expand Down Expand Up @@ -178,14 +170,15 @@ public void execute() throws HibernateException {
finally {
eventMonitor.completeEntityUpdateEvent( event, id, persister.getEntityName(), success, session );
}
final var entry = session.getPersistenceContextInternal().getEntry( instance );
final var persistenceContext = session.getPersistenceContextInternal();
final var entry = persistenceContext.getEntry( instance );
if ( entry == null ) {
throw new AssertionFailure( "possible non thread safe access to session" );
}
handleGeneratedProperties( entry, generatedValues );
handleDeleted( entry );
updateCacheItem( previousVersion, ck, entry );
handleNaturalIdResolutions( persister, session, id );
handleNaturalIdResolutions( persister, persistenceContext, id );
postUpdate();

final var statistics = session.getFactory().getStatistics();
Expand All @@ -195,9 +188,9 @@ public void execute() throws HibernateException {
}
}

protected void handleNaturalIdResolutions(EntityPersister persister, SharedSessionContractImplementor session, Object id) {
protected void handleNaturalIdResolutions(EntityPersister persister, PersistenceContext context, Object id) {
if ( naturalIdMapping != null ) {
session.getPersistenceContextInternal().getNaturalIdResolutions().manageSharedResolution(
context.getNaturalIdResolutions().manageSharedResolution(
id,
naturalIdMapping.extractNaturalIdFromEntityState( state ),
previousNaturalIdValues,
Expand All @@ -216,8 +209,8 @@ protected void updateCacheItem(Object previousVersion, Object ck, EntityEntry en
}
else if ( session.getCacheMode().isPutEnabled() ) {
//TODO: inefficient if that cache is just going to ignore the updated state!
final var ce = persister.buildCacheEntry( getInstance(), state, nextVersion, getSession() );
cacheEntry = persister.getCacheEntryStructure().structure( ce );
final var cacheEntry = persister.buildCacheEntry( getInstance(), state, nextVersion, getSession() );
this.cacheEntry = persister.getCacheEntryStructure().structure( cacheEntry );
final boolean put = updateCache( persister, previousVersion, ck );

final var statistics = session.getFactory().getStatistics();
Expand Down Expand Up @@ -279,9 +272,9 @@ protected void handleDeleted(EntityEntry entry) {
final boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned()
&& entityMetamodel.getOptimisticLockStyle().isAllOrDirty();
if ( isImpliedOptimisticLocking && entry.getLoadedState() != null ) {
// The entity will be deleted and because we are going to create a delete statement
// The entity will be deleted, and because we are going to create a delete statement
// that uses all the state values in the where clause, the entry state needs to be
// updated otherwise the statement execution will not delete any row (see HHH-15218).
// updated. Otherwise, the statement execution will not delete any row (see HHH-15218).
entry.postUpdate( getInstance(), state, nextVersion );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public Set<AbstractEntityInsertAction> resolveDependentActions(Object managedEnt
infoString( entityEntry.getEntityName(), entityEntry.getId() )
);
}
// dependentAction only depended on managedEntity..
// dependentAction only depended on managedEntity
dependenciesByAction.remove( dependentAction );
resolvedActions.add( dependentAction );
}
Expand All @@ -252,18 +252,18 @@ public void clear() {

@Override
public String toString() {
final var sb = new StringBuilder( getClass().getSimpleName() ).append( '[' );
final var representation = new StringBuilder( getClass().getSimpleName() ).append( '[' );
for ( var entry : dependenciesByAction.entrySet() ) {
final AbstractEntityInsertAction insert = entry.getKey();
final NonNullableTransientDependencies dependencies = entry.getValue();
sb.append( "[insert=" )
representation.append( "[insert=" )
.append( insert )
.append( " dependencies=[" )
.append( dependencies.toLoggableString( insert.getSession() ) )
.append( "]" );
}
sb.append( ']');
return sb.toString();
representation.append( ']');
return representation.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ private void evictNaturalIdData(NavigableRole rootEntityRole, NaturalIdDataAcces

@Override
public boolean containsCollection(String role, Object ownerIdentifier) {
final CollectionPersister persister = getCollectionDescriptor( role );
final CollectionDataAccess cacheAccess = persister.getCacheAccessStrategy();
final var persister = getCollectionDescriptor( role );
final var cacheAccess = persister.getCacheAccessStrategy();
if ( cacheAccess != null ) {
final Object cacheKey =
cacheAccess.generateCacheKey( ownerIdentifier, persister, sessionFactory, null );
Expand Down
Loading