minor cleanups in QueryBinder

This commit is contained in:
Gavin 2022-12-28 11:28:51 +01:00 committed by Gavin King
parent 33fec62a56
commit 3d14d1b25f
3 changed files with 50 additions and 69 deletions

View File

@ -159,8 +159,8 @@ public final class AnnotationBinder {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<SqlResultSetMapping> mappings = ( List<SqlResultSetMapping> ) defaults.get( SqlResultSetMapping.class ); List<SqlResultSetMapping> mappings = ( List<SqlResultSetMapping> ) defaults.get( SqlResultSetMapping.class );
if ( mappings != null ) { if ( mappings != null ) {
for ( SqlResultSetMapping ann : mappings ) { for ( SqlResultSetMapping annotation : mappings ) {
QueryBinder.bindSqlResultSetMapping( ann, context, true ); QueryBinder.bindSqlResultSetMapping( annotation, context, true );
} }
} }
} }
@ -262,12 +262,11 @@ public final class AnnotationBinder {
false false
); );
final SqlResultSetMappings ann = annotatedElement.getAnnotation( SqlResultSetMappings.class ); QueryBinder.bindSqlResultSetMappings(
if ( ann != null ) { annotatedElement.getAnnotation( SqlResultSetMappings.class ),
for ( SqlResultSetMapping current : ann.value() ) { context,
QueryBinder.bindSqlResultSetMapping( current, context, false ); false
} );
}
QueryBinder.bindQuery( QueryBinder.bindQuery(
annotatedElement.getAnnotation( NamedQuery.class ), annotatedElement.getAnnotation( NamedQuery.class ),

View File

@ -223,14 +223,11 @@ public abstract class QueryBinder {
if ( !sqlString.startsWith( "{" ) || !sqlString.endsWith( "}" ) ) { if ( !sqlString.startsWith( "{" ) || !sqlString.endsWith( "}" ) ) {
throw exceptionProducer.get(); throw exceptionProducer.get();
} }
final JdbcCall jdbcCall = parseJdbcCall( final JdbcCall jdbcCall = parseJdbcCall( sqlString, exceptionProducer );
sqlString,
exceptionProducer
);
AnnotationDescriptor ann = new AnnotationDescriptor( NamedStoredProcedureQuery.class ); AnnotationDescriptor descriptor = new AnnotationDescriptor( NamedStoredProcedureQuery.class );
ann.setValue( "name", builder.getName() ); descriptor.setValue( "name", builder.getName() );
ann.setValue( "procedureName", jdbcCall.callableName ); descriptor.setValue( "procedureName", jdbcCall.callableName );
for ( String parameterName : jdbcCall.parameters ) { for ( String parameterName : jdbcCall.parameters ) {
AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor( StoredProcedureParameter.class ); AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor( StoredProcedureParameter.class );
@ -249,20 +246,20 @@ public abstract class QueryBinder {
} }
storedProcedureParameters.add( AnnotationFactory.create( parameterDescriptor ) ); storedProcedureParameters.add( AnnotationFactory.create( parameterDescriptor ) );
} }
ann.setValue( descriptor.setValue(
"parameters", "parameters",
storedProcedureParameters.toArray( new StoredProcedureParameter[storedProcedureParameters.size()] ) storedProcedureParameters.toArray( new StoredProcedureParameter[storedProcedureParameters.size()] )
); );
if ( builder.getResultSetMappingName() != null ) { if ( builder.getResultSetMappingName() != null ) {
ann.setValue( "resultSetMappings", new String[]{ builder.getResultSetMappingName() } ); descriptor.setValue( "resultSetMappings", new String[]{ builder.getResultSetMappingName() } );
} }
else { else {
ann.setValue( "resultSetMappings", new String[0] ); descriptor.setValue( "resultSetMappings", new String[0] );
} }
if ( builder.getResultSetMappingClassName() != null ) { if ( builder.getResultSetMappingClassName() != null ) {
ann.setValue( descriptor.setValue(
"resultClasses", "resultClasses",
new Class[] { new Class[] {
context.getBootstrapContext() context.getBootstrapContext()
@ -271,7 +268,7 @@ public abstract class QueryBinder {
); );
} }
else { else {
ann.setValue( "resultClasses", new Class[0] ); descriptor.setValue( "resultClasses", new Class[0] );
} }
if ( builder.getQuerySpaces() != null ) { if ( builder.getQuerySpaces() != null ) {
@ -289,9 +286,9 @@ public abstract class QueryBinder {
queryHints.add( AnnotationFactory.create( hintDescriptor2 ) ); queryHints.add( AnnotationFactory.create( hintDescriptor2 ) );
} }
ann.setValue( "hints", queryHints.toArray( new QueryHint[queryHints.size()] ) ); descriptor.setValue( "hints", queryHints.toArray( new QueryHint[queryHints.size()] ) );
return new NamedProcedureCallDefinitionImpl( AnnotationFactory.create( ann ) ); return new NamedProcedureCallDefinitionImpl( AnnotationFactory.create( descriptor ) );
} }
public static void bindQueries(NamedQueries namedQueries, MetadataBuildingContext context, boolean isDefault) { public static void bindQueries(NamedQueries namedQueries, MetadataBuildingContext context, boolean isDefault) {
@ -368,27 +365,20 @@ public abstract class QueryBinder {
} }
private static FlushMode getFlushMode(FlushModeType flushModeType) { private static FlushMode getFlushMode(FlushModeType flushModeType) {
FlushMode flushMode;
switch ( flushModeType ) { switch ( flushModeType ) {
case ALWAYS: case ALWAYS:
flushMode = FlushMode.ALWAYS; return FlushMode.ALWAYS;
break;
case AUTO: case AUTO:
flushMode = FlushMode.AUTO; return FlushMode.AUTO;
break;
case COMMIT: case COMMIT:
flushMode = FlushMode.COMMIT; return FlushMode.COMMIT;
break;
case MANUAL: case MANUAL:
flushMode = FlushMode.MANUAL; return FlushMode.MANUAL;
break;
case PERSISTENCE_CONTEXT: case PERSISTENCE_CONTEXT:
flushMode = null; return null;
break;
default: default:
throw new AssertionFailure( "Unknown flushModeType: " + flushModeType ); throw new AssertionFailure( "Unknown FlushModeType: " + flushModeType );
} }
return flushMode;
} }
private static CacheMode getCacheMode(CacheModeType cacheModeType) { private static CacheMode getCacheMode(CacheModeType cacheModeType) {
@ -410,60 +400,52 @@ public abstract class QueryBinder {
public static void bindQueries( public static void bindQueries(
org.hibernate.annotations.NamedQueries queriesAnn, org.hibernate.annotations.NamedQueries namedQueries,
MetadataBuildingContext context) { MetadataBuildingContext context) {
if ( queriesAnn == null ) { if ( namedQueries != null ) {
return; for (org.hibernate.annotations.NamedQuery namedQuery : namedQueries.value()) {
} bindQuery( namedQuery, context );
}
for (org.hibernate.annotations.NamedQuery q : queriesAnn.value()) {
bindQuery( q, context );
} }
} }
public static void bindNamedStoredProcedureQuery( public static void bindNamedStoredProcedureQuery(
NamedStoredProcedureQuery annotation, NamedStoredProcedureQuery namedStoredProcedureQuery,
MetadataBuildingContext context, MetadataBuildingContext context,
boolean isDefault) { boolean isDefault) {
if ( annotation == null ) { if ( namedStoredProcedureQuery != null ) {
return; if ( namedStoredProcedureQuery.name().isEmpty() ) {
} throw new AnnotationException( "Class or package level '@NamedStoredProcedureQuery' annotation must specify a 'name'" );
}
if ( annotation.name().isEmpty() ) { final NamedProcedureCallDefinitionImpl definition = new NamedProcedureCallDefinitionImpl( namedStoredProcedureQuery );
throw new AnnotationException( "Class or package level '@NamedStoredProcedureQuery' annotation must specify a 'name'" ); if ( isDefault ) {
context.getMetadataCollector().addDefaultNamedProcedureCall( definition );
}
else {
context.getMetadataCollector().addNamedProcedureCallDefinition( definition );
}
LOG.debugf( "Bound named stored procedure query : %s => %s", definition.getRegistrationName(), definition.getProcedureName() );
} }
final NamedProcedureCallDefinitionImpl def = new NamedProcedureCallDefinitionImpl( annotation );
if ( isDefault ) {
context.getMetadataCollector().addDefaultNamedProcedureCall( def );
}
else {
context.getMetadataCollector().addNamedProcedureCallDefinition( def );
}
LOG.debugf( "Bound named stored procedure query : %s => %s", def.getRegistrationName(), def.getProcedureName() );
} }
public static void bindSqlResultSetMappings( public static void bindSqlResultSetMappings(
SqlResultSetMappings ann, SqlResultSetMappings resultSetMappings,
MetadataBuildingContext context, MetadataBuildingContext context,
boolean isDefault) { boolean isDefault) {
if ( ann == null ) { if ( resultSetMappings != null ) {
return; for ( SqlResultSetMapping resultSetMapping : resultSetMappings.value() ) {
} bindSqlResultSetMapping( resultSetMapping, context, isDefault );
}
for (SqlResultSetMapping rs : ann.value()) {
//no need to handle inSecondPass
context.getMetadataCollector().addSecondPass( new ResultSetMappingSecondPass( rs, context, true ) );
} }
} }
public static void bindSqlResultSetMapping( public static void bindSqlResultSetMapping(
SqlResultSetMapping ann, SqlResultSetMapping resultSetMapping,
MetadataBuildingContext context, MetadataBuildingContext context,
boolean isDefault) { boolean isDefault) {
//no need to handle inSecondPass //no need to handle inSecondPass
context.getMetadataCollector().addSecondPass( new ResultSetMappingSecondPass( ann, context, isDefault ) ); context.getMetadataCollector().addSecondPass( new ResultSetMappingSecondPass( resultSetMapping, context, isDefault ) );
} }
private static class JdbcCall { private static class JdbcCall {

View File

@ -154,7 +154,7 @@ public class QueryHintDefinition {
final Integer lockTimeoutHint = specLockTimeout(); final Integer lockTimeoutHint = specLockTimeout();
final Boolean followOnLocking = getBooleanWrapper( HibernateHints.HINT_FOLLOW_ON_LOCKING ); final Boolean followOnLocking = getBooleanWrapper( HibernateHints.HINT_FOLLOW_ON_LOCKING );
return determineLockOptions(lockModeType, lockTimeoutHint, followOnLocking); return determineLockOptions( lockModeType, lockTimeoutHint, followOnLocking );
} }
private Integer specLockTimeout() { private Integer specLockTimeout() {