HHH-9294 Reduce throw of NumberFormatException in BaseQueryImpl

This commit is contained in:
barreiro 2014-07-17 17:27:32 +01:00 committed by Sanne Grinovero
parent 05cd4ff5d3
commit 56d0d480e4
1 changed files with 14 additions and 6 deletions

View File

@ -485,27 +485,35 @@ public abstract class BaseQueryImpl implements Query {
@SuppressWarnings("unchecked")
protected <X> ParameterRegistration<X> findParameterRegistration(String parameterName) {
final Integer jpaPositionalParameter = toNumberOrNull( parameterName );
if ( parameterRegistrations != null ) {
for ( ParameterRegistration<?> param : parameterRegistrations ) {
if ( parameterName.equals( param.getName() ) ) {
return (ParameterRegistration<X>) param;
}
}
// legacy allowance of the application to access the parameter using the position as a String
if ( param.isJpaPositionalParameter() && jpaPositionalParameter != null ) {
if ( jpaPositionalParameter.equals( param.getPosition() ) ) {
final Integer jpaPositionalParameter = toNumberOrNull( parameterName );
if ( jpaPositionalParameter != null ) {
for ( ParameterRegistration<?> param : parameterRegistrations ) {
if ( param.isJpaPositionalParameter() && jpaPositionalParameter.equals( param.getPosition() ) ) {
LOG.deprecatedJpaPositionalParameterAccess( jpaPositionalParameter );
}
return (ParameterRegistration<X>) param;
}
}
}
}
throw new IllegalArgumentException( "Parameter with that name [" + parameterName + "] did not exist" );
}
private Integer toNumberOrNull(String parameterName) {
// HHH-9294 Quick check if string is made of digits to avoid the overhead of throwing an exception
for(int i = 0; i < parameterName.length(); i++) {
if ( !Character.isDigit( parameterName.charAt( i ) ) ) {
return null;
}
}
try {
return Integer.valueOf( parameterName );
}