@@ -73,116 +73,7 @@ public BasicEntityIdentifierMapping getIdentifierMapping() {
73
73
}
74
74
75
75
@ Override
76
- protected <K > List <E > performOrderedMultiLoad (K [] ids , MultiIdLoadOptions loadOptions , EventSource session ) {
77
- if ( MultiKeyLoadLogging .MULTI_KEY_LOAD_LOGGER .isTraceEnabled () ) {
78
- MultiKeyLoadLogging .MULTI_KEY_LOAD_LOGGER .tracef (
79
- "MultiIdEntityLoaderArrayParam#performOrderedMultiLoad - %s" ,
80
- getLoadable ().getEntityName ()
81
- );
82
- }
83
-
84
- assert loadOptions .isOrderReturnEnabled ();
85
-
86
- final boolean coerce = !getSessionFactory ().getJpaMetamodel ().getJpaCompliance ().isLoadByIdComplianceEnabled ();
87
- final JavaType <?> idType = getLoadable ().getIdentifierMapping ().getJavaType ();
88
-
89
- final LockOptions lockOptions = loadOptions .getLockOptions () == null
90
- ? new LockOptions ( LockMode .NONE )
91
- : loadOptions .getLockOptions ();
92
-
93
- final int maxBatchSize = maxBatchSize ( ids , loadOptions );
94
-
95
- final List <Object > result = CollectionHelper .arrayList ( ids .length );
96
-
97
- final List <Object > idsInBatch = new ArrayList <>();
98
- final List <Integer > elementPositionsLoadedByBatch = new ArrayList <>();
99
-
100
- for ( int i = 0 ; i < ids .length ; i ++ ) {
101
- final Object id = coerce ? idType .coerce ( ids [i ], session ) : ids [i ];
102
- final EntityKey entityKey = new EntityKey ( id , getLoadable ().getEntityPersister () );
103
-
104
- if ( !loadFromCaches ( loadOptions , session , id , lockOptions , entityKey , result , i ) ) {
105
- // if we did not hit any of the continues above,
106
- // then we need to batch load the entity state.
107
- idsInBatch .add ( id );
108
-
109
- if ( idsInBatch .size () >= maxBatchSize ) {
110
- // we've hit the allotted max-batch-size, perform an "intermediate load"
111
- loadEntitiesById ( loadOptions , session , lockOptions , idsInBatch );
112
- idsInBatch .clear ();
113
- }
114
-
115
- // Save the EntityKey instance for use later
116
- result .add ( i , entityKey );
117
- elementPositionsLoadedByBatch .add ( i );
118
- }
119
- }
120
-
121
- if ( !idsInBatch .isEmpty () ) {
122
- // we still have ids to load from the processing above since
123
- // the last max-batch-size trigger, perform a load for them
124
- loadEntitiesById ( loadOptions , session , lockOptions , idsInBatch );
125
- }
126
-
127
- // for each result where we set the EntityKey earlier, replace them
128
- handleResults ( loadOptions , session , elementPositionsLoadedByBatch , result );
129
-
130
- //noinspection unchecked
131
- return (List <E >) result ;
132
- }
133
-
134
- private boolean loadFromCaches (
135
- MultiIdLoadOptions loadOptions ,
136
- EventSource session ,
137
- Object id ,
138
- LockOptions lockOptions ,
139
- EntityKey entityKey ,
140
- List <Object > result ,
141
- int i ) {
142
- if ( loadOptions .isSessionCheckingEnabled () || loadOptions .isSecondLevelCacheCheckingEnabled () ) {
143
- final LoadEvent loadEvent = new LoadEvent (
144
- id ,
145
- getLoadable ().getJavaType ().getJavaTypeClass ().getName (),
146
- lockOptions ,
147
- session ,
148
- LoaderHelper .getReadOnlyFromLoadQueryInfluencers ( session )
149
- );
150
-
151
- Object managedEntity = null ;
152
-
153
- if ( loadOptions .isSessionCheckingEnabled () ) {
154
- // look for it in the Session first
155
- final PersistenceContextEntry persistenceContextEntry =
156
- loadFromSessionCacheStatic ( loadEvent , entityKey , LoadEventListener .GET );
157
- managedEntity = persistenceContextEntry .getEntity ();
158
-
159
- if ( managedEntity != null
160
- && !loadOptions .isReturnOfDeletedEntitiesEnabled ()
161
- && !persistenceContextEntry .isManaged () ) {
162
- // put a null in the result
163
- result .add ( i , null );
164
- return true ;
165
- }
166
- }
167
-
168
- if ( managedEntity == null && loadOptions .isSecondLevelCacheCheckingEnabled () ) {
169
- // look for it in the SessionFactory
170
- managedEntity = CacheEntityLoaderHelper .INSTANCE .loadFromSecondLevelCache (
171
- loadEvent ,
172
- getLoadable ().getEntityPersister (),
173
- entityKey
174
- );
175
- }
176
-
177
- if ( managedEntity != null ) {
178
- result .add ( i , managedEntity );
179
- return true ;
180
- }
181
- }
182
- return false ;
183
- }
184
-
185
- private static void handleResults (
76
+ protected void handleResults (
186
77
MultiIdLoadOptions loadOptions ,
187
78
EventSource session ,
188
79
List <Integer > elementPositionsLoadedByBatch ,
@@ -209,7 +100,8 @@ private static void handleResults(
209
100
}
210
101
}
211
102
212
- private <K > int maxBatchSize (K [] ids , MultiIdLoadOptions loadOptions ) {
103
+ @ Override
104
+ protected int maxBatchSize (Object [] ids , MultiIdLoadOptions loadOptions ) {
213
105
if ( loadOptions .getBatchSize () != null && loadOptions .getBatchSize () > 0 ) {
214
106
return loadOptions .getBatchSize ();
215
107
}
@@ -225,8 +117,12 @@ private <K> int maxBatchSize(K[] ids, MultiIdLoadOptions loadOptions) {
225
117
}
226
118
}
227
119
228
- private void loadEntitiesById (
229
- MultiIdLoadOptions loadOptions , EventSource session , LockOptions lockOptions , List <Object > idsToLoadFromDatabase ) {
120
+ @ Override
121
+ protected void loadEntitiesById (
122
+ List <Object > idsToLoadFromDatabase ,
123
+ LockOptions lockOptions ,
124
+ MultiIdLoadOptions loadOptions ,
125
+ EventSource session ) {
230
126
final SelectStatement sqlAst = LoaderSelectBuilder .createSelectBySingleArrayParameter (
231
127
getLoadable (),
232
128
getIdentifierMapping (),
@@ -361,22 +257,16 @@ protected final <R,K> K[] processResolvableEntities(
361
257
return ids ;
362
258
}
363
259
364
- final boolean coerce = !getSessionFactory ().getJpaMetamodel ().getJpaCompliance ().isLoadByIdComplianceEnabled ();
365
-
366
260
boolean foundAnyResolvedEntities = false ;
367
261
List <K > nonResolvedIds = null ;
368
262
369
- for ( int i = 0 ; i < ids .length ; i ++ ) {
370
- final Object id ;
371
- if ( coerce ) {
372
- //noinspection unchecked
373
- id = (K ) getLoadable ().getIdentifierMapping ().getJavaType ().coerce ( ids [i ], session );
374
- }
375
- else {
376
- id = ids [i ];
377
- }
263
+ final boolean coerce = !getSessionFactory ().getJpaMetamodel ().getJpaCompliance ().isLoadByIdComplianceEnabled ();
264
+ final JavaType <?> idType = getLoadable ().getIdentifierMapping ().getJavaType ();
378
265
266
+ for ( int i = 0 ; i < ids .length ; i ++ ) {
267
+ final Object id = coerce ? idType .coerce ( ids [i ], session ) : ids [i ];
379
268
final EntityKey entityKey = new EntityKey ( id , getLoadable ().getEntityPersister () );
269
+
380
270
final LoadEvent loadEvent = new LoadEvent (
381
271
id ,
382
272
getLoadable ().getJavaType ().getJavaTypeClass ().getName (),
@@ -385,18 +275,15 @@ protected final <R,K> K[] processResolvableEntities(
385
275
LoaderHelper .getReadOnlyFromLoadQueryInfluencers ( session )
386
276
);
387
277
388
- Object resolvedEntity = null ;
278
+ Object managedEntity = null ;
389
279
390
280
// look for it in the Session first
391
- final PersistenceContextEntry persistenceContextEntry = loadFromSessionCacheStatic (
392
- loadEvent ,
393
- entityKey ,
394
- LoadEventListener .GET
395
- );
281
+ final PersistenceContextEntry persistenceContextEntry =
282
+ loadFromSessionCacheStatic ( loadEvent , entityKey , LoadEventListener .GET );
396
283
if ( loadOptions .isSessionCheckingEnabled () ) {
397
- resolvedEntity = persistenceContextEntry .getEntity ();
284
+ managedEntity = persistenceContextEntry .getEntity ();
398
285
399
- if ( resolvedEntity != null
286
+ if ( managedEntity != null
400
287
&& !loadOptions .isReturnOfDeletedEntitiesEnabled ()
401
288
&& !persistenceContextEntry .isManaged () ) {
402
289
foundAnyResolvedEntities = true ;
@@ -405,25 +292,25 @@ protected final <R,K> K[] processResolvableEntities(
405
292
}
406
293
}
407
294
408
- if ( resolvedEntity == null && loadOptions .isSecondLevelCacheCheckingEnabled () ) {
409
- resolvedEntity = CacheEntityLoaderHelper .INSTANCE .loadFromSecondLevelCache (
295
+ if ( managedEntity == null && loadOptions .isSecondLevelCacheCheckingEnabled () ) {
296
+ managedEntity = CacheEntityLoaderHelper .INSTANCE .loadFromSecondLevelCache (
410
297
loadEvent ,
411
298
getLoadable ().getEntityPersister (),
412
299
entityKey
413
300
);
414
301
}
415
302
416
- if ( resolvedEntity != null ) {
303
+ if ( managedEntity != null ) {
417
304
foundAnyResolvedEntities = true ;
418
305
419
306
//noinspection unchecked
420
- resolutionConsumer .consume ( i , entityKey , (R ) resolvedEntity );
307
+ resolutionConsumer .consume ( i , entityKey , (R ) managedEntity );
421
308
}
422
309
else {
423
310
if ( nonResolvedIds == null ) {
424
311
nonResolvedIds = new ArrayList <>();
425
312
}
426
- //noinspection unchecked,CastCanBeRemovedNarrowingVariableType
313
+ //noinspection unchecked
427
314
nonResolvedIds .add ( (K ) id );
428
315
}
429
316
}
0 commit comments