remove hibernate.query.validate_parameters

in H6 we're going to always validate them
This commit is contained in:
Gavin King 2022-01-20 22:07:22 +01:00
parent a59bf70ccf
commit d385cc8825
20 changed files with 17 additions and 215 deletions

View File

@ -492,10 +492,6 @@ Can reference a
`StatementInspector` implementation `Class` reference or
`StatementInspector` implementation class name (fully-qualified class name).
`*hibernate.query.validate_parameters*` (e.g. `true` (default value) or `false`)::
This configuration property can be used to disable parameters validation performed by `org.hibernate.query.Query#setParameter` when the Session is bootstrapped via Jakarta Persistence
`jakarta.persistence.EntityManagerFactory`.
`*hibernate.criteria.value_handling_mode*` (e.g. `BIND` (default value) or `INLINE`)::
By default, Criteria queries uses bind parameters for any value passed through the Jakarta Persistence Criteria API.
+

View File

@ -135,22 +135,6 @@ public interface SessionBuilder<T extends SessionBuilder> {
T jdbcTimeZone(TimeZone timeZone);
/**
* Should {@link Query#setParameter} perform parameter validation
* when the Session is bootstrapped via JPA {@link jakarta.persistence.EntityManagerFactory}
*
* @param enabled {@code true} indicates the validation should be performed, {@code false} otherwise
* <p>
* The default value is {@code true}
*
* @return {@code this}, for method chaining
*/
default T setQueryParameterValidation(boolean enabled) {
return (T) this;
}
/**
* Should the session be automatically closed after transaction completion?
*

View File

@ -40,18 +40,4 @@ public interface StatelessSessionBuilder<T extends StatelessSessionBuilder> {
* @return {@code this}, for method chaining
*/
T tenantIdentifier(String tenantIdentifier);
/**
* Should {@link Query#setParameter} perform parameter validation
* when the Session is bootstrapped via JPA {@link jakarta.persistence.EntityManagerFactory}
*
* @param enabled {@code true} indicates the validation should be performed, {@code false} otherwise
* <p>
* The default value is {@code true}
*
* @return {@code this}, for method chaining
*/
default T setQueryParameterValidation(boolean enabled) {
return (T) this;
}
}

View File

