40
40
import org .hibernate .query .QueryFlushMode ;
41
41
import org .hibernate .query .sql .internal .ParameterParser ;
42
42
import org .hibernate .query .sql .spi .ParameterRecognizer ;
43
- import org .hibernate .type .BasicType ;
44
43
45
44
import java .util .ArrayList ;
46
45
import java .util .Collections ;
@@ -71,31 +70,30 @@ public static void bindQuery(
71
70
MetadataBuildingContext context ,
72
71
boolean isDefault ,
73
72
AnnotationTarget annotationTarget ) {
74
- if ( namedQuery == null ) {
75
- return ;
76
- }
77
-
78
- final String queryName = namedQuery .name ();
79
- final String queryString = namedQuery .query ();
80
- final Class <?> resultClass = namedQuery .resultClass ();
81
-
82
- if ( queryName .isBlank () ) {
83
- throw new AnnotationException ( "Class or package level '@NamedQuery' annotation must specify a 'name'" );
84
- }
73
+ if ( namedQuery != null ) {
74
+ final String queryName = namedQuery .name ();
75
+ final String queryString = namedQuery .query ();
76
+ final Class <?> resultClass = namedQuery .resultClass ();
77
+
78
+ if ( queryName .isBlank () ) {
79
+ throw new AnnotationException (
80
+ "Class or package level '@NamedQuery' annotation must specify a 'name'" );
81
+ }
85
82
86
- if ( LOG .isDebugEnabled () ) {
87
- LOG .debugf ( "Binding named query: %s => %s" , queryName , queryString );
88
- }
83
+ if ( LOG .isDebugEnabled () ) {
84
+ LOG .debugf ( "Binding named query: %s => %s" , queryName , queryString );
85
+ }
89
86
90
- final QueryHintDefinition hints = new QueryHintDefinition ( queryName , namedQuery .hints () );
91
- final NamedHqlQueryDefinition <?> queryMapping =
92
- createNamedQueryDefinition ( queryName , queryString , resultClass ,
93
- hints .determineLockOptions ( namedQuery ), hints , annotationTarget );
94
- if ( isDefault ) {
95
- context .getMetadataCollector ().addDefaultQuery ( queryMapping );
96
- }
97
- else {
98
- context .getMetadataCollector ().addNamedQuery ( queryMapping );
87
+ final QueryHintDefinition hints = new QueryHintDefinition ( queryName , namedQuery .hints () );
88
+ final NamedHqlQueryDefinition <?> queryMapping =
89
+ createNamedQueryDefinition ( queryName , queryString , resultClass ,
90
+ hints .determineLockOptions ( namedQuery ), hints , annotationTarget );
91
+ if ( isDefault ) {
92
+ context .getMetadataCollector ().addDefaultQuery ( queryMapping );
93
+ }
94
+ else {
95
+ context .getMetadataCollector ().addNamedQuery ( queryMapping );
96
+ }
99
97
}
100
98
}
101
99
@@ -286,10 +284,29 @@ public static NamedProcedureCallDefinition createStoredProcedure(
286
284
JpaAnnotations .NAMED_STORED_PROCEDURE_QUERY .createUsage ( modelsContext );
287
285
nameStoredProcedureQueryAnn .name ( builder .getName () );
288
286
nameStoredProcedureQueryAnn .procedureName ( jdbcCall .callableName );
287
+ nameStoredProcedureQueryAnn .parameters ( parametersAsAnnotations ( builder , context , jdbcCall ) );
288
+
289
+ final String resultSetMappingName = builder .getResultSetMappingName ();
290
+ if ( resultSetMappingName != null ) {
291
+ nameStoredProcedureQueryAnn .resultSetMappings ( new String [] {resultSetMappingName } );
292
+ }
293
+
294
+ final Class <?> resultClass = builder .getResultClass ();
295
+ if ( resultClass != null ) {
296
+ nameStoredProcedureQueryAnn .resultClasses ( new Class []{ builder .getResultClass () } );
297
+ }
298
+
299
+ final List <QueryHint > hints = hintsAsAnnotations ( builder , modelsContext , jdbcCall );
300
+ nameStoredProcedureQueryAnn .hints ( hints .toArray (QueryHint []::new ) );
289
301
290
- final StoredProcedureParameter [] parameters = new StoredProcedureParameter [ jdbcCall . parameters . size ()] ;
291
- nameStoredProcedureQueryAnn . parameters ( parameters );
302
+ return new NamedProcedureCallDefinitionImpl ( nameStoredProcedureQueryAnn ) ;
303
+ }
292
304
305
+ private static StoredProcedureParameter [] parametersAsAnnotations (
306
+ NamedNativeQueryDefinition .Builder <?> builder , MetadataBuildingContext context , JdbcCall jdbcCall ) {
307
+ final ModelsContext modelsContext = context .getBootstrapContext ().getModelsContext ();
308
+ final StoredProcedureParameter [] parameters =
309
+ new StoredProcedureParameter [jdbcCall .parameters .size ()];
293
310
for ( int i = 0 ; i < jdbcCall .parameters .size (); i ++ ) {
294
311
final StoredProcedureParameterJpaAnnotation param =
295
312
JpaAnnotations .STORED_PROCEDURE_PARAMETER .createUsage ( modelsContext );
@@ -300,40 +317,25 @@ public static NamedProcedureCallDefinition createStoredProcedure(
300
317
param .mode ( ParameterMode .IN );
301
318
302
319
final String typeName = builder .getParameterTypes ().get ( paramName );
303
- final ClassDetails classDetails ;
304
- if ( isEmpty ( typeName ) ) {
305
- classDetails = ClassDetails .VOID_CLASS_DETAILS ;
306
- }
307
- else {
308
- final BasicType <Object > registeredType = context .getBootstrapContext ()
309
- .getTypeConfiguration ()
310
- .getBasicTypeRegistry ()
311
- .getRegisteredType ( typeName );
312
- classDetails = context .getMetadataCollector ().getClassDetailsRegistry ()
313
- .getClassDetails ( registeredType .getJavaType ().getName () );
314
- }
320
+ final ClassDetails classDetails =
321
+ isEmpty ( typeName )
322
+ ? ClassDetails .VOID_CLASS_DETAILS
323
+ : classDetails ( context , typeName );
315
324
param .type ( classDetails .toJavaClass () );
316
325
}
326
+ return parameters ;
327
+ }
317
328
318
- if ( builder .getResultSetMappingName () != null ) {
319
- nameStoredProcedureQueryAnn .resultSetMappings ( new String [] { builder .getResultSetMappingName () } );
320
- }
321
-
322
- final Class <?> resultClass = builder .getResultClass ();
323
- if ( resultClass != null ) {
324
- nameStoredProcedureQueryAnn .resultClasses ( new Class []{ builder .getResultClass () } );
325
- }
326
-
327
- final List <QueryHintJpaAnnotation > hints = new ArrayList <>();
328
-
329
+ private static List <QueryHint > hintsAsAnnotations (
330
+ NamedNativeQueryDefinition .Builder <?> builder , ModelsContext modelsContext , JdbcCall jdbcCall ) {
331
+ final List <QueryHint > hints = new ArrayList <>();
329
332
if ( builder .getQuerySpaces () != null ) {
330
333
final QueryHintJpaAnnotation hint =
331
334
JpaAnnotations .QUERY_HINT .createUsage ( modelsContext );
332
335
hint .name ( HibernateHints .HINT_NATIVE_SPACES );
333
336
hint .value ( String .join ( " " , builder .getQuerySpaces () ) );
334
337
hints .add ( hint );
335
338
}
336
-
337
339
if ( jdbcCall .resultParameter ) {
338
340
// Mark native queries that have a result parameter as callable functions
339
341
final QueryHintJpaAnnotation hint =
@@ -342,10 +344,15 @@ public static NamedProcedureCallDefinition createStoredProcedure(
342
344
hint .value ( "true" );
343
345
hints .add ( hint );
344
346
}
347
+ return hints ;
348
+ }
345
349
346
- nameStoredProcedureQueryAnn .hints ( hints .toArray (QueryHint []::new ) );
347
-
348
- return new NamedProcedureCallDefinitionImpl ( nameStoredProcedureQueryAnn );
350
+ private static ClassDetails classDetails (MetadataBuildingContext context , String typeName ) {
351
+ final String registeredTypeName =
352
+ context .getBootstrapContext ().getTypeConfiguration ().getBasicTypeRegistry ()
353
+ .getRegisteredType ( typeName ).getJavaType ().getName ();
354
+ return context .getMetadataCollector ().getClassDetailsRegistry ()
355
+ .getClassDetails ( registeredTypeName );
349
356
}
350
357
351
358
public static void bindQuery (
0 commit comments