HHH-18067 get rid of a typecast to AbstractPersistentCollection

and code cleanups

(Note that this typecast has actually been there since 2012,
though it only used to happen when batching was enabled.)

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-03 21:00:49 +02:00
parent 138e0bba01
commit 0cfb52d127
5 changed files with 54 additions and 47 deletions

View File

@ -1107,7 +1107,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
public CollectionEntry addInitializedCollection(CollectionPersister persister, PersistentCollection<?> collection, Object id)
throws HibernateException {
final CollectionEntry ce = new CollectionEntry( collection, persister, id, flushing );
ce.postInitialize( collection );
ce.postInitialize( collection, session );
addCollection( collection, ce, id );
return ce;
}

View File

@ -14,7 +14,6 @@ import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.NullnessUtil;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.pretty.MessageHelper;
import java.io.IOException;
import java.io.ObjectInputStream;
@ -24,6 +23,8 @@ import java.util.Collection;
import org.checkerframework.checker.nullness.qual.Nullable;
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
/**
* We need an entry to tell us all about the current state
* of a collection with respect to its persistent state
@ -156,12 +157,13 @@ public final class CollectionEntry implements Serializable {
private void dirty(PersistentCollection<?> collection) throws HibernateException {
final CollectionPersister loadedPersister = getLoadedPersister();
boolean forceDirty = collection.wasInitialized() &&
!collection.isDirty() && //optimization
loadedPersister != null &&
loadedPersister.isMutable() && //optimization
( collection.isDirectlyAccessible() || loadedPersister.getElementType().isMutable() ) && //optimization
!collection.equalsSnapshot( loadedPersister );
final boolean forceDirty =
collection.wasInitialized()
&& !collection.isDirty() //optimization
&& loadedPersister != null
&& loadedPersister.isMutable() //optimization
&& ( collection.isDirectlyAccessible() || loadedPersister.getElementType().isMutable() ) //optimization
&& !collection.equalsSnapshot( loadedPersister );
if ( forceDirty ) {
collection.dirty();
@ -181,7 +183,7 @@ public final class CollectionEntry implements Serializable {
if ( nonMutableChange ) {
throw new HibernateException(
"changed an immutable collection instance: " +
MessageHelper.collectionInfoString( NullnessUtil.castNonNull( loadedPersister ).getRole(), getLoadedKey() )
collectionInfoString( NullnessUtil.castNonNull( loadedPersister ).getRole(), getLoadedKey() )
);
}
@ -190,7 +192,7 @@ public final class CollectionEntry implements Serializable {
if ( LOG.isDebugEnabled() && collection.isDirty() && loadedPersister != null ) {
LOG.debugf(
"Collection dirty: %s",
MessageHelper.collectionInfoString( loadedPersister.getRole(), getLoadedKey() )
collectionInfoString( loadedPersister.getRole(), getLoadedKey() )
);
}
@ -202,13 +204,13 @@ public final class CollectionEntry implements Serializable {
setDorecreate( false );
}
public void postInitialize(PersistentCollection<?> collection) throws HibernateException {
public void postInitialize(PersistentCollection<?> collection, SharedSessionContractImplementor session)
throws HibernateException {
final CollectionPersister loadedPersister = getLoadedPersister();
snapshot = loadedPersister != null && loadedPersister.isMutable()
? collection.getSnapshot( loadedPersister )
: null;
collection.setSnapshot( loadedKey, role, snapshot );
final SharedSessionContractImplementor session = ((AbstractPersistentCollection<?>) collection).getSession();
if ( loadedPersister != null && session.getLoadQueryInfluencers().effectivelyBatchLoadable( loadedPersister ) ) {
session.getPersistenceContextInternal()
.getBatchFetchQueue()
@ -380,10 +382,10 @@ public final class CollectionEntry implements Serializable {
@Override
public String toString() {
String result = "CollectionEntry" +
MessageHelper.collectionInfoString( role, loadedKey );
collectionInfoString( role, loadedKey );
if ( currentPersister != null ) {
result += "->" +
MessageHelper.collectionInfoString( currentPersister.getRole(), currentKey );
collectionInfoString( currentPersister.getRole(), currentKey );
}
return result;
}

View File

@ -19,6 +19,7 @@ import org.hibernate.event.spi.InitializeCollectionEvent;
import org.hibernate.event.spi.InitializeCollectionEventListener;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.sql.results.internal.ResultsHelper;
import org.hibernate.stat.spi.StatisticsImplementor;
@ -118,38 +119,40 @@ public class DefaultInitializeCollectionEventListener implements InitializeColle
return false;
}
final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled();
if ( persister.hasCache() && source.getCacheMode().isGetEnabled() ) {
final SessionFactoryImplementor factory = source.getFactory();
final CollectionDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy();
final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() );
final Object ce = CacheHelper.fromSharedCache( source, ck, persister, cacheAccessStrategy );
if ( !useCache ) {
return false;
}
final StatisticsImplementor statistics = factory.getStatistics();
if ( statistics.isStatisticsEnabled() ) {
final NavigableRole navigableRole = persister.getNavigableRole();
final String regionName = cacheAccessStrategy.getRegion().getName();
if ( ce == null ) {
statistics.collectionCacheMiss( navigableRole, regionName );
}
else {
statistics.collectionCacheHit( navigableRole, regionName );
}
}
final SessionFactoryImplementor factory = source.getFactory();
final CollectionDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy();
final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() );
final Object ce = CacheHelper.fromSharedCache( source, ck, persister, cacheAccessStrategy );
final StatisticsImplementor statistics = factory.getStatistics();
if ( statistics.isStatisticsEnabled() ) {
if ( ce == null ) {
statistics.collectionCacheMiss( persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName() );
return false;
}
else {
statistics.collectionCacheHit( persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName() );
final CollectionCacheEntry cacheEntry = (CollectionCacheEntry)
persister.getCacheEntryStructure().destructure( ce, factory );
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
cacheEntry.assemble( collection, persister, persistenceContext.getCollectionOwner( id, persister ) );
persistenceContext.getCollectionEntry( collection ).postInitialize( collection, source );
// addInitializedCollection(collection, persister, id);
return true;
}
}
if ( ce == null ) {
else {
return false;
}
final CollectionCacheEntry cacheEntry = (CollectionCacheEntry)
persister.getCacheEntryStructure().destructure( ce, factory );
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
cacheEntry.assemble( collection, persister, persistenceContext.getCollectionOwner( id, persister ) );
persistenceContext.getCollectionEntry( collection ).postInitialize( collection );
// addInitializedCollection(collection, persister, id);
return true;
}
}

View File

@ -53,9 +53,12 @@ public class CollectionLoaderNamedQuery implements CollectionLoader {
else {
// using annotations we have no way to specify a @CollectionResult
final CollectionKey collectionKey = new CollectionKey( persister, key );
final PersistentCollection<?> collection = session.getPersistenceContextInternal().getCollection( collectionKey );
final PersistentCollection<?> collection =
session.getPersistenceContextInternal()
.getCollection( collectionKey );
for ( Object element : resultList ) {
if ( element != null && !persister.getElementType().getReturnedClass().isInstance( element ) ) {
if ( element != null
&& !persister.getElementType().getReturnedClass().isInstance( element ) ) {
throw new QueryTypeMismatchException( "Collection loader for '" + persister.getRole()
+ "' returned an instance of '" + element.getClass().getName() + "'" );
}
@ -63,7 +66,9 @@ public class CollectionLoaderNamedQuery implements CollectionLoader {
collection.beforeInitialize( persister, resultList.size() );
collection.injectLoadedState( getLoadable(), resultList );
collection.afterInitialize();
session.getPersistenceContextInternal().getCollectionEntry( collection ).postInitialize( collection );
session.getPersistenceContextInternal()
.getCollectionEntry( collection )
.postInitialize( collection, session );
return collection;
}
}

View File

@ -184,16 +184,14 @@ public class ResultsHelper {
PersistentCollection<?> collectionInstance,
Object key,
boolean hasNoQueuedAdds) {
final SharedSessionContractImplementor session = persistenceContext.getSession();
CollectionEntry collectionEntry = persistenceContext.getCollectionEntry( collectionInstance );
if ( collectionEntry == null ) {
collectionEntry = persistenceContext.addInitializedCollection(
collectionDescriptor,
collectionInstance,
key
);
collectionEntry = persistenceContext.addInitializedCollection( collectionDescriptor, collectionInstance, key );
}
else {
collectionEntry.postInitialize( collectionInstance );
collectionEntry.postInitialize( collectionInstance, session );
}
if ( collectionDescriptor.getCollectionType().hasHolder() ) {
@ -211,7 +209,6 @@ public class ResultsHelper {
final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue();
batchFetchQueue.removeBatchLoadableCollection( collectionEntry );
final SharedSessionContractImplementor session = persistenceContext.getSession();
// add to cache if:
final boolean addToCache =
// there were no queued additions