Skip to content

Commit b53728f

Browse files
committed
clean up QueryBinder a bit
Signed-off-by: Gavin King <[email protected]>
1 parent b1fcfdc commit b53728f

File tree

1 file changed

+117
-92
lines changed

1 file changed

+117
-92
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/QueryBinder.java

Lines changed: 117 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.AnnotationException;
1717
import org.hibernate.CacheMode;
1818
import org.hibernate.FlushMode;
19+
import org.hibernate.LockOptions;
1920
import org.hibernate.annotations.FlushModeType;
2021
import org.hibernate.annotations.HQLSelect;
2122
import org.hibernate.annotations.SQLSelect;
@@ -33,7 +34,6 @@
3334
import org.hibernate.boot.spi.MetadataBuildingContext;
3435
import org.hibernate.internal.CoreMessageLogger;
3536
import org.hibernate.internal.log.DeprecationLogger;
36-
import org.hibernate.internal.util.collections.ArrayHelper;
3737
import org.hibernate.jpa.HibernateHints;
3838
import org.hibernate.models.internal.util.StringHelper;
3939
import org.hibernate.models.spi.ClassDetails;
@@ -56,6 +56,7 @@
5656

5757
import static java.lang.Boolean.TRUE;
5858
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
59+
import static org.hibernate.internal.util.collections.ArrayHelper.isEmpty;
5960
import static org.hibernate.internal.util.collections.CollectionHelper.determineProperSizing;
6061
import static org.hibernate.internal.util.collections.CollectionHelper.setOf;
6162

@@ -80,6 +81,7 @@ public static void bindQuery(
8081

8182
final String queryName = namedQuery.name();
8283
final String queryString = namedQuery.query();
84+
final Class<?> resultClass = namedQuery.resultClass();
8385

8486
if ( queryName.isEmpty() ) {
8587
throw new AnnotationException( "Class or package level '@NamedQuery' annotation must specify a 'name'" );
@@ -90,21 +92,9 @@ public static void bindQuery(
9092
}
9193

9294
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 );
10898
if ( isDefault ) {
10999
context.getMetadataCollector().addDefaultQuery( queryMapping );
110100
}
@@ -113,13 +103,22 @@ public static void bindQuery(
113103
}
114104
}
115105

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();
123122
}
124123

125124
public static void bindNativeQuery(
@@ -141,29 +140,14 @@ public static void bindNativeQuery(
141140

142141
final String resultSetMappingName = namedNativeQuery.resultSetMapping();
143142
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 );
164147

165148
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() );
167151
}
168152

169153
if ( isDefault ) {
@@ -174,6 +158,27 @@ public static void bindNativeQuery(
174158
}
175159
}
176160

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+
177182
public static void bindNativeQuery(
178183
String name,
179184
SQLSelect sqlSelect,
@@ -192,10 +197,11 @@ public static void bindNativeQuery(
192197
}
193198

194199
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 ) );
199205
builder.setResultSetMappingName( name );
200206
}
201207

@@ -211,37 +217,21 @@ public static void bindNativeQuery(
211217

212218
final String registrationName = namedNativeQuery.name();
213219

214-
//ResultSetMappingDefinition mappingDefinition = mappings.getJdbcValuesMappingProducer( queryAnn.resultSetMapping() );
215220
if ( registrationName.isEmpty() ) {
216221
throw new AnnotationException( "Class or package level '@NamedNativeQuery' annotation must specify a 'name'" );
217222
}
218223

219224
final String resultSetMappingName = namedNativeQuery.resultSetMapping();
220225
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;
227227

228228
final String[] querySpacesList = namedNativeQuery.querySpaces();
229229
final HashSet<String> querySpaces = new HashSet<>( determineProperSizing( querySpacesList.length ) );
230230
Collections.addAll( querySpaces, querySpacesList );
231231

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 );
245235

246236
if ( TRUE == namedNativeQuery.callable() ) {
247237
final NamedProcedureCallDefinition definition =
@@ -266,6 +256,27 @@ public static void bindNativeQuery(
266256
}
267257
}
268258

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+
269280
/**
270281
* Handles legacy cases where a named native query was used to specify a procedure call
271282
*
@@ -284,15 +295,17 @@ public static NamedProcedureCallDefinition createStoredProcedure(
284295

285296
final SourceModelBuildingContext sourceModelBuildingContext = context.getMetadataCollector()
286297
.getSourceModelBuildingContext();
287-
final NamedStoredProcedureQueryJpaAnnotation nameStoredProcedureQueryAnn = JpaAnnotations.NAMED_STORED_PROCEDURE_QUERY.createUsage( sourceModelBuildingContext );
298+
final NamedStoredProcedureQueryJpaAnnotation nameStoredProcedureQueryAnn =
299+
JpaAnnotations.NAMED_STORED_PROCEDURE_QUERY.createUsage( sourceModelBuildingContext );
288300
nameStoredProcedureQueryAnn.name( builder.getName() );
289301
nameStoredProcedureQueryAnn.procedureName( jdbcCall.callableName );
290302

291303
final StoredProcedureParameter[] parameters = new StoredProcedureParameter[jdbcCall.parameters.size()];
292304
nameStoredProcedureQueryAnn.parameters( parameters );
293305

294306
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 );
296309
parameters[i] = param;
297310

298311
final String paramName = jdbcCall.parameters.get( i );
@@ -309,7 +322,8 @@ public static NamedProcedureCallDefinition createStoredProcedure(
309322
.getTypeConfiguration()
310323
.getBasicTypeRegistry()
311324
.getRegisteredType( typeName );
312-
classDetails = context.getMetadataCollector().getClassDetailsRegistry().getClassDetails( registeredType.getJavaType().getName() );
325+
classDetails = context.getMetadataCollector().getClassDetailsRegistry()
326+
.getClassDetails( registeredType.getJavaType().getName() );
313327
}
314328
param.type( classDetails.toJavaClass() );
315329
}
@@ -326,15 +340,17 @@ public static NamedProcedureCallDefinition createStoredProcedure(
326340
final List<QueryHintJpaAnnotation> hints = new ArrayList<>();
327341

328342
if ( builder.getQuerySpaces() != null ) {
329-
final QueryHintJpaAnnotation hint = JpaAnnotations.QUERY_HINT.createUsage( sourceModelBuildingContext );
343+
final QueryHintJpaAnnotation hint =
344+
JpaAnnotations.QUERY_HINT.createUsage( sourceModelBuildingContext );
330345
hint.name( HibernateHints.HINT_NATIVE_SPACES );
331346
hint.value( String.join( " ", builder.getQuerySpaces() ) );
332347
hints.add( hint );
333348
}
334349

335350
if ( jdbcCall.resultParameter ) {
336351
// 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 );
338354
hint.name( HibernateHints.HINT_CALLABLE_FUNCTION );
339355
hint.value( "true" );
340356
hints.add( hint );
@@ -365,36 +381,42 @@ public static void bindQuery(
365381
}
366382

367383
final String registrationName = namedQuery.name();
384+
final Class<?> resultClass = namedQuery.resultClass();
368385

369-
//ResultSetMappingDefinition mappingDefinition = mappings.getJdbcValuesMappingProducer( namedQuery.resultSetMapping() );
370386
if ( registrationName.isEmpty() ) {
371387
throw new AnnotationException( "Class or package level '@NamedQuery' annotation must specify a 'name'" );
372388
}
373389

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() ) ;
388393

389394
final NamedHqlQueryDefinitionImpl<?> hqlQueryDefinition = builder.build();
390395

391396
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() );
393399
}
394400

395401
context.getMetadataCollector().addNamedQuery( hqlQueryDefinition );
396402
}
397403

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+
398420
private static CacheMode getCacheMode(CacheRetrieveMode cacheRetrieveMode, CacheStoreMode cacheStoreMode) {
399421
final CacheMode cacheMode = CacheMode.fromJpaModes( cacheRetrieveMode, cacheStoreMode );
400422
return cacheMode == null ? CacheMode.NORMAL : cacheMode;
@@ -419,14 +441,16 @@ public static void bindNamedStoredProcedureQuery(
419441
throw new AnnotationException( "Class or package level '@NamedStoredProcedureQuery' annotation must specify a 'name'" );
420442
}
421443

422-
final NamedProcedureCallDefinitionImpl definition = new NamedProcedureCallDefinitionImpl( namedStoredProcedureQuery );
444+
final NamedProcedureCallDefinitionImpl definition =
445+
new NamedProcedureCallDefinitionImpl( namedStoredProcedureQuery );
423446
if ( isDefault ) {
424447
context.getMetadataCollector().addDefaultNamedProcedureCall( definition );
425448
}
426449
else {
427450
context.getMetadataCollector().addNamedProcedureCallDefinition( definition );
428451
}
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() );
430454
}
431455
}
432456

@@ -435,7 +459,8 @@ public static void bindSqlResultSetMapping(
435459
MetadataBuildingContext context,
436460
boolean isDefault) {
437461
//no need to handle inSecondPass
438-
context.getMetadataCollector().addSecondPass( new ResultSetMappingSecondPass( resultSetMappingAnn, context, isDefault ) );
462+
context.getMetadataCollector()
463+
.addSecondPass( new ResultSetMappingSecondPass( resultSetMappingAnn, context, isDefault ) );
439464
}
440465

441466
private static class JdbcCall {

0 commit comments

Comments
 (0)