From 3a0b5feda07e35df9dd04f44c6d3af17bb9bfacd Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 10 Nov 2022 16:18:38 +0100 Subject: [PATCH] HHH-15640 ElementCollection with SubSelect FetchMode throws NPE, ownerEntry is null --- .../CollectionLoaderSubSelectFetch.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderSubSelectFetch.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderSubSelectFetch.java index 2be929a7e0..c6d8104db7 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderSubSelectFetch.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderSubSelectFetch.java @@ -86,29 +86,32 @@ public class CollectionLoaderSubSelectFetch implements CollectionLoader { final PersistentCollection collection = persistenceContext.getCollection( collectionKey ); attributeMapping.getCollectionDescriptor().getCollectionType().getKeyOfOwner( collection.getOwner(), session ); - final EntityEntry ownerEntry = persistenceContext.getEntry( collection.getOwner() ); final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue(); - final EntityKey triggerKeyOwnerKey = ownerEntry.getEntityKey(); - final SubselectFetch registeredFetch = batchFetchQueue.getSubselect( triggerKeyOwnerKey ); + final EntityEntry ownerEntry = persistenceContext.getEntry( collection.getOwner() ); List> subSelectFetchedCollections = null; - if ( registeredFetch != null ) { - subSelectFetchedCollections = CollectionHelper.arrayList( registeredFetch.getResultingEntityKeys().size() ); + if ( ownerEntry != null ) { + final EntityKey triggerKeyOwnerKey = ownerEntry.getEntityKey(); + final SubselectFetch registeredFetch = batchFetchQueue.getSubselect( triggerKeyOwnerKey ); + if ( registeredFetch != null ) { + subSelectFetchedCollections = CollectionHelper.arrayList( + registeredFetch.getResultingEntityKeys().size() ); - // there was one, so we want to make sure to prepare the corresponding collection - // reference for reading - final Iterator itr = registeredFetch.getResultingEntityKeys().iterator(); - while ( itr.hasNext() ) { - final EntityKey key = itr.next(); + // there was one, so we want to make sure to prepare the corresponding collection + // reference for reading + final Iterator itr = registeredFetch.getResultingEntityKeys().iterator(); + while ( itr.hasNext() ) { + final EntityKey key = itr.next(); - final PersistentCollection containedCollection = persistenceContext.getCollection( - new CollectionKey( attributeMapping.getCollectionDescriptor(), key.getIdentifier() ) - ); + final PersistentCollection containedCollection = persistenceContext.getCollection( + new CollectionKey( attributeMapping.getCollectionDescriptor(), key.getIdentifier() ) + ); - if ( containedCollection != collection ) { - containedCollection.beginRead(); - containedCollection.beforeInitialize( getLoadable().getCollectionDescriptor(), -1 ); + if ( containedCollection != collection ) { + containedCollection.beginRead(); + containedCollection.beforeInitialize( getLoadable().getCollectionDescriptor(), -1 ); - subSelectFetchedCollections.add( containedCollection ); + subSelectFetchedCollections.add( containedCollection ); + } } } }