diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java index 006856e1f377..2aa1e7f477d0 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java @@ -392,37 +392,23 @@ public boolean elementExists(Object element) { protected Object readElementByIndex(final Object index) { if ( !initialized ) { - class ExtraLazyElementByIndexReader implements LazyInitializationWork { - private boolean isExtraLazy; - private Object element; - - @Override - public Object doWork() { - final var entry = getCollectionEntry(); - final var persister = entry.getLoadedPersister(); - checkPersister( AbstractPersistentCollection.this, persister ); - isExtraLazy = persister.isExtraLazy(); - if ( isExtraLazy ) { - if ( hasQueuedOperations() ) { - session.flush(); - } - element = persister.getElementByIndex( entry.getLoadedKey(), index, session, owner ); + return withTemporarySessionIfNeeded( () -> { + final var entry = getCollectionEntry(); + final var persister = entry.getLoadedPersister(); + checkPersister( AbstractPersistentCollection.this, persister ); + if ( persister.isExtraLazy() ) { + if ( hasQueuedOperations() ) { + session.flush(); } - else { - read(); - } - return null; + return persister.getElementByIndex( entry.getLoadedKey(), index, session, owner ); } - } - - final ExtraLazyElementByIndexReader reader = new ExtraLazyElementByIndexReader(); - withTemporarySessionIfNeeded( reader ); - if ( reader.isExtraLazy ) { - return reader.element; - } + else { + read(); + return UNKNOWN; + } + } ); } return UNKNOWN; - } @Override @@ -791,7 +777,7 @@ private String unexpectedSessionStateMessage(SharedSessionContractImplementor se final String roleCurrent = role; final Object keyCurrent = key; - final var message = new StringBuilder( "Collection : " ); + final var message = new StringBuilder( "Collection: " ); if ( roleCurrent != null ) { message.append( collectionInfoString( roleCurrent, keyCurrent ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index eef96d4780d2..c7030267ffbc 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -54,7 +54,6 @@ import org.hibernate.mapping.IdentifierCollection; import org.hibernate.mapping.IndexedCollection; import org.hibernate.mapping.Table; -import org.hibernate.mapping.Value; import org.hibernate.metamodel.CollectionClassification; import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.CollectionPart; @@ -247,6 +246,7 @@ public AbstractCollectionPersister( RuntimeModelCreationContext creationContext) throws MappingException, CacheException { factory = creationContext.getSessionFactory(); + final var factoryOptions = creationContext.getSessionFactoryOptions(); this.collectionBootDescriptor = collectionBootDescriptor; this.collectionSemantics = @@ -256,10 +256,10 @@ public AbstractCollectionPersister( this.cacheAccessStrategy = cacheAccessStrategy; - cacheEntryStructure = cacheEntryStructure( collectionBootDescriptor, creationContext ); + cacheEntryStructure = + cacheEntryStructure( collectionBootDescriptor, factoryOptions ); useShallowQueryCacheLayout = - shouldUseShallowCacheLayout( collectionBootDescriptor.getQueryCacheLayout(), - creationContext.getSessionFactoryOptions() ); + shouldUseShallowCacheLayout( collectionBootDescriptor.getQueryCacheLayout(), factoryOptions ); dialect = creationContext.getDialect(); sqlExceptionHelper = creationContext.getJdbcServices().getSqlExceptionHelper(); @@ -292,7 +292,7 @@ public AbstractCollectionPersister( hasOrphanDelete = collectionBootDescriptor.hasOrphanDelete(); - batchSize = batchSize( collectionBootDescriptor, creationContext ); + batchSize = batchSize( collectionBootDescriptor, factoryOptions ); isVersioned = collectionBootDescriptor.isOptimisticLocked(); @@ -384,7 +384,7 @@ else if ( selectable instanceof Column column ) { if ( collectionBootDescriptor instanceof IndexedCollection indexedCollection ) { assert collectionBootDescriptor.isIndexed(); // NativeSQL: collect index column and auto-aliases - final Value index = indexedCollection.getIndex(); + final var index = indexedCollection.getIndex(); indexType = index.getType(); final int indexSpan = index.getColumnSpan(); final boolean[] indexColumnInsertability = index.getColumnInsertability(); @@ -511,7 +511,8 @@ private FilterHelper manyToManyFilterHelper(Collection collection, RuntimeModelC private FilterHelper filterHelper( Collection collection, EntityPersister elementPersister, RuntimeModelCreationContext context) { - if ( collection.getFilters().isEmpty() ) { + final var filters = collection.getFilters(); + if ( filters.isEmpty() ) { return null; } else { @@ -522,18 +523,19 @@ private FilterHelper filterHelper( context.getBootModel().getEntityBinding( elementPersister.getEntityName() ), context.getSessionFactory().getSqlStringGenerationContext() ); - return new FilterHelper( collection.getFilters(), entityNameByTableNameMap, factory ); + return new FilterHelper( filters, entityNameByTableNameMap, factory ); } } - private static int batchSize(Collection collection, RuntimeModelCreationContext context) { - return collection.getBatchSize() < 0 - ? context.getSessionFactoryOptions().getDefaultBatchFetchSize() - : collection.getBatchSize(); + private static int batchSize(Collection collection, SessionFactoryOptions options) { + int batchSize = collection.getBatchSize(); + return batchSize >= 0 + ? batchSize + : options.getDefaultBatchFetchSize(); } - private static CacheEntryStructure cacheEntryStructure(Collection collection, RuntimeModelCreationContext context) { - if ( context.getSessionFactoryOptions().isStructuredCacheEntriesEnabled() ) { + private static CacheEntryStructure cacheEntryStructure(Collection collection, SessionFactoryOptions options) { + if ( options.isStructuredCacheEntriesEnabled() ) { return collection.isMap() ? StructuredMapCacheEntry.INSTANCE : StructuredCollectionCacheEntry.INSTANCE;