@ -132,7 +132,6 @@ import static org.hibernate.cfg.AvailableSettings.USE_SCROLLABLE_RESULTSET;
import static org.hibernate.cfg.AvailableSettings.USE_SECOND_LEVEL_CACHE;
import static org.hibernate.cfg.AvailableSettings.USE_SQL_COMMENTS;
import static org.hibernate.cfg.AvailableSettings.USE_STRUCTURED_CACHE;
import static org.hibernate.cfg.AvailableSettings.VALIDATE_QUERY_PARAMETERS;
import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN;
import static org.hibernate.internal.CoreLogging.messageLogger;
import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
@ -246,7 +245,6 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private PhysicalConnectionHandlingMode connectionHandlingMode;
private boolean connectionProviderDisablesAutoCommit;
private TimeZone jdbcTimeZone;
private boolean queryParametersValidationEnabled;
private ValueHandlingMode criteriaValueHandlingMode;
private ImmutableEntityUpdateQueryHandlingMode immutableEntityUpdateQueryHandlingMode;
// These two settings cannot be modified from the builder,
@ -558,12 +556,6 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
throw new IllegalArgumentException( "Configuration property " + JDBC_TIME_ZONE + " value [" + jdbcTimeZoneValue + "] is not supported!" );
}
this.queryParametersValidationEnabled = ConfigurationHelper.getBoolean(
VALIDATE_QUERY_PARAMETERS,
configurationSettings,
true
);
this.criteriaValueHandlingMode = ValueHandlingMode.interpret(
configurationSettings.get( CRITERIA_VALUE_HANDLING_MODE )
);
@ -1168,11 +1160,6 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
return this.jdbcTimeZone;
}
@Override
public boolean isQueryParametersValidationEnabled() {
return this.queryParametersValidationEnabled;
}
@Override
public ValueHandlingMode getCriteriaValueHandlingMode() {
return criteriaValueHandlingMode;

View File

@ -385,11 +385,6 @@ public class AbstractDelegatingSessionFactoryOptions implements SessionFactoryOp
return delegate.getJdbcTimeZone();
}
@Override
public boolean isQueryParametersValidationEnabled() {
return delegate.isQueryParametersValidationEnabled();
}
@Override
public ValueHandlingMode getCriteriaValueHandlingMode() {
return delegate.getCriteriaValueHandlingMode();

View File

@ -252,10 +252,6 @@ public interface SessionFactoryOptions extends QueryEngineOptions {
TimeZone getJdbcTimeZone();
default boolean isQueryParametersValidationEnabled(){
return isJpaBootstrap();
}
default ValueHandlingMode getCriteriaValueHandlingMode() {
return ValueHandlingMode.BIND;
}

View File

@ -1968,20 +1968,6 @@ public interface AvailableSettings {
*/
String MERGE_ENTITY_COPY_OBSERVER = "hibernate.event.merge.entity_copy_observer";
/**
* Setting which indicates if {@link Query#setParameter} should not perform parameters validation
*
* This setting is applied only when the Session is bootstrapped via JPA {@link jakarta.persistence.EntityManagerFactory}
*
* </p>
* Values are: {@code true} indicates the validation should be performed, {@code false} otherwise
* <p>
* The default value is {@code true} when the Session is bootstrapped via JPA {@link jakarta.persistence.EntityManagerFactory},
* otherwise is {@code false}
*
*/
String VALIDATE_QUERY_PARAMETERS = "hibernate.query.validate_parameters";
/**
* By default, Criteria queries uses bind parameters for any value passed through the JPA Criteria API.
*

View File

@ -121,12 +121,6 @@ public abstract class AbstractDelegatingSessionBuilder<T extends SessionBuilder>
return getThis();
}
@Override
public T setQueryParameterValidation(boolean enabled) {
delegate.setQueryParameterValidation( enabled );
return getThis();
}
@Override
public T connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
delegate.connectionHandlingMode( mode );

View File

@ -327,11 +327,6 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
return delegate.isAutoCloseSessionEnabled();
}
@Override
public boolean isQueryParametersValidationEnabled() {
return delegate.isQueryParametersValidationEnabled();
}
@Override
public boolean shouldAutoJoinTransaction() {
return delegate.shouldAutoJoinTransaction();

View File

@ -396,10 +396,6 @@ public interface SharedSessionContractImplementor
boolean isAutoCloseSessionEnabled();
default boolean isQueryParametersValidationEnabled(){
return getFactory().getSessionFactoryOptions().isQueryParametersValidationEnabled();
}
/**
* Get the load query influencers associated with this session.
*

View File

@ -72,6 +72,4 @@ public interface SessionCreationOptions {
AfterCompletionAction getAfterCompletionAction();
ManagedFlushChecker getManagedFlushChecker();
boolean isQueryParametersValidationEnabled();
}

View File

@ -1170,7 +1170,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
private boolean autoClear;
private String tenantIdentifier;
private TimeZone jdbcTimeZone;
private boolean queryParametersValidationEnabled;
private boolean explicitNoInterceptor;
// Lazy: defaults can be built by invoking the builder in fastSessionServices.defaultSessionEventListeners
@ -1195,7 +1194,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
tenantIdentifier = currentTenantIdentifierResolver.resolveCurrentTenantIdentifier();
}
this.jdbcTimeZone = sessionFactoryOptions.getJdbcTimeZone();
this.queryParametersValidationEnabled = sessionFactoryOptions.isQueryParametersValidationEnabled();
}
@ -1228,11 +1226,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
: null;
}
@Override
public boolean isQueryParametersValidationEnabled() {
return this.queryParametersValidationEnabled;
}
@Override
public boolean shouldAutoJoinTransactions() {
return autoJoinTransactions;
@ -1419,19 +1412,12 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
jdbcTimeZone = timeZone;
return (T) this;
}
@Override
public T setQueryParameterValidation(boolean enabled) {
queryParametersValidationEnabled = enabled;
return (T) this;
}
}
public static class StatelessSessionBuilderImpl implements StatelessSessionBuilder, SessionCreationOptions {
private final SessionFactoryImpl sessionFactory;
private Connection connection;
private String tenantIdentifier;
private boolean queryParametersValidationEnabled;
public StatelessSessionBuilderImpl(SessionFactoryImpl sessionFactory) {
this.sessionFactory = sessionFactory;
@ -1440,7 +1426,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
if ( tenantIdentifierResolver != null ) {
tenantIdentifier = tenantIdentifierResolver.resolveCurrentTenantIdentifier();
}
queryParametersValidationEnabled = sessionFactory.getSessionFactoryOptions().isQueryParametersValidationEnabled();
}
@Override
@ -1535,17 +1520,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
public ManagedFlushChecker getManagedFlushChecker() {
return null;
}
@Override
public boolean isQueryParametersValidationEnabled() {
return queryParametersValidationEnabled;
}
@Override
public StatelessSessionBuilder setQueryParameterValidation(boolean enabled) {
queryParametersValidationEnabled = enabled;
return this;
}
}
@Override

View File

@ -183,7 +183,6 @@ public class SessionImpl
private boolean autoClear;
private boolean autoClose;
private boolean queryParametersValidationEnabled;
private transient int dontFlushFromFind;
@ -201,7 +200,6 @@ public class SessionImpl
this.autoClear = options.shouldAutoClear();
this.autoClose = options.shouldAutoClose();
this.queryParametersValidationEnabled = options.isQueryParametersValidationEnabled();
if ( options instanceof SharedSessionCreationOptions ) {
final SharedSessionCreationOptions sharedOptions = (SharedSessionCreationOptions) options;
@ -431,11 +429,6 @@ public class SessionImpl
return autoClose;
}
@Override
public boolean isQueryParametersValidationEnabled() {
return queryParametersValidationEnabled;
}
@Override
public boolean isOpen() {
checkSessionFactoryOpen();
@ -2067,11 +2060,6 @@ public class SessionImpl
session.getActionQueue().getTransactionCompletionProcesses() :
null;
}
@Override
public boolean isQueryParametersValidationEnabled() {
return session.isQueryParametersValidationEnabled();
}
}
private class LockRequestImpl implements LockRequest {

View File

@ -23,6 +23,6 @@ public class ProcedureParameterBindingImpl<T>
public ProcedureParameterBindingImpl(
ProcedureParameterImplementor<T> queryParameter,
SessionFactoryImplementor sessionFactory) {
super( queryParameter, sessionFactory, true );
super( queryParameter, sessionFactory );
}
}

View File

@ -33,7 +33,6 @@ import jakarta.persistence.TemporalType;
public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, JavaType.CoercionContext {
private final QueryParameter<T> queryParameter;
private final SessionFactoryImplementor sessionFactory;
private final boolean isBindingValidationRequired;
private boolean isBound;
private boolean isMultiValued;
@ -50,11 +49,9 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
*/
protected QueryParameterBindingImpl(
QueryParameter<T> queryParameter,
SessionFactoryImplementor sessionFactory,
boolean isBindingValidationRequired) {
SessionFactoryImplementor sessionFactory) {
this.queryParameter = queryParameter;
this.sessionFactory = sessionFactory;
this.isBindingValidationRequired = isBindingValidationRequired;
this.bindType = queryParameter.getHibernateType();
}
@ -64,11 +61,9 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
public QueryParameterBindingImpl(
QueryParameter<T> queryParameter,
SessionFactoryImplementor sessionFactory,
BindableType<T> bindType,
boolean isBindingValidationRequired) {
BindableType<T> bindType) {
this.queryParameter = queryParameter;
this.sessionFactory = sessionFactory;
this.isBindingValidationRequired = isBindingValidationRequired;
this.bindType = bindType;
}
@ -132,9 +127,7 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
}
}
if ( isBindingValidationRequired ) {
validate( value );
}
validate( value );
if ( resolveJdbcTypeIfNecessary && bindType == null && value == null ) {
bindType = getTypeConfiguration().getBasicTypeRegistry().getRegisteredType( "null" );
@ -203,9 +196,7 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
value = coerce( value, queryParameter.getHibernateType() );
}
if ( isBindingValidationRequired ) {
validate( value, clarifiedType );
}
validate( value, clarifiedType );
bindValue( value );
}
@ -242,9 +233,7 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
}
}
if ( isBindingValidationRequired ) {
validate( value, temporalTypePrecision );
}
validate( value, temporalTypePrecision );
bindValue( value );
setExplicitTemporalPrecision( temporalTypePrecision );

View File

@ -41,57 +41,34 @@ import org.hibernate.type.spi.TypeConfiguration;
public class QueryParameterBindingsImpl implements QueryParameterBindings {
private final SessionFactoryImplementor sessionFactory;
private final ParameterMetadataImplementor parameterMetadata;
private final boolean queryParametersValidationEnabled;
private Map<QueryParameter<?>, QueryParameterBinding<?>> parameterBindingMap;
/**
* Constructs a QueryParameterBindings based on the passed information
*
* @apiNote Calls {@link #from(ParameterMetadataImplementor,SessionFactoryImplementor,boolean)}
* using {@link org.hibernate.boot.spi.SessionFactoryOptions#isQueryParametersValidationEnabled}
* as `queryParametersValidationEnabled`
*/
public static QueryParameterBindingsImpl from(
ParameterMetadataImplementor parameterMetadata,
SessionFactoryImplementor sessionFactory) {
return from(
parameterMetadata,
sessionFactory,
sessionFactory.getSessionFactoryOptions().isQueryParametersValidationEnabled()
);
}
/**
* Constructs a QueryParameterBindings based on the passed information
*/
public static QueryParameterBindingsImpl from(
ParameterMetadataImplementor parameterMetadata,
SessionFactoryImplementor sessionFactory,
boolean queryParametersValidationEnabled) {
if ( parameterMetadata == null ) {
throw new QueryParameterException( "Query parameter metadata cannot be null" );
}
return new QueryParameterBindingsImpl(
sessionFactory,
parameterMetadata,
queryParametersValidationEnabled
parameterMetadata
);
}
private QueryParameterBindingsImpl(
SessionFactoryImplementor sessionFactory,
ParameterMetadataImplementor parameterMetadata,
boolean queryParametersValidationEnabled) {
ParameterMetadataImplementor parameterMetadata) {
this.sessionFactory = sessionFactory;
this.parameterMetadata = parameterMetadata;
this.queryParametersValidationEnabled = queryParametersValidationEnabled;
this.parameterBindingMap = new ConcurrentHashMap<>( parameterMetadata.getParameterCount() );
}
@SuppressWarnings({"WeakerAccess" })
protected <T> QueryParameterBinding<T> makeBinding(QueryParameterImplementor<T> queryParameter) {
if ( parameterBindingMap == null ) {
parameterBindingMap = new IdentityHashMap<>();
@ -109,8 +86,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
final QueryParameterBinding<T> binding = new QueryParameterBindingImpl<>(
queryParameter,
sessionFactory,
parameterMetadata.getInferredParameterType( queryParameter ),
queryParametersValidationEnabled
parameterMetadata.getInferredParameterType( queryParameter )
);
parameterBindingMap.put( queryParameter, binding );

View File

@ -190,11 +190,7 @@ public class NativeQueryImpl<R>
this.sqlString = parameterInterpretation.getAdjustedSqlString();
this.parameterMetadata = parameterInterpretation.toParameterMetadata( session );
this.parameterOccurrences = parameterInterpretation.getOrderedParameterOccurrences();
this.parameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
session.getFactory(),
session.isQueryParametersValidationEnabled()
);
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, session.getFactory() );
this.querySpaces = new HashSet<>();
this.resultSetMapping = resultSetMappingCreator.get();
@ -327,11 +323,7 @@ public class NativeQueryImpl<R>
this.sqlString = parameterInterpretation.getAdjustedSqlString();
this.parameterMetadata = parameterInterpretation.toParameterMetadata( session );
this.parameterOccurrences = parameterInterpretation.getOrderedParameterOccurrences();
this.parameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
session.getFactory(),
session.isQueryParametersValidationEnabled()
);
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, session.getFactory() );
this.querySpaces = new HashSet<>();
this.resultSetMapping = new ResultSetMappingImpl( resultSetMappingMemento.getName() );
@ -386,11 +378,7 @@ public class NativeQueryImpl<R>
this.sqlString = parameterInterpretation.getAdjustedSqlString();
this.parameterMetadata = parameterInterpretation.toParameterMetadata( session );
this.parameterOccurrences = parameterInterpretation.getOrderedParameterOccurrences();
this.parameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
session.getFactory(),
session.isQueryParametersValidationEnabled()
);
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, session.getFactory() );
this.resultSetMapping = new ResultSetMappingImpl( sqlString, true );
this.resultMappingSuppliedToCtor = false;

View File

@ -159,11 +159,7 @@ public class QuerySqmImpl<R>
this.domainParameterXref = hqlInterpretation.getDomainParameterXref();
this.parameterMetadata = hqlInterpretation.getParameterMetadata();
this.parameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
producer.getFactory(),
producer.isQueryParametersValidationEnabled()
);
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, producer.getFactory() );
applyOptions( memento );
}
@ -224,11 +220,7 @@ public class QuerySqmImpl<R>
this.parameterMetadata = hqlInterpretation.getParameterMetadata();
this.domainParameterXref = hqlInterpretation.getDomainParameterXref();
this.parameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
producer.getFactory(),
producer.isQueryParametersValidationEnabled()
);
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, producer.getFactory() );
}
/**
@ -275,11 +267,8 @@ public class QuerySqmImpl<R>
this.parameterMetadata = new ParameterMetadataImpl( domainParameterXref.getQueryParameters() );
}
this.parameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
producer.getFactory(),
producer.isQueryParametersValidationEnabled()
);
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, producer.getFactory() );
// Parameters might be created through HibernateCriteriaBuilder.value which we need to bind here
for ( SqmParameter<?> sqmParameter : this.domainParameterXref.getParameterResolutions().getSqmParameters() ) {
if ( sqmParameter instanceof SqmJpaCriteriaParameterWrapper<?> ) {

View File

@ -59,20 +59,6 @@ public class QueryParametersValidationTest extends BaseEntityManagerFunctionalTe
}
}
@Test
@TestForIssue(jiraKey = "HHH-11579")
public void setParameterWithWrongTypeShouldNotThrowIllegalArgumentExceptionWhenValidationIsDisabled() {
final SessionFactory sessionFactory = entityManagerFactory().unwrap( SessionFactory.class );
final Session session = sessionFactory.withOptions().setQueryParameterValidation( false ).openSession();
try {
session.createQuery( "select e from TestEntity e where e.id = :id" ).setParameter( "id", 1 );
}
finally {
session.close();
sessionFactory.close();
}
}
@Test
public void setParameterWithCorrectTypeShouldNotThrowIllegalArgumentException() {
final EntityManager entityManager = entityManagerFactory().createEntityManager();

View File

@ -27,8 +27,7 @@ import org.junit.jupiter.api.Test;
@Jpa(
annotatedClasses = {
QueryParametersWithDisabledValidationTest.TestEntity.class
},
integrationSettings = { @Setting(name = AvailableSettings.VALIDATE_QUERY_PARAMETERS, value = "false") }
}
)
public class QueryParametersWithDisabledValidationTest {