25
25
26
26
import java .util .Collection ;
27
27
import java .util .HashMap ;
28
+
28
29
import javax .persistence .LockModeType ;
29
30
import javax .persistence .ParameterMode ;
30
31
41
42
import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
42
43
import org .hibernate .engine .query .spi .sql .NativeSQLQueryRootReturn ;
43
44
import org .hibernate .engine .spi .NamedQueryDefinitionBuilder ;
44
- import org .hibernate .engine .spi .NamedSQLQueryDefinition ;
45
45
import org .hibernate .engine .spi .NamedSQLQueryDefinitionBuilder ;
46
46
import org .hibernate .internal .CoreLogging ;
47
47
import org .hibernate .internal .CoreMessageLogger ;
52
52
import org .hibernate .metamodel .source .internal .annotations .util .HibernateDotNames ;
53
53
import org .hibernate .metamodel .source .internal .annotations .util .JPADotNames ;
54
54
import org .hibernate .metamodel .source .internal .annotations .util .JandexHelper ;
55
-
56
55
import org .jboss .jandex .AnnotationInstance ;
57
56
import org .jboss .jandex .AnnotationValue ;
58
57
@@ -113,7 +112,7 @@ public static void bind(AnnotationBindingContext bindingContext) {
113
112
JPADotNames .NAMED_NATIVE_QUERIES
114
113
);
115
114
for ( AnnotationInstance query : annotations ) {
116
- bindNamedNativeQuery ( query , bindingContext );
115
+ bindNamedNativeQuery ( bindingContext , query );
117
116
}
118
117
119
118
annotations = JandexHelper .collectionAnnotations (
@@ -122,7 +121,7 @@ public static void bind(AnnotationBindingContext bindingContext) {
122
121
JPADotNames .NAMED_STORED_PROCEDURE_QUERIES
123
122
);
124
123
for ( AnnotationInstance query : annotations ) {
125
- bindNamedStoredProcedureQuery ( query , bindingContext );
124
+ bindNamedStoredProcedureQuery ( bindingContext , query );
126
125
}
127
126
128
127
annotations = JandexHelper .collectionAnnotations (
@@ -140,7 +139,7 @@ public static void bind(AnnotationBindingContext bindingContext) {
140
139
HibernateDotNames .NAMED_NATIVE_QUERIES
141
140
);
142
141
for ( AnnotationInstance query : annotations ) {
143
- bindNamedNativeQuery ( query , bindingContext );
142
+ bindNamedNativeQuery ( bindingContext , query );
144
143
}
145
144
}
146
145
@@ -161,8 +160,7 @@ private static void bindNamedQuery(AnnotationBindingContext bindingContext, Anno
161
160
if ( annotation .name ().equals ( JPADotNames .NAMED_QUERY ) ) {
162
161
bindJPANamedQuery ( annotation , builder , name , query , bindingContext );
163
162
} else {
164
- builder .setFlushMode (
165
- getFlushMode ( JandexHelper .getEnumValue ( annotation , "flushMode" , FlushModeType .class , classLoaderService ) ) )
163
+ builder .setFlushMode ( getFlushMode ( JandexHelper .getEnumValue ( annotation , "flushMode" , FlushModeType .class , classLoaderService ) ) )
166
164
.setCacheable ( JandexHelper .getValue ( annotation , "cacheable" , Boolean .class , classLoaderService ) )
167
165
.setCacheRegion ( defaultToNull ( JandexHelper .getValue ( annotation , "cacheRegion" , String .class , classLoaderService ) ) )
168
166
.setFetchSize ( defaultToNull ( JandexHelper .getValue ( annotation , "fetchSize" , Integer .class , classLoaderService ) ) )
@@ -172,52 +170,10 @@ private static void bindNamedQuery(AnnotationBindingContext bindingContext, Anno
172
170
.setReadOnly ( JandexHelper .getValue ( annotation , "readOnly" , Boolean .class , classLoaderService ) );
173
171
}
174
172
175
-
176
173
bindingContext .getMetadataCollector ().addNamedQuery (builder .createNamedQueryDefinition ());
177
174
LOG .debugf ( "Binding named query: %s => %s" , name , query );
178
175
}
179
176
180
- public static FlushMode getFlushMode (FlushModeType flushModeType ) {
181
- FlushMode flushMode ;
182
- switch ( flushModeType ) {
183
- case ALWAYS :
184
- flushMode = FlushMode .ALWAYS ;
185
- break ;
186
- case AUTO :
187
- flushMode = FlushMode .AUTO ;
188
- break ;
189
- case COMMIT :
190
- flushMode = FlushMode .COMMIT ;
191
- break ;
192
- case MANUAL :
193
- flushMode = FlushMode .MANUAL ;
194
- break ;
195
- case PERSISTENCE_CONTEXT :
196
- flushMode = null ;
197
- break ;
198
- default :
199
- throw new AssertionFailure ( "Unknown flushModeType: " + flushModeType );
200
- }
201
- return flushMode ;
202
- }
203
- private static CacheMode getCacheMode (CacheModeType cacheModeType ) {
204
- switch ( cacheModeType ) {
205
- case GET :
206
- return CacheMode .GET ;
207
- case IGNORE :
208
- return CacheMode .IGNORE ;
209
- case NORMAL :
210
- return CacheMode .NORMAL ;
211
- case PUT :
212
- return CacheMode .PUT ;
213
- case REFRESH :
214
- return CacheMode .REFRESH ;
215
- default :
216
- throw new AssertionFailure ( "Unknown cacheModeType: " + cacheModeType );
217
- }
218
- }
219
-
220
-
221
177
private static void bindJPANamedQuery (
222
178
AnnotationInstance annotation ,
223
179
NamedQueryDefinitionBuilder builder ,
@@ -263,21 +219,84 @@ private static void bindJPANamedQuery(
263
219
.setComment ( defaultToNull ( getString ( hints , QueryHints .COMMENT , bindingContext ) ) )
264
220
.setParameterTypes ( null );
265
221
}
266
-
267
- private static void bindNamedNativeQuery (AnnotationInstance annotation , AnnotationBindingContext bindingContext ) {
222
+
223
+ private static void bindNamedNativeQuery (AnnotationBindingContext bindingContext , AnnotationInstance annotation ) {
268
224
final ClassLoaderService classLoaderService = bindingContext .getBuildingOptions ().getServiceRegistry ().getService ( ClassLoaderService .class );
269
- String name = JandexHelper .getValue ( annotation , "name" , String .class , classLoaderService );
225
+ final String name = JandexHelper .getValue ( annotation , "name" , String .class , classLoaderService );
270
226
if ( StringHelper .isEmpty ( name ) ) {
271
227
throw new AnnotationException ( "A named native query must have a name when used in class or package level" );
272
228
}
229
+ NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder ();
230
+ builder .setName ( name );
273
231
274
- String query = JandexHelper .getValue ( annotation , "query" , String .class , classLoaderService );
232
+ final String query = JandexHelper .getValue ( annotation , "query" , String .class , classLoaderService );
233
+ builder .setQuery ( query );
234
+
235
+ if ( annotation .name ().equals ( JPADotNames .NAMED_NATIVE_QUERY ) ) {
236
+ bindJPANamedNativeQuery ( annotation , builder , name , query , bindingContext );
237
+
238
+ final String resultSetMapping = JandexHelper .getValue (
239
+ annotation , "resultSetMapping" , String .class , classLoaderService );
240
+ if ( StringHelper .isNotEmpty ( resultSetMapping ) ) {
241
+ boolean resultSetMappingExists = bindingContext .getMetadataCollector ().getResultSetMappingDefinitions ().containsKey ( resultSetMapping );
242
+ if ( !resultSetMappingExists ) {
243
+ throw new MappingException (
244
+ String .format (
245
+ "Named SQL Query [%s] referenced an non-existent result set mapping [%s] " ,
246
+ name ,
247
+ resultSetMapping
248
+ )
249
+ );
250
+ }
251
+ builder .setResultSetRef ( resultSetMapping );
252
+ }
253
+ else {
254
+ AnnotationValue annotationValue = annotation .value ( "resultClass" );
255
+ NativeSQLQueryRootReturn [] queryRoots ;
256
+ if ( annotationValue == null ) {
257
+ // pure native scalar query
258
+ queryRoots = new NativeSQLQueryRootReturn [0 ];
259
+ }
260
+ else {
261
+ queryRoots = new NativeSQLQueryRootReturn [] {
262
+ new NativeSQLQueryRootReturn (
263
+ "alias1" ,
264
+ annotationValue .asString (),
265
+ new HashMap <String , String []>(),
266
+ LockMode .READ
267
+ )
268
+ };
269
+ }
270
+ builder .setQueryReturns ( queryRoots );
271
+ }
272
+ }
273
+ else {
274
+ builder .setFlushMode ( getFlushMode ( JandexHelper .getEnumValue ( annotation , "flushMode" , FlushModeType .class , classLoaderService ) ) )
275
+ .setCacheable ( JandexHelper .getValue ( annotation , "cacheable" , Boolean .class , classLoaderService ) )
276
+ .setCacheRegion ( defaultToNull ( JandexHelper .getValue ( annotation , "cacheRegion" , String .class , classLoaderService ) ) )
277
+ .setFetchSize ( defaultToNull ( JandexHelper .getValue ( annotation , "fetchSize" , Integer .class , classLoaderService ) ) )
278
+ .setTimeout ( defaultToNull ( JandexHelper .getValue ( annotation , "timeout" , Integer .class , classLoaderService ) ) )
279
+ .setComment ( JandexHelper .getValue ( annotation , "comment" , String .class , classLoaderService ) )
280
+ .setCacheMode ( getCacheMode ( JandexHelper .getValue ( annotation , "cacheMode" , CacheModeType .class , classLoaderService ) ) )
281
+ .setReadOnly ( JandexHelper .getValue ( annotation , "readOnly" , Boolean .class , classLoaderService ) )
282
+ .setCallable ( JandexHelper .getValue ( annotation , "callable" , Boolean .class , classLoaderService ) )
283
+ .setQueryReturns ( new NativeSQLQueryRootReturn [0 ] );
284
+ }
275
285
276
- String resultSetMapping = JandexHelper .getValue ( annotation , "resultSetMapping" , String .class , classLoaderService );
286
+ bindingContext .getMetadataCollector ().addNamedNativeQuery (builder .createNamedQueryDefinition ());
287
+ LOG .debugf ( "Binding named query: %s => %s" , name , query );
288
+ }
277
289
278
- AnnotationInstance [] hints = JandexHelper .getValue ( annotation , "hints" , AnnotationInstance [].class , classLoaderService );
290
+ private static void bindJPANamedNativeQuery (
291
+ AnnotationInstance annotation ,
292
+ NamedSQLQueryDefinitionBuilder builder ,
293
+ String name ,
294
+ String query ,
295
+ AnnotationBindingContext bindingContext ){
296
+ final ClassLoaderService classLoaderService = bindingContext .getBuildingOptions ().getServiceRegistry ().getService ( ClassLoaderService .class );
297
+ AnnotationInstance [] hints = JandexHelper .getValue ( annotation , "hints" , AnnotationInstance [].class ,
298
+ classLoaderService );
279
299
280
- boolean cacheable = getBoolean ( hints , "org.hibernate.cacheable" , name , bindingContext );
281
300
String cacheRegion = getString ( hints , QueryHints .CACHE_REGION , bindingContext );
282
301
if ( StringHelper .isEmpty ( cacheRegion ) ) {
283
302
cacheRegion = null ;
@@ -287,93 +306,63 @@ private static void bindNamedNativeQuery(AnnotationInstance annotation, Annotati
287
306
if ( timeout != null && timeout < 0 ) {
288
307
timeout = null ;
289
308
}
309
+
310
+ builder .setCacheable ( getBoolean ( hints , QueryHints .CACHEABLE , name , bindingContext ) )
311
+ .setCacheRegion ( cacheRegion )
312
+ .setTimeout ( timeout )
313
+ .setFetchSize ( defaultToNull ( getInteger ( hints , QueryHints .FETCH_SIZE , name , bindingContext ) ) )
314
+ .setFlushMode ( getFlushMode ( hints , QueryHints .FLUSH_MODE , name , bindingContext ) )
315
+ .setCacheMode ( getCacheMode ( hints , QueryHints .CACHE_MODE , name , bindingContext ) )
316
+ .setReadOnly ( getBoolean ( hints , QueryHints .READ_ONLY , name , bindingContext ) )
317
+ .setComment ( defaultToNull ( getString ( hints , QueryHints .COMMENT , bindingContext ) ) )
318
+ .setParameterTypes ( null )
319
+ .setCallable ( getBoolean ( hints , QueryHints .CALLABLE , name , bindingContext ) );
320
+ }
290
321
291
- Integer fetchSize = getInteger ( hints , QueryHints .FETCH_SIZE , name , bindingContext );
292
- if ( fetchSize != null && fetchSize < 0 ) {
293
- fetchSize = null ;
294
- }
295
-
296
- FlushMode flushMode = getFlushMode ( hints , QueryHints .FLUSH_MODE , name , bindingContext );
297
- CacheMode cacheMode = getCacheMode ( hints , QueryHints .CACHE_MODE , name , bindingContext );
298
-
299
- boolean readOnly = getBoolean ( hints , QueryHints .READ_ONLY , name , bindingContext );
300
-
301
- String comment = getString ( hints , QueryHints .COMMENT , bindingContext );
302
- if ( StringHelper .isEmpty ( comment ) ) {
303
- comment = null ;
322
+ public static FlushMode getFlushMode (FlushModeType flushModeType ) {
323
+ FlushMode flushMode ;
324
+ switch ( flushModeType ) {
325
+ case ALWAYS :
326
+ flushMode = FlushMode .ALWAYS ;
327
+ break ;
328
+ case AUTO :
329
+ flushMode = FlushMode .AUTO ;
330
+ break ;
331
+ case COMMIT :
332
+ flushMode = FlushMode .COMMIT ;
333
+ break ;
334
+ case MANUAL :
335
+ flushMode = FlushMode .MANUAL ;
336
+ break ;
337
+ case PERSISTENCE_CONTEXT :
338
+ flushMode = null ;
339
+ break ;
340
+ default :
341
+ throw new AssertionFailure ( "Unknown flushModeType: " + flushModeType );
304
342
}
305
-
306
- boolean callable = getBoolean ( hints , QueryHints .CALLABLE , name , bindingContext );
307
- NamedSQLQueryDefinition def ;
308
- if ( StringHelper .isNotEmpty ( resultSetMapping ) ) {
309
- boolean resultSetMappingExists = bindingContext .getMetadataCollector ().getResultSetMappingDefinitions ().containsKey ( resultSetMapping );
310
- if ( !resultSetMappingExists ) {
311
- throw new MappingException (
312
- String .format (
313
- "Named SQL Query [%s] referenced an non-existent result set mapping [%s] " ,
314
- name ,
315
- resultSetMapping
316
- )
317
- );
318
- }
319
- def = new NamedSQLQueryDefinitionBuilder ().setName ( name )
320
- .setQuery ( query )
321
- .setResultSetRef (
322
- resultSetMapping
323
- )
324
- .setQuerySpaces ( null )
325
- .setCacheable ( cacheable )
326
- .setCacheRegion ( cacheRegion )
327
- .setTimeout ( timeout )
328
- .setFetchSize ( fetchSize )
329
- .setFlushMode ( flushMode )
330
- .setCacheMode ( cacheMode )
331
- .setReadOnly ( readOnly )
332
- .setComment ( comment )
333
- .setParameterTypes ( null )
334
- .setCallable ( callable )
335
- .createNamedQueryDefinition ();
343
+ return flushMode ;
344
+ }
345
+
346
+ private static CacheMode getCacheMode (CacheModeType cacheModeType ) {
347
+ switch ( cacheModeType ) {
348
+ case GET :
349
+ return CacheMode .GET ;
350
+ case IGNORE :
351
+ return CacheMode .IGNORE ;
352
+ case NORMAL :
353
+ return CacheMode .NORMAL ;
354
+ case PUT :
355
+ return CacheMode .PUT ;
356
+ case REFRESH :
357
+ return CacheMode .REFRESH ;
358
+ default :
359
+ throw new AssertionFailure ( "Unknown cacheModeType: " + cacheModeType );
336
360
}
337
- else {
338
- AnnotationValue annotationValue = annotation .value ( "resultClass" );
339
- NativeSQLQueryRootReturn [] queryRoots ;
340
- if ( annotationValue == null ) {
341
- // pure native scalar query
342
- queryRoots = new NativeSQLQueryRootReturn [0 ];
343
- }
344
- else {
345
- queryRoots = new NativeSQLQueryRootReturn [] {
346
- new NativeSQLQueryRootReturn (
347
- "alias1" ,
348
- annotationValue .asString (),
349
- new HashMap <String , String []>(),
350
- LockMode .READ
351
- )
352
- };
353
- }
354
- def = new NamedSQLQueryDefinitionBuilder ().setName ( name )
355
- .setQuery ( query )
356
- .setQueryReturns ( queryRoots )
357
- .setQuerySpaces ( null )
358
- .setCacheable ( cacheable )
359
- .setCacheRegion ( cacheRegion )
360
- .setTimeout ( timeout )
361
- .setFetchSize ( fetchSize )
362
- .setFlushMode ( flushMode )
363
- .setCacheMode ( cacheMode )
364
- .setReadOnly ( readOnly )
365
- .setComment ( comment )
366
- .setParameterTypes ( null )
367
- .setCallable ( callable )
368
- .createNamedQueryDefinition ();
369
- }
370
- bindingContext .getMetadataCollector ().addNamedNativeQuery ( def );
371
- LOG .debugf ( "Binding named native query: %s => %s" , name , query );
372
361
}
373
362
374
363
private static void bindNamedStoredProcedureQuery (
375
- AnnotationInstance query ,
376
- AnnotationBindingContext bindingContext ) {
364
+ AnnotationBindingContext bindingContext ,
365
+ AnnotationInstance query ) {
377
366
final String name = query .value ( "name" ).asString ();
378
367
final String procedureName = query .value ( "procedureName" ).asString ();
379
368
LOG .debugf ( "Starting binding of @NamedStoredProcedureQuery(name=%s, procedureName=%s)" , name , procedureName );
0 commit comments