16
16
import org .hibernate .AnnotationException ;
17
17
import org .hibernate .CacheMode ;
18
18
import org .hibernate .FlushMode ;
19
+ import org .hibernate .LockOptions ;
19
20
import org .hibernate .annotations .FlushModeType ;
20
21
import org .hibernate .annotations .HQLSelect ;
21
22
import org .hibernate .annotations .SQLSelect ;
33
34
import org .hibernate .boot .spi .MetadataBuildingContext ;
34
35
import org .hibernate .internal .CoreMessageLogger ;
35
36
import org .hibernate .internal .log .DeprecationLogger ;
36
- import org .hibernate .internal .util .collections .ArrayHelper ;
37
37
import org .hibernate .jpa .HibernateHints ;
38
38
import org .hibernate .models .internal .util .StringHelper ;
39
39
import org .hibernate .models .spi .ClassDetails ;
56
56
57
57
import static java .lang .Boolean .TRUE ;
58
58
import static org .hibernate .internal .util .StringHelper .nullIfEmpty ;
59
+ import static org .hibernate .internal .util .collections .ArrayHelper .isEmpty ;
59
60
import static org .hibernate .internal .util .collections .CollectionHelper .determineProperSizing ;
60
61
import static org .hibernate .internal .util .collections .CollectionHelper .setOf ;
61
62
@@ -80,6 +81,7 @@ public static void bindQuery(
80
81
81
82
final String queryName = namedQuery .name ();
82
83
final String queryString = namedQuery .query ();
84
+ final Class <?> resultClass = namedQuery .resultClass ();
83
85
84
86
if ( queryName .isEmpty () ) {
85
87
throw new AnnotationException ( "Class or package level '@NamedQuery' annotation must specify a 'name'" );
@@ -90,21 +92,9 @@ public static void bindQuery(
90
92
}
91
93
92
94
final QueryHintDefinition hints = new QueryHintDefinition ( queryName , namedQuery .hints () );
93
-
94
- final NamedHqlQueryDefinition <?> queryMapping = new NamedHqlQueryDefinitionImpl .Builder <>( queryName )
95
- .setHqlString ( queryString )
96
- .setResultClass ( (Class <Object >) namedQuery .resultClass () )
97
- .setCacheable ( hints .getCacheability () )
98
- .setCacheMode ( hints .getCacheMode () )
99
- .setCacheRegion ( hints .getString ( HibernateHints .HINT_CACHE_REGION ) )
100
- .setTimeout ( hints .getTimeout () )
101
- .setFetchSize ( hints .getInteger ( HibernateHints .HINT_FETCH_SIZE ) )
102
- .setFlushMode ( hints .getFlushMode () )
103
- .setReadOnly ( hints .getBooleanWrapper ( HibernateHints .HINT_READ_ONLY ) )
104
- .setLockOptions ( hints .determineLockOptions ( namedQuery ) )
105
- .setComment ( hints .getString ( HibernateHints .HINT_COMMENT ) )
106
- .build ();
107
-
95
+ final NamedHqlQueryDefinition <?> queryMapping =
96
+ createNamedQueryDefinition ( queryName , queryString , resultClass ,
97
+ hints .determineLockOptions ( namedQuery ), hints );
108
98
if ( isDefault ) {
109
99
context .getMetadataCollector ().addDefaultQuery ( queryMapping );
110
100
}
@@ -113,13 +103,22 @@ public static void bindQuery(
113
103
}
114
104
}
115
105
116
- private static Class <Object > loadClass (ClassDetails classDetails , MetadataBuildingContext context ) {
117
- return ClassDetails .VOID_CLASS_DETAILS == classDetails
118
- ? null
119
- : context .getBootstrapContext ()
120
- .getServiceRegistry ()
121
- .requireService ( ClassLoaderService .class )
122
- .classForName ( classDetails .getName () );
106
+ private static <T > NamedHqlQueryDefinitionImpl <T > createNamedQueryDefinition (
107
+ String queryName , String queryString , Class <T > resultClass , LockOptions lockOptions ,
108
+ QueryHintDefinition hints ) {
109
+ return new NamedHqlQueryDefinitionImpl .Builder <T >(queryName )
110
+ .setHqlString (queryString )
111
+ .setResultClass (resultClass )
112
+ .setCacheable (hints .getCacheability ())
113
+ .setCacheMode (hints .getCacheMode ())
114
+ .setCacheRegion (hints .getString (HibernateHints .HINT_CACHE_REGION ))
115
+ .setTimeout (hints .getTimeout ())
116
+ .setFetchSize (hints .getInteger (HibernateHints .HINT_FETCH_SIZE ))
117
+ .setFlushMode (hints .getFlushMode ())
118
+ .setReadOnly (hints .getBooleanWrapper (HibernateHints .HINT_READ_ONLY ))
119
+ .setLockOptions (lockOptions )
120
+ .setComment (hints .getString (HibernateHints .HINT_COMMENT ))
121
+ .build ();
123
122
}
124
123
125
124
public static void bindNativeQuery (
@@ -141,29 +140,14 @@ public static void bindNativeQuery(
141
140
142
141
final String resultSetMappingName = namedNativeQuery .resultSetMapping ();
143
142
final Class <?> resultClassDetails = namedNativeQuery .resultClass ();
144
- final Class <Object > resultClass = void .class == resultClassDetails
145
- ? null
146
- : (Class <Object >) resultClassDetails ;
147
-
148
- final NamedNativeQueryDefinition .Builder <?> builder = new NamedNativeQueryDefinition .Builder <>( registrationName )
149
- .setSqlString ( queryString )
150
- .setResultClass ( resultClass )
151
- .setResultSetMappingName ( resultSetMappingName )
152
- .setQuerySpaces ( null )
153
- .setCacheable ( hints .getCacheability () )
154
- .setCacheMode ( hints .getCacheMode () )
155
- .setCacheRegion ( hints .getString ( HibernateHints .HINT_CACHE_REGION ) )
156
- .setTimeout ( hints .getTimeout () )
157
- .setFetchSize ( hints .getInteger ( HibernateHints .HINT_FETCH_SIZE ) )
158
- .setFlushMode ( hints .getFlushMode () )
159
- .setReadOnly ( hints .getBooleanWrapper ( HibernateHints .HINT_READ_ONLY ) )
160
- .setComment ( hints .getString ( HibernateHints .HINT_COMMENT ) )
161
- .addHints ( hints .getHintsMap () );
162
-
163
- final NamedNativeQueryDefinition <?> queryDefinition = builder .build ();
143
+ final Class <?> resultClass = void .class == resultClassDetails ? null : resultClassDetails ;
144
+
145
+ final NamedNativeQueryDefinition <?> queryDefinition =
146
+ createNamedQueryDefinition ( registrationName , queryString , resultClass , resultSetMappingName , hints );
164
147
165
148
if ( LOG .isDebugEnabled () ) {
166
- LOG .debugf ( "Binding named native query: %s => %s" , queryDefinition .getRegistrationName (), queryDefinition .getSqlQueryString () );
149
+ LOG .debugf ( "Binding named native query: %s => %s" ,
150
+ queryDefinition .getRegistrationName (), queryDefinition .getSqlQueryString () );
167
151
}
168
152
169
153
if ( isDefault ) {
@@ -174,6 +158,27 @@ public static void bindNativeQuery(
174
158
}
175
159
}
176
160
161
+ private static <T > NamedNativeQueryDefinition <T > createNamedQueryDefinition (
162
+ String registrationName , String queryString ,
163
+ Class <T > resultClass , String resultSetMappingName ,
164
+ QueryHintDefinition hints ) {
165
+ return new NamedNativeQueryDefinition .Builder <T >(registrationName )
166
+ .setSqlString (queryString )
167
+ .setResultClass (resultClass )
168
+ .setResultSetMappingName (resultSetMappingName )
169
+ .setQuerySpaces (null )
170
+ .setCacheable (hints .getCacheability ())
171
+ .setCacheMode (hints .getCacheMode ())
172
+ .setCacheRegion (hints .getString (HibernateHints .HINT_CACHE_REGION ))
173
+ .setTimeout (hints .getTimeout ())
174
+ .setFetchSize (hints .getInteger (HibernateHints .HINT_FETCH_SIZE ))
175
+ .setFlushMode (hints .getFlushMode ())
176
+ .setReadOnly (hints .getBooleanWrapper (HibernateHints .HINT_READ_ONLY ))
177
+ .setComment (hints .getString (HibernateHints .HINT_COMMENT ))
178
+ .addHints (hints .getHintsMap ())
179
+ .build ();
180
+ }
181
+
177
182
public static void bindNativeQuery (
178
183
String name ,
179
184
SQLSelect sqlSelect ,
@@ -192,10 +197,11 @@ public static void bindNativeQuery(
192
197
}
193
198
194
199
final SqlResultSetMapping resultSetMapping = sqlSelect .resultSetMapping ();
195
- if ( !ArrayHelper .isEmpty ( resultSetMapping .columns () )
196
- || !ArrayHelper .isEmpty ( resultSetMapping .entities () )
197
- || !ArrayHelper .isEmpty ( resultSetMapping .classes () ) ) {
198
- context .getMetadataCollector ().addResultSetMapping ( SqlResultSetMappingDescriptor .from ( resultSetMapping , name ) );
200
+ if ( !isEmpty ( resultSetMapping .columns () )
201
+ || !isEmpty ( resultSetMapping .entities () )
202
+ || !isEmpty ( resultSetMapping .classes () ) ) {
203
+ context .getMetadataCollector ()
204
+ .addResultSetMapping ( SqlResultSetMappingDescriptor .from ( resultSetMapping , name ) );
199
205
builder .setResultSetMappingName ( name );
200
206
}
201
207
@@ -211,37 +217,21 @@ public static void bindNativeQuery(
211
217
212
218
final String registrationName = namedNativeQuery .name ();
213
219
214
- //ResultSetMappingDefinition mappingDefinition = mappings.getJdbcValuesMappingProducer( queryAnn.resultSetMapping() );
215
220
if ( registrationName .isEmpty () ) {
216
221
throw new AnnotationException ( "Class or package level '@NamedNativeQuery' annotation must specify a 'name'" );
217
222
}
218
223
219
224
final String resultSetMappingName = namedNativeQuery .resultSetMapping ();
220
225
final Class <?> resultClassDetails = namedNativeQuery .resultClass ();
221
- final Class <Object > resultClass = resultClassDetails == void .class
222
- ? null
223
- : (Class <Object >) resultClassDetails ;
224
-
225
- final Integer timeout = namedNativeQuery .timeout ();
226
- final Integer fetchSize = namedNativeQuery .fetchSize ();
226
+ final Class <?> resultClass = resultClassDetails == void .class ? null : resultClassDetails ;
227
227
228
228
final String [] querySpacesList = namedNativeQuery .querySpaces ();
229
229
final HashSet <String > querySpaces = new HashSet <>( determineProperSizing ( querySpacesList .length ) );
230
230
Collections .addAll ( querySpaces , querySpacesList );
231
231
232
- final NamedNativeQueryDefinition .Builder <?> builder = new NamedNativeQueryDefinition .Builder <>( registrationName )
233
- .setSqlString ( namedNativeQuery .query () )
234
- .setResultSetMappingName ( resultSetMappingName )
235
- .setResultClass ( resultClass )
236
- .setCacheable ( namedNativeQuery .cacheable () )
237
- .setCacheRegion ( nullIfEmpty ( namedNativeQuery .cacheRegion () ) )
238
- .setCacheMode ( getCacheMode ( namedNativeQuery .cacheRetrieveMode (), namedNativeQuery .cacheStoreMode () ) )
239
- .setTimeout ( timeout < 0 ? null : timeout )
240
- .setFetchSize ( fetchSize < 0 ? null : fetchSize )
241
- .setFlushMode ( getFlushMode ( namedNativeQuery .flushMode () ) )
242
- .setReadOnly ( namedNativeQuery .readOnly () )
243
- .setQuerySpaces ( querySpaces )
244
- .setComment ( nullIfEmpty ( namedNativeQuery .comment () ) );
232
+ final NamedNativeQueryDefinition .Builder <?> builder =
233
+ createQueryDefinition ( namedNativeQuery , registrationName , resultSetMappingName , resultClass ,
234
+ namedNativeQuery .timeout (), namedNativeQuery .fetchSize (), querySpaces );
245
235
246
236
if ( TRUE == namedNativeQuery .callable () ) {
247
237
final NamedProcedureCallDefinition definition =
@@ -266,6 +256,27 @@ public static void bindNativeQuery(
266
256
}
267
257
}
268
258
259
+ private static <T > NamedNativeQueryDefinition .Builder <T > createQueryDefinition (
260
+ org .hibernate .annotations .NamedNativeQuery namedNativeQuery ,
261
+ String registrationName , String resultSetMappingName ,
262
+ Class <T > resultClass ,
263
+ int timeout , int fetchSize ,
264
+ HashSet <String > querySpaces ) {
265
+ return new NamedNativeQueryDefinition .Builder <T >(registrationName )
266
+ .setSqlString (namedNativeQuery .query ())
267
+ .setResultSetMappingName (resultSetMappingName )
268
+ .setResultClass (resultClass )
269
+ .setCacheable (namedNativeQuery .cacheable ())
270
+ .setCacheRegion (nullIfEmpty (namedNativeQuery .cacheRegion ()))
271
+ .setCacheMode (getCacheMode (namedNativeQuery .cacheRetrieveMode (), namedNativeQuery .cacheStoreMode ()))
272
+ .setTimeout (timeout < 0 ? null : timeout )
273
+ .setFetchSize (fetchSize < 0 ? null : fetchSize )
274
+ .setFlushMode (getFlushMode (namedNativeQuery .flushMode ()))
275
+ .setReadOnly (namedNativeQuery .readOnly ())
276
+ .setQuerySpaces (querySpaces )
277
+ .setComment (nullIfEmpty (namedNativeQuery .comment ()));
278
+ }
279
+
269
280
/**
270
281
* Handles legacy cases where a named native query was used to specify a procedure call
271
282
*
@@ -284,15 +295,17 @@ public static NamedProcedureCallDefinition createStoredProcedure(
284
295
285
296
final SourceModelBuildingContext sourceModelBuildingContext = context .getMetadataCollector ()
286
297
.getSourceModelBuildingContext ();
287
- final NamedStoredProcedureQueryJpaAnnotation nameStoredProcedureQueryAnn = JpaAnnotations .NAMED_STORED_PROCEDURE_QUERY .createUsage ( sourceModelBuildingContext );
298
+ final NamedStoredProcedureQueryJpaAnnotation nameStoredProcedureQueryAnn =
299
+ JpaAnnotations .NAMED_STORED_PROCEDURE_QUERY .createUsage ( sourceModelBuildingContext );
288
300
nameStoredProcedureQueryAnn .name ( builder .getName () );
289
301
nameStoredProcedureQueryAnn .procedureName ( jdbcCall .callableName );
290
302
291
303
final StoredProcedureParameter [] parameters = new StoredProcedureParameter [jdbcCall .parameters .size ()];
292
304
nameStoredProcedureQueryAnn .parameters ( parameters );
293
305
294
306
for ( int i = 0 ; i < jdbcCall .parameters .size (); i ++ ) {
295
- final StoredProcedureParameterJpaAnnotation param = JpaAnnotations .STORED_PROCEDURE_PARAMETER .createUsage ( sourceModelBuildingContext );
307
+ final StoredProcedureParameterJpaAnnotation param =
308
+ JpaAnnotations .STORED_PROCEDURE_PARAMETER .createUsage ( sourceModelBuildingContext );
296
309
parameters [i ] = param ;
297
310
298
311
final String paramName = jdbcCall .parameters .get ( i );
@@ -309,7 +322,8 @@ public static NamedProcedureCallDefinition createStoredProcedure(
309
322
.getTypeConfiguration ()
310
323
.getBasicTypeRegistry ()
311
324
.getRegisteredType ( typeName );
312
- classDetails = context .getMetadataCollector ().getClassDetailsRegistry ().getClassDetails ( registeredType .getJavaType ().getName () );
325
+ classDetails = context .getMetadataCollector ().getClassDetailsRegistry ()
326
+ .getClassDetails ( registeredType .getJavaType ().getName () );
313
327
}
314
328
param .type ( classDetails .toJavaClass () );
315
329
}
@@ -326,15 +340,17 @@ public static NamedProcedureCallDefinition createStoredProcedure(
326
340
final List <QueryHintJpaAnnotation > hints = new ArrayList <>();
327
341
328
342
if ( builder .getQuerySpaces () != null ) {
329
- final QueryHintJpaAnnotation hint = JpaAnnotations .QUERY_HINT .createUsage ( sourceModelBuildingContext );
343
+ final QueryHintJpaAnnotation hint =
344
+ JpaAnnotations .QUERY_HINT .createUsage ( sourceModelBuildingContext );
330
345
hint .name ( HibernateHints .HINT_NATIVE_SPACES );
331
346
hint .value ( String .join ( " " , builder .getQuerySpaces () ) );
332
347
hints .add ( hint );
333
348
}
334
349
335
350
if ( jdbcCall .resultParameter ) {
336
351
// Mark native queries that have a result parameter as callable functions
337
- final QueryHintJpaAnnotation hint = JpaAnnotations .QUERY_HINT .createUsage ( sourceModelBuildingContext );
352
+ final QueryHintJpaAnnotation hint =
353
+ JpaAnnotations .QUERY_HINT .createUsage ( sourceModelBuildingContext );
338
354
hint .name ( HibernateHints .HINT_CALLABLE_FUNCTION );
339
355
hint .value ( "true" );
340
356
hints .add ( hint );
@@ -365,36 +381,42 @@ public static void bindQuery(
365
381
}
366
382
367
383
final String registrationName = namedQuery .name ();
384
+ final Class <?> resultClass = namedQuery .resultClass ();
368
385
369
- //ResultSetMappingDefinition mappingDefinition = mappings.getJdbcValuesMappingProducer( namedQuery.resultSetMapping() );
370
386
if ( registrationName .isEmpty () ) {
371
387
throw new AnnotationException ( "Class or package level '@NamedQuery' annotation must specify a 'name'" );
372
388
}
373
389
374
- final int timeout = namedQuery .timeout ();
375
- final int fetchSize = namedQuery .fetchSize ();
376
-
377
- final NamedHqlQueryDefinition .Builder <?> builder = new NamedHqlQueryDefinition .Builder <>( registrationName )
378
- .setHqlString ( namedQuery .query () )
379
- .setResultClass ( (Class <Object >) namedQuery .resultClass () )
380
- .setCacheable ( namedQuery .cacheable () )
381
- .setCacheRegion ( nullIfEmpty ( namedQuery .cacheRegion () ) )
382
- .setCacheMode ( getCacheMode ( namedQuery .cacheRetrieveMode (), namedQuery .cacheStoreMode () ) )
383
- .setTimeout ( timeout < 0 ? null : timeout )
384
- .setFetchSize ( fetchSize < 0 ? null : fetchSize )
385
- .setFlushMode ( getFlushMode ( namedQuery .flushMode () ) )
386
- .setReadOnly ( namedQuery .readOnly () )
387
- .setComment ( nullIfEmpty ( namedQuery .comment () ) );
390
+ final NamedHqlQueryDefinition .Builder <?> builder =
391
+ createQueryDefinition ( namedQuery , registrationName , resultClass ,
392
+ namedQuery .timeout (), namedQuery .fetchSize () ) ;
388
393
389
394
final NamedHqlQueryDefinitionImpl <?> hqlQueryDefinition = builder .build ();
390
395
391
396
if ( LOG .isDebugEnabled () ) {
392
- LOG .debugf ( "Binding named query: %s => %s" , hqlQueryDefinition .getRegistrationName (), hqlQueryDefinition .getHqlString () );
397
+ LOG .debugf ( "Binding named query: %s => %s" ,
398
+ hqlQueryDefinition .getRegistrationName (), hqlQueryDefinition .getHqlString () );
393
399
}
394
400
395
401
context .getMetadataCollector ().addNamedQuery ( hqlQueryDefinition );
396
402
}
397
403
404
+ private static <T > NamedHqlQueryDefinition .Builder <T > createQueryDefinition (
405
+ org .hibernate .annotations .NamedQuery namedQuery ,
406
+ String registrationName , Class <T > resultClass , int timeout , int fetchSize ) {
407
+ return new NamedHqlQueryDefinition .Builder <T >(registrationName )
408
+ .setHqlString (namedQuery .query ())
409
+ .setResultClass (resultClass )
410
+ .setCacheable (namedQuery .cacheable ())
411
+ .setCacheRegion (nullIfEmpty (namedQuery .cacheRegion ()))
412
+ .setCacheMode (getCacheMode (namedQuery .cacheRetrieveMode (), namedQuery .cacheStoreMode ()))
413
+ .setTimeout (timeout < 0 ? null : timeout )
414
+ .setFetchSize (fetchSize < 0 ? null : fetchSize )
415
+ .setFlushMode (getFlushMode (namedQuery .flushMode ()))
416
+ .setReadOnly (namedQuery .readOnly ())
417
+ .setComment (nullIfEmpty (namedQuery .comment ()));
418
+ }
419
+
398
420
private static CacheMode getCacheMode (CacheRetrieveMode cacheRetrieveMode , CacheStoreMode cacheStoreMode ) {
399
421
final CacheMode cacheMode = CacheMode .fromJpaModes ( cacheRetrieveMode , cacheStoreMode );
400
422
return cacheMode == null ? CacheMode .NORMAL : cacheMode ;
@@ -419,14 +441,16 @@ public static void bindNamedStoredProcedureQuery(
419
441
throw new AnnotationException ( "Class or package level '@NamedStoredProcedureQuery' annotation must specify a 'name'" );
420
442
}
421
443
422
- final NamedProcedureCallDefinitionImpl definition = new NamedProcedureCallDefinitionImpl ( namedStoredProcedureQuery );
444
+ final NamedProcedureCallDefinitionImpl definition =
445
+ new NamedProcedureCallDefinitionImpl ( namedStoredProcedureQuery );
423
446
if ( isDefault ) {
424
447
context .getMetadataCollector ().addDefaultNamedProcedureCall ( definition );
425
448
}
426
449
else {
427
450
context .getMetadataCollector ().addNamedProcedureCallDefinition ( definition );
428
451
}
429
- LOG .debugf ( "Bound named stored procedure query : %s => %s" , definition .getRegistrationName (), definition .getProcedureName () );
452
+ LOG .debugf ( "Bound named stored procedure query : %s => %s" ,
453
+ definition .getRegistrationName (), definition .getProcedureName () );
430
454
}
431
455
}
432
456
@@ -435,7 +459,8 @@ public static void bindSqlResultSetMapping(
435
459
MetadataBuildingContext context ,
436
460
boolean isDefault ) {
437
461
//no need to handle inSecondPass
438
- context .getMetadataCollector ().addSecondPass ( new ResultSetMappingSecondPass ( resultSetMappingAnn , context , isDefault ) );
462
+ context .getMetadataCollector ()
463
+ .addSecondPass ( new ResultSetMappingSecondPass ( resultSetMappingAnn , context , isDefault ) );
439
464
}
440
465
441
466
private static class JdbcCall {
0 commit comments