10
10
11
11
import org .hibernate .LockMode ;
12
12
import org .hibernate .LockOptions ;
13
- import org .hibernate .engine .internal .BatchFetchQueueHelper ;
14
- import org .hibernate .engine .spi .BatchFetchQueue ;
13
+ import org .hibernate .engine .jdbc .spi .JdbcServices ;
15
14
import org .hibernate .engine .spi .EntityEntry ;
16
15
import org .hibernate .engine .spi .EntityKey ;
17
16
import org .hibernate .engine .spi .PersistenceContext ;
18
17
import org .hibernate .engine .spi .SessionFactoryImplementor ;
19
- import org .hibernate .engine .spi .SubselectFetch ;
20
18
import org .hibernate .event .spi .EventSource ;
21
19
import org .hibernate .event .spi .LoadEvent ;
22
20
import org .hibernate .event .spi .LoadEventListener ;
28
26
import org .hibernate .metamodel .mapping .EntityMappingType ;
29
27
import org .hibernate .metamodel .mapping .JdbcMapping ;
30
28
import org .hibernate .query .spi .QueryOptions ;
29
+ import org .hibernate .sql .ast .SqlAstTranslatorFactory ;
31
30
import org .hibernate .sql .ast .tree .expression .JdbcParameter ;
32
31
import org .hibernate .sql .ast .tree .select .SelectStatement ;
33
32
import org .hibernate .sql .exec .internal .JdbcParameterBindingImpl ;
36
35
import org .hibernate .sql .exec .spi .JdbcOperationQuerySelect ;
37
36
import org .hibernate .sql .exec .spi .JdbcParameterBindings ;
38
37
import org .hibernate .sql .exec .spi .JdbcParametersList ;
38
+ import org .hibernate .sql .exec .spi .JdbcSelectExecutor ;
39
39
import org .hibernate .sql .results .internal .RowTransformerStandardImpl ;
40
40
import org .hibernate .sql .results .spi .ManagedResultConsumer ;
41
41
42
42
import org .checkerframework .checker .nullness .qual .NonNull ;
43
43
import org .hibernate .type .descriptor .java .JavaType ;
44
44
45
45
import static java .lang .Boolean .TRUE ;
46
+ import static org .hibernate .engine .internal .BatchFetchQueueHelper .removeBatchLoadableEntityKey ;
47
+ import static org .hibernate .engine .spi .SubselectFetch .createRegistrationHandler ;
46
48
import static org .hibernate .internal .util .collections .CollectionHelper .isEmpty ;
47
49
import static org .hibernate .loader .ast .internal .CacheEntityLoaderHelper .loadFromSessionCacheStatic ;
50
+ import static org .hibernate .loader .ast .internal .LoaderHelper .getReadOnlyFromLoadQueryInfluencers ;
51
+ import static org .hibernate .loader .ast .internal .LoaderHelper .loadByArrayParameter ;
52
+ import static org .hibernate .loader .ast .internal .LoaderSelectBuilder .createSelectBySingleArrayParameter ;
53
+ import static org .hibernate .loader .ast .internal .MultiKeyLoadHelper .resolveArrayJdbcMapping ;
54
+ import static org .hibernate .sql .exec .spi .JdbcParameterBindings .NO_BINDINGS ;
48
55
49
56
/**
50
57
* @author Steve Ebersole
51
58
*/
52
59
public class MultiIdEntityLoaderArrayParam <E > extends AbstractMultiIdEntityLoader <E > implements SqlArrayMultiKeyLoader {
53
60
private final JdbcMapping arrayJdbcMapping ;
54
61
private final JdbcParameter jdbcParameter ;
55
- private final int idJdbcTypeCount ;
56
62
57
- public MultiIdEntityLoaderArrayParam (EntityMappingType entityDescriptor , int identifierColumnSpan , SessionFactoryImplementor sessionFactory ) {
63
+ public MultiIdEntityLoaderArrayParam (
64
+ EntityMappingType entityDescriptor ,
65
+ SessionFactoryImplementor sessionFactory ) {
58
66
super ( entityDescriptor , sessionFactory );
59
- this .idJdbcTypeCount = identifierColumnSpan ;
60
67
final Class <?> arrayClass = createTypedArray ( 0 ).getClass ();
61
- arrayJdbcMapping = MultiKeyLoadHelper . resolveArrayJdbcMapping (
68
+ arrayJdbcMapping = resolveArrayJdbcMapping (
62
69
getSessionFactory ().getTypeConfiguration ().getBasicTypeRegistry ().getRegisteredType ( arrayClass ),
63
70
getIdentifierMapping ().getJdbcMapping (),
64
71
arrayClass ,
@@ -83,7 +90,7 @@ protected void handleResults(
83
90
// the element value at this position in the result List should be
84
91
// the EntityKey for that entity - reuse it
85
92
final EntityKey entityKey = (EntityKey ) result .get ( position );
86
- BatchFetchQueueHelper . removeBatchLoadableEntityKey ( entityKey , session );
93
+ removeBatchLoadableEntityKey ( entityKey , session );
87
94
Object entity = persistenceContext .getEntity ( entityKey );
88
95
if ( entity != null && !loadOptions .isReturnOfDeletedEntitiesEnabled () ) {
89
96
// make sure it is not DELETED
@@ -102,71 +109,52 @@ protected void handleResults(
102
109
103
110
@ Override
104
111
protected int maxBatchSize (Object [] ids , MultiIdLoadOptions loadOptions ) {
105
- if ( loadOptions .getBatchSize () != null && loadOptions .getBatchSize () > 0 ) {
106
- return loadOptions .getBatchSize ();
107
- }
108
- else {
109
- // disable batching by default
110
- return ids .length ;
111
- // return getSessionFactory().getJdbcServices().getJdbcEnvironment().getDialect()
112
- // .getBatchLoadSizingStrategy().determineOptimalBatchLoadSize(
113
- // idJdbcTypeCount,
114
- // ids.length,
115
- // getSessionFactory().getSessionFactoryOptions().inClauseParameterPaddingEnabled()
116
- // );
117
- }
112
+ final Integer explicitBatchSize = loadOptions .getBatchSize ();
113
+ return explicitBatchSize != null && explicitBatchSize > 0
114
+ ? explicitBatchSize
115
+ // disable batching by default
116
+ : ids .length ;
118
117
}
119
118
120
119
@ Override
121
120
protected void loadEntitiesById (
122
- List <Object > idsToLoadFromDatabase ,
121
+ List <Object > idsInBatch ,
123
122
LockOptions lockOptions ,
124
123
MultiIdLoadOptions loadOptions ,
125
124
EventSource session ) {
126
- final SelectStatement sqlAst = LoaderSelectBuilder . createSelectBySingleArrayParameter (
125
+ final SelectStatement sqlAst = createSelectBySingleArrayParameter (
127
126
getLoadable (),
128
127
getIdentifierMapping (),
129
128
session .getLoadQueryInfluencers (),
130
129
lockOptions ,
131
130
jdbcParameter ,
132
131
getSessionFactory ()
133
132
);
134
- final JdbcOperationQuerySelect jdbcSelectOperation = getSessionFactory ().getJdbcServices ()
135
- .getJdbcEnvironment ()
136
- .getSqlAstTranslatorFactory ()
137
- .buildSelectTranslator ( getSessionFactory (), sqlAst )
138
- .translate ( JdbcParameterBindings .NO_BINDINGS , QueryOptions .NONE );
139
133
140
134
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl (1 );
141
- jdbcParameterBindings .addBinding (
142
- jdbcParameter ,
143
- new JdbcParameterBindingImpl ( arrayJdbcMapping , idsToLoadFromDatabase .toArray ( createTypedArray (0 ) ) )
144
- );
145
-
146
- final PersistenceContext persistenceContext = session .getPersistenceContext ();
147
- final BatchFetchQueue batchFetchQueue = persistenceContext .getBatchFetchQueue ();
135
+ jdbcParameterBindings .addBinding ( jdbcParameter ,
136
+ new JdbcParameterBindingImpl ( arrayJdbcMapping , idsInBatch .toArray ( createTypedArray (0 ) ) ) );
148
137
149
- final SubselectFetch .RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch .createRegistrationHandler (
150
- batchFetchQueue ,
151
- sqlAst ,
152
- JdbcParametersList .singleton ( jdbcParameter ),
153
- jdbcParameterBindings
154
- );
155
-
156
- session .getJdbcServices ().getJdbcSelectExecutor ().executeQuery (
157
- jdbcSelectOperation ,
138
+ getJdbcSelectExecutor ().executeQuery (
139
+ getSqlAstTranslatorFactory ().buildSelectTranslator ( getSessionFactory (), sqlAst )
140
+ .translate ( NO_BINDINGS , QueryOptions .NONE ),
158
141
jdbcParameterBindings ,
159
- new ExecutionContextWithSubselectFetchHandler ( session ,
160
- subSelectFetchableKeysHandler ,
142
+ new ExecutionContextWithSubselectFetchHandler (
143
+ session ,
144
+ createRegistrationHandler (
145
+ session .getPersistenceContext ().getBatchFetchQueue (),
146
+ sqlAst ,
147
+ JdbcParametersList .singleton ( jdbcParameter ),
148
+ jdbcParameterBindings
149
+ ),
161
150
TRUE .equals ( loadOptions .getReadOnly ( session ) ) ),
162
151
RowTransformerStandardImpl .instance (),
163
152
null ,
164
- idsToLoadFromDatabase .size (),
153
+ idsInBatch .size (),
165
154
ManagedResultConsumer .INSTANCE
166
155
);
167
156
}
168
157
169
-
170
158
@ Override
171
159
protected <K > List <E > performUnorderedMultiLoad (
172
160
K [] ids ,
@@ -180,7 +168,7 @@ protected <K> List<E> performUnorderedMultiLoad(
180
168
}
181
169
182
170
final List <E > result = CollectionHelper .arrayList ( ids .length );
183
- final LockOptions lockOptions = ( loadOptions .getLockOptions () == null )
171
+ final LockOptions lockOptions = loadOptions .getLockOptions () == null
184
172
? new LockOptions ( LockMode .NONE )
185
173
: loadOptions .getLockOptions ();
186
174
@@ -198,21 +186,19 @@ protected <K> List<E> performUnorderedMultiLoad(
198
186
return result ;
199
187
}
200
188
201
- final SelectStatement sqlAst = LoaderSelectBuilder . createSelectBySingleArrayParameter (
189
+ final SelectStatement sqlAst = createSelectBySingleArrayParameter (
202
190
getLoadable (),
203
191
getIdentifierMapping (),
204
192
session .getLoadQueryInfluencers (),
205
193
lockOptions ,
206
194
jdbcParameter ,
207
195
getSessionFactory ()
208
196
);
209
- final JdbcOperationQuerySelect jdbcSelectOperation = getSessionFactory ().getJdbcServices ()
210
- .getJdbcEnvironment ()
211
- .getSqlAstTranslatorFactory ()
212
- .buildSelectTranslator ( getSessionFactory (), sqlAst )
213
- .translate ( JdbcParameterBindings .NO_BINDINGS , QueryOptions .NONE );
197
+ final JdbcOperationQuerySelect jdbcSelectOperation =
198
+ getSqlAstTranslatorFactory ().buildSelectTranslator ( getSessionFactory (), sqlAst )
199
+ .translate ( NO_BINDINGS , QueryOptions .NONE );
214
200
215
- final List <E > databaseResults = LoaderHelper . loadByArrayParameter (
201
+ final List <E > databaseResults = loadByArrayParameter (
216
202
idsToLoadFromDatabase ,
217
203
sqlAst ,
218
204
jdbcSelectOperation ,
@@ -236,7 +222,7 @@ protected <K> List<E> performUnorderedMultiLoad(
236
222
continue ;
237
223
}
238
224
// found or not, remove the key from the batch-fetch queue
239
- BatchFetchQueueHelper . removeBatchLoadableEntityKey ( id , getLoadable (), session );
225
+ removeBatchLoadableEntityKey ( id , getLoadable (), session );
240
226
}
241
227
242
228
return result ;
@@ -272,7 +258,7 @@ protected final <R,K> K[] processResolvableEntities(
272
258
getLoadable ().getJavaType ().getJavaTypeClass ().getName (),
273
259
lockOptions ,
274
260
session ,
275
- LoaderHelper . getReadOnlyFromLoadQueryInfluencers ( session )
261
+ getReadOnlyFromLoadQueryInfluencers ( session )
276
262
);
277
263
278
264
Object managedEntity = null ;
0 commit comments