4040import org .hibernate .query .QueryFlushMode ;
4141import org .hibernate .query .sql .internal .ParameterParser ;
4242import org .hibernate .query .sql .spi .ParameterRecognizer ;
43- import org .hibernate .type .BasicType ;
4443
4544import java .util .ArrayList ;
4645import java .util .Collections ;
@@ -71,31 +70,30 @@ public static void bindQuery(
7170 MetadataBuildingContext context ,
7271 boolean isDefault ,
7372 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+ }
8582
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+ }
8986
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+ }
9997 }
10098 }
10199
@@ -286,10 +284,29 @@ public static NamedProcedureCallDefinition createStoredProcedure(
286284 JpaAnnotations .NAMED_STORED_PROCEDURE_QUERY .createUsage ( modelsContext );
287285 nameStoredProcedureQueryAnn .name ( builder .getName () );
288286 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 ) );
289301
290- final StoredProcedureParameter [] parameters = new StoredProcedureParameter [ jdbcCall . parameters . size ()] ;
291- nameStoredProcedureQueryAnn . parameters ( parameters );
302+ return new NamedProcedureCallDefinitionImpl ( nameStoredProcedureQueryAnn ) ;
303+ }
292304
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 ()];
293310 for ( int i = 0 ; i < jdbcCall .parameters .size (); i ++ ) {
294311 final StoredProcedureParameterJpaAnnotation param =
295312 JpaAnnotations .STORED_PROCEDURE_PARAMETER .createUsage ( modelsContext );
@@ -300,40 +317,25 @@ public static NamedProcedureCallDefinition createStoredProcedure(
300317 param .mode ( ParameterMode .IN );
301318
302319 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 );
315324 param .type ( classDetails .toJavaClass () );
316325 }
326+ return parameters ;
327+ }
317328
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 <>();
329332 if ( builder .getQuerySpaces () != null ) {
330333 final QueryHintJpaAnnotation hint =
331334 JpaAnnotations .QUERY_HINT .createUsage ( modelsContext );
332335 hint .name ( HibernateHints .HINT_NATIVE_SPACES );
333336 hint .value ( String .join ( " " , builder .getQuerySpaces () ) );
334337 hints .add ( hint );
335338 }
336-
337339 if ( jdbcCall .resultParameter ) {
338340 // Mark native queries that have a result parameter as callable functions
339341 final QueryHintJpaAnnotation hint =
@@ -342,10 +344,15 @@ public static NamedProcedureCallDefinition createStoredProcedure(
342344 hint .value ( "true" );
343345 hints .add ( hint );
344346 }
347+ return hints ;
348+ }
345349
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 );
349356 }
350357
351358 public static void bindQuery (
0 commit comments