1616import org .hibernate .metamodel .mapping .NaturalIdMapping ;
1717
1818import java .util .List ;
19+ import java .util .function .Consumer ;
1920
2021import static java .util .Collections .emptyList ;
2122import static org .hibernate .internal .util .collections .CollectionHelper .arrayList ;
@@ -69,7 +70,7 @@ private <K> List<E> unorderedMultiLoad(
6970 final List <E > results = arrayList ( naturalIds .length );
7071 final LockOptions lockOptions = lockOptions ( loadOptions );
7172 final Object [] unresolvedIds =
72- checkPersistenceContextForCachedResults ( naturalIds , loadOptions , session , lockOptions , results );
73+ checkPersistenceContextForCachedResults ( naturalIds , loadOptions , session , lockOptions , results :: add );
7374 if ( !isEmpty ( unresolvedIds ) ) {
7475 results .addAll ( loadEntitiesWithUnresolvedIds ( unresolvedIds , loadOptions , lockOptions , session ) );
7576 }
@@ -82,7 +83,10 @@ protected abstract List<E> loadEntitiesWithUnresolvedIds(
8283 LockOptions lockOptions ,
8384 SharedSessionContractImplementor session );
8485
85- private <K > List <E > performOrderedMultiLoad (K [] naturalIds , MultiNaturalIdLoadOptions options , SharedSessionContractImplementor session ) {
86+ private <K > List <E > performOrderedMultiLoad (
87+ K [] naturalIds ,
88+ MultiNaturalIdLoadOptions options ,
89+ SharedSessionContractImplementor session ) {
8690 if ( MULTI_KEY_LOAD_LOGGER .isTraceEnabled () ) {
8791 MULTI_KEY_LOAD_LOGGER .trace ( "Ordered MultiLoad starting: "
8892 + getEntityDescriptor ().getEntityName () );
@@ -94,7 +98,12 @@ private <K> List<E> orderedMultiLoad(
9498 K [] naturalIds ,
9599 MultiNaturalIdLoadOptions loadOptions ,
96100 SharedSessionContractImplementor session ) {
97- unorderedMultiLoad ( naturalIds , loadOptions , session );
101+ final LockOptions lockOptions = lockOptions ( loadOptions );
102+ final Object [] unresolvedIds =
103+ checkPersistenceContextForCachedResults ( naturalIds , loadOptions , session , lockOptions , result -> {} );
104+ if ( !isEmpty ( unresolvedIds ) ) {
105+ loadEntitiesWithUnresolvedIds ( unresolvedIds , loadOptions , lockOptions , session );
106+ }
98107 return sortResults ( naturalIds , loadOptions , session );
99108 }
100109
@@ -133,7 +142,7 @@ private <K> Object[] checkPersistenceContextForCachedResults(
133142 MultiNaturalIdLoadOptions loadOptions ,
134143 SharedSessionContractImplementor session ,
135144 LockOptions lockOptions ,
136- List <E > results ) {
145+ Consumer <E > results ) {
137146 final List <K > unresolvedIds = arrayList ( naturalIds .length );
138147 final PersistenceContext context = session .getPersistenceContextInternal ();
139148 final NaturalIdMapping naturalIdMapping = getEntityDescriptor ().getNaturalIdMapping ();
@@ -147,7 +156,7 @@ private <K> Object[] checkPersistenceContextForCachedResults(
147156 // either a managed entry, or a deleted one with returnDeleted enabled
148157 upgradeLock ( entity , entry , lockOptions , session );
149158 final Object result = context .proxyFor ( entity );
150- results .add ( (E ) result );
159+ results .accept ( (E ) result );
151160 }
152161 }
153162 else {
@@ -166,7 +175,7 @@ public EntityMappingType getLoadable() {
166175 return getEntityDescriptor ();
167176 }
168177
169- protected EntityMappingType getEntityDescriptor () {
178+ protected final EntityMappingType getEntityDescriptor () {
170179 return entityDescriptor ;
171180 }
172181}
0 commit comments