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 62e492af41..0aceaf1f35 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 @@ -282,7 +282,11 @@ public abstract class AbstractPersistentCollection implements Serializable, P } } else { - if ( !session.isTransactionInProgress() ) { + /* + Whenever the collection lazy loading is triggered during the loading process, + closing the connection will cause an error when RowProcessingStateStandardImpl#next() will be called. + */ + if ( !session.isTransactionInProgress() && session.getPersistenceContext().isLoadFinished() ) { session.getJdbcCoordinator().afterTransaction(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java index 8073013809..e5aa8f6a50 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/AbstractCollectionInitializer.java @@ -101,6 +101,11 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial if ( loadingEntry != null ) { collectionInstance = loadingEntry.getCollectionInstance(); + if ( collectionInstance.getOwner() == null ) { + parentAccess.registerResolutionListener( + owner -> collectionInstance.setOwner( owner ) + ); + } return; } @@ -108,6 +113,11 @@ public abstract class AbstractCollectionInitializer implements CollectionInitial if ( existing != null ) { collectionInstance = existing; + if ( collectionInstance.getOwner() == null ) { + parentAccess.registerResolutionListener( + owner -> collectionInstance.setOwner( owner ) + ); + } return; }