HHH-14617 remove QueryLiteralRendering and LiteralHandlingMode

This commit is contained in:
nathan.xu 2021-05-18 19:57:03 -04:00 committed by Christian Beikov
parent b51fb9fe03
commit 2e875f9b8a
15 changed files with 13 additions and 243 deletions

View File

@ -501,19 +501,6 @@ Can reference a
This configuration property can be used to disable parameters validation performed by `org.hibernate.query.Query#setParameter` when the Session is bootstrapped via JPA This configuration property can be used to disable parameters validation performed by `org.hibernate.query.Query#setParameter` when the Session is bootstrapped via JPA
`javax.persistence.EntityManagerFactory`. `javax.persistence.EntityManagerFactory`.
`*hibernate.criteria.literal_handling_mode*` (e.g. `AUTO` (default value), `BIND` or `INLINE`)::
By default, Criteria queries use bind parameters for any literal that is not a numeric value.
However, to increase the likelihood of JDBC statement caching, you might want to use bind parameters for numeric values too.
+
The `org.hibernate.query.criteria.LiteralHandlingMode#BIND` mode will use bind variables for any literal value.
The `org.hibernate.query.criteria.LiteralHandlingMode#INLINE` mode will inline literal values as is.
+
To prevent SQL injection, never use `org.hibernate.query.criteria.LiteralHandlingMode#INLINE` with String variables.
Always use constants with the `org.hibernate.query.criteria.LiteralHandlingMode#INLINE` mode.
+
Valid options are defined by the `org.hibernate.query.criteria.LiteralHandlingMode` enum.
The default value is `org.hibernate.query.criteria.LiteralHandlingMode#AUTO`.
`*hibernate.criteria.value_handling_mode*` (e.g. `BIND` (default value) or `INLINE`):: `*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 JPA Criteria API. By default, Criteria queries uses bind parameters for any value passed through the JPA Criteria API.
+ +

View File

@ -56,7 +56,6 @@ import org.hibernate.jpa.spi.MutableJpaCompliance;
import org.hibernate.loader.BatchFetchStyle; import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode; import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor; import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
@ -85,7 +84,6 @@ import static org.hibernate.cfg.AvailableSettings.CALLABLE_NAMED_PARAMS_ENABLED;
import static org.hibernate.cfg.AvailableSettings.CHECK_NULLABILITY; import static org.hibernate.cfg.AvailableSettings.CHECK_NULLABILITY;
import static org.hibernate.cfg.AvailableSettings.CONNECTION_HANDLING; import static org.hibernate.cfg.AvailableSettings.CONNECTION_HANDLING;
import static org.hibernate.cfg.AvailableSettings.CONVENTIONAL_JAVA_CONSTANTS; import static org.hibernate.cfg.AvailableSettings.CONVENTIONAL_JAVA_CONSTANTS;
import static org.hibernate.cfg.AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE;
import static org.hibernate.cfg.AvailableSettings.CRITERIA_VALUE_HANDLING_MODE; import static org.hibernate.cfg.AvailableSettings.CRITERIA_VALUE_HANDLING_MODE;
import static org.hibernate.cfg.AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY; import static org.hibernate.cfg.AvailableSettings.CUSTOM_ENTITY_DIRTINESS_STRATEGY;
import static org.hibernate.cfg.AvailableSettings.DEFAULT_BATCH_FETCH_SIZE; import static org.hibernate.cfg.AvailableSettings.DEFAULT_BATCH_FETCH_SIZE;
@ -248,7 +246,6 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
private boolean wrapResultSetsEnabled; private boolean wrapResultSetsEnabled;
private TimeZone jdbcTimeZone; private TimeZone jdbcTimeZone;
private boolean queryParametersValidationEnabled; private boolean queryParametersValidationEnabled;
private LiteralHandlingMode criteriaLiteralHandlingMode;
private ValueHandlingMode criteriaValueHandlingMode; private ValueHandlingMode criteriaValueHandlingMode;
private ImmutableEntityUpdateQueryHandlingMode immutableEntityUpdateQueryHandlingMode; private ImmutableEntityUpdateQueryHandlingMode immutableEntityUpdateQueryHandlingMode;
@ -537,9 +534,6 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
true true
); );
this.criteriaLiteralHandlingMode = LiteralHandlingMode.interpret(
configurationSettings.get( CRITERIA_LITERAL_HANDLING_MODE )
);
this.criteriaValueHandlingMode = ValueHandlingMode.interpret( this.criteriaValueHandlingMode = ValueHandlingMode.interpret(
configurationSettings.get( CRITERIA_VALUE_HANDLING_MODE ) configurationSettings.get( CRITERIA_VALUE_HANDLING_MODE )
); );
@ -1135,11 +1129,6 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
return this.queryParametersValidationEnabled; return this.queryParametersValidationEnabled;
} }
@Override
public LiteralHandlingMode getCriteriaLiteralHandlingMode() {
return this.criteriaLiteralHandlingMode;
}
@Override @Override
public ValueHandlingMode getCriteriaValueHandlingMode() { public ValueHandlingMode getCriteriaValueHandlingMode() {
return criteriaValueHandlingMode; return criteriaValueHandlingMode;

View File

@ -28,7 +28,6 @@ import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.loader.BatchFetchStyle; import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode; import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor; import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
@ -410,11 +409,6 @@ public class AbstractDelegatingSessionFactoryOptions implements SessionFactoryOp
return delegate.isQueryParametersValidationEnabled(); return delegate.isQueryParametersValidationEnabled();
} }
@Override
public LiteralHandlingMode getCriteriaLiteralHandlingMode() {
return delegate.getCriteriaLiteralHandlingMode();
}
@Override @Override
public ValueHandlingMode getCriteriaValueHandlingMode() { public ValueHandlingMode getCriteriaValueHandlingMode() {
return delegate.getCriteriaValueHandlingMode(); return delegate.getCriteriaValueHandlingMode();

View File

@ -30,8 +30,6 @@ import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.loader.BatchFetchStyle; import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode; import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
import org.hibernate.query.QueryLiteralRendering;
import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.spi.QueryEngineOptions; import org.hibernate.query.spi.QueryEngineOptions;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@ -269,18 +267,10 @@ public interface SessionFactoryOptions extends QueryEngineOptions {
return isJpaBootstrap(); return isJpaBootstrap();
} }
default LiteralHandlingMode getCriteriaLiteralHandlingMode() {
return LiteralHandlingMode.AUTO;
}
default ValueHandlingMode getCriteriaValueHandlingMode() { default ValueHandlingMode getCriteriaValueHandlingMode() {
return ValueHandlingMode.BIND; return ValueHandlingMode.BIND;
} }
default QueryLiteralRendering getQueryLiteralRenderingMode() {
return getCriteriaLiteralHandlingMode().getCounterpart();
}
JpaCompliance getJpaCompliance(); JpaCompliance getJpaCompliance();
boolean isFailOnPaginationOverCollectionFetchEnabled(); boolean isFailOnPaginationOverCollectionFetchEnabled();

View File

@ -1988,26 +1988,6 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings {
*/ */
String VALIDATE_QUERY_PARAMETERS = "hibernate.query.validate_parameters"; String VALIDATE_QUERY_PARAMETERS = "hibernate.query.validate_parameters";
/**
* By default, Criteria queries uses bind parameters for any literal that is not a numeric value.
*
* However, to increase the likelihood of JDBC statement caching,
* you might want to use bind parameters for numeric values too.
* The {@link org.hibernate.query.criteria.LiteralHandlingMode#BIND} mode will use bind variables for any literal value.
*
* The {@link org.hibernate.query.criteria.LiteralHandlingMode#INLINE} mode will inline literal values as-is.
* To prevent SQL injection, never use {@link org.hibernate.query.criteria.LiteralHandlingMode#INLINE} with String variables.
* Always use constants with the {@link org.hibernate.query.criteria.LiteralHandlingMode#INLINE} mode.
* </p>
* Valid options are defined by the {@link org.hibernate.query.criteria.LiteralHandlingMode} enum.
* </p>
* The default value is {@link org.hibernate.query.criteria.LiteralHandlingMode#AUTO}
*
* @since 5.2.12
* @see org.hibernate.query.criteria.LiteralHandlingMode
*/
String CRITERIA_LITERAL_HANDLING_MODE = "hibernate.criteria.literal_handling_mode";
/** /**
* By default, Criteria queries uses bind parameters for any value passed through the JPA Criteria API. * By default, Criteria queries uses bind parameters for any value passed through the JPA Criteria API.
* *

View File

@ -1,94 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.query;
import java.util.Locale;
import org.hibernate.query.criteria.LiteralHandlingMode;
/**
* Defines possible ways we can handle query literals in terms of JDBC.
*
* @author Steve Ebersole
*
* @since 6.0
*/
public enum QueryLiteralRendering {
/**
* The default - more-or-less an alias for {@link #AS_PARAM_OUTSIDE_SELECT}
*/
AUTO( "auto" ),
/**
* Always render as a SQL literal, never a parameter
*/
AS_LITERAL( "literal" ),
/**
* Always as a parameter, never as a literal
*/
AS_PARAM( "param" ),
/**
* As a parameter when the literal occurs outside the SELECT clause,
* otherwise render as a SQL literal.
*
* Technically this should be called something like AS_PARAM_UNLESS_SELECTED. If the literal is used as an argument
* to a function call in the select-clause, for example, we'd still want to render as a parameter.
*/
AS_PARAM_OUTSIDE_SELECT( "param-outside-select" );
private final String externalForm;
QueryLiteralRendering(String externalForm) {
this.externalForm = externalForm;
}
public String toExternalForm() {
return externalForm;
}
public static QueryLiteralRendering fromExternalForm(Object externalValue) {
if ( externalValue != null ) {
if ( externalValue instanceof QueryLiteralRendering ) {
return (QueryLiteralRendering) externalValue;
}
if ( externalValue instanceof LiteralHandlingMode ) {
return ( (LiteralHandlingMode) externalValue ).getCounterpart();
}
final String externalValueString = externalValue.toString().trim().toLowerCase( Locale.ROOT );
if ( externalValueString.isEmpty() ) {
return null;
}
if ( AS_LITERAL.externalForm.equals( externalValueString ) ) {
return AS_LITERAL;
}
if ( AS_PARAM.externalForm.equals( externalValueString ) ) {
return AS_PARAM;
}
if ( AS_PARAM_OUTSIDE_SELECT.externalForm.equals( externalValueString ) ) {
return AS_PARAM_OUTSIDE_SELECT;
}
try {
final LiteralHandlingMode legacy = LiteralHandlingMode.interpret( externalValue );
return legacy.getCounterpart();
}
catch (Exception ignore) {
}
}
// the default...
return AS_PARAM_OUTSIDE_SELECT;
}
}

View File

@ -1,72 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.query.criteria;
import org.hibernate.HibernateException;
import org.hibernate.query.QueryLiteralRendering;
/**
* This enum defines how literals are handled by JPA Criteria.
*
* By default ({@code AUTO}), Criteria queries uses bind parameters for any literal that is not a numeric value.
*
* However, to increase the likelihood of JDBC statement caching,
* you might want to use bind parameters for numeric values too.
* The {@code BIND} mode will use bind variables for any literal value.
*
* The {@code INLINE} mode will inline literal values as-is.
* To prevent SQL injection, never use {@code INLINE} with String variables.
* Always use constants with the {@code INLINE} mode.
*
* @author Vlad Mihalcea
*
* @deprecated since 6.0, Use {@link QueryLiteralRendering} instead.
*/
@Deprecated
public enum LiteralHandlingMode {
AUTO( QueryLiteralRendering.AUTO ),
BIND( QueryLiteralRendering.AS_PARAM_OUTSIDE_SELECT ),
INLINE( QueryLiteralRendering.AS_LITERAL );
private final QueryLiteralRendering counterpart;
LiteralHandlingMode(QueryLiteralRendering counterpart) {
this.counterpart = counterpart;
}
public QueryLiteralRendering getCounterpart() {
return counterpart;
}
/**
* Interpret the configured literalHandlingMode value.
* Valid values are either a {@link LiteralHandlingMode} object or its String representation.
* For string values, the matching is case insensitive, so you can use either {@code AUTO} or {@code auto}.
*
* @param literalHandlingMode configured {@link LiteralHandlingMode} representation
* @return associated {@link LiteralHandlingMode} object
*/
public static LiteralHandlingMode interpret(Object literalHandlingMode) {
if ( literalHandlingMode == null ) {
return AUTO;
}
else if ( literalHandlingMode instanceof LiteralHandlingMode ) {
return (LiteralHandlingMode) literalHandlingMode;
}
else if ( literalHandlingMode instanceof String ) {
for ( LiteralHandlingMode value : values() ) {
if ( value.name().equalsIgnoreCase( (String) literalHandlingMode ) ) {
return value;
}
}
}
throw new HibernateException(
"Unrecognized literal_handling_mode value : " + literalHandlingMode
+ ". Supported values include 'auto', 'inline', and 'bind'."
);
}
}

View File

@ -60,7 +60,6 @@ import org.hibernate.query.internal.QueryHelper;
import org.hibernate.query.spi.QueryEngine; import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SqmExpressable; import org.hibernate.query.sqm.SqmExpressable;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.SqmQuerySource; import org.hibernate.query.sqm.SqmQuerySource;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor; import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.spi.SqmCreationContext; import org.hibernate.query.sqm.spi.SqmCreationContext;

View File

@ -10,7 +10,7 @@ import java.util.Map;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.query.criteria.LiteralHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
@ -24,8 +24,8 @@ public class CriteriaLiteralHandlingModeBindTest extends AbstractCriteriaLiteral
protected Map getConfig() { protected Map getConfig() {
Map config = super.getConfig(); Map config = super.getConfig();
config.put( config.put(
AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE, AvailableSettings.CRITERIA_VALUE_HANDLING_MODE,
LiteralHandlingMode.BIND ValueHandlingMode.BIND
); );
return config; return config;
} }

View File

@ -23,7 +23,7 @@ public class CriteriaLiteralHandlingModeInlineShortNameLowercaseTest extends Abs
protected Map getConfig() { protected Map getConfig() {
Map config = super.getConfig(); Map config = super.getConfig();
config.put( config.put(
AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE, AvailableSettings.CRITERIA_VALUE_HANDLING_MODE,
"inline" "inline"
); );
return config; return config;

View File

@ -10,7 +10,6 @@ import java.util.Map;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
@ -24,7 +23,7 @@ public class CriteriaLiteralHandlingModeInlineShortNameUppercaseTest extends Abs
protected Map getConfig() { protected Map getConfig() {
Map config = super.getConfig(); Map config = super.getConfig();
config.put( config.put(
AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE, AvailableSettings.CRITERIA_VALUE_HANDLING_MODE,
"INLINE" "INLINE"
); );
return config; return config;

View File

@ -10,7 +10,7 @@ import java.util.Map;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.query.criteria.LiteralHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
@ -24,8 +24,8 @@ public class CriteriaLiteralHandlingModeInlineTest extends AbstractCriteriaLiter
protected Map getConfig() { protected Map getConfig() {
Map config = super.getConfig(); Map config = super.getConfig();
config.put( config.put(
AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE, AvailableSettings.CRITERIA_VALUE_HANDLING_MODE,
LiteralHandlingMode.INLINE ValueHandlingMode.INLINE
); );
return config; return config;
} }

View File

@ -9,9 +9,8 @@ package org.hibernate.jpa.test.criteria.literal;
import java.util.Map; import java.util.Map;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MySQLDialect; import org.hibernate.dialect.MySQLDialect;
import org.hibernate.query.criteria.LiteralHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
@ -25,8 +24,8 @@ public class MySQLCriteriaLiteralHandlingModeInlineTest extends AbstractCriteria
protected Map getConfig() { protected Map getConfig() {
Map config = super.getConfig(); Map config = super.getConfig();
config.put( config.put(
AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE, AvailableSettings.CRITERIA_VALUE_HANDLING_MODE,
LiteralHandlingMode.INLINE ValueHandlingMode.INLINE
); );
return config; return config;
} }

View File

@ -20,7 +20,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.DB2Dialect; import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DerbyDialect; import org.hibernate.dialect.DerbyDialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.criteria.LiteralHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
@ -132,7 +132,7 @@ public class SelectCaseLiteralHandlingBindTest extends BaseEntityManagerFunction
@Override @Override
protected Map getConfig() { protected Map getConfig() {
Map config = super.getConfig(); Map config = super.getConfig();
config.put( AvailableSettings.CRITERIA_LITERAL_HANDLING_MODE, LiteralHandlingMode.BIND ); config.put( AvailableSettings.CRITERIA_VALUE_HANDLING_MODE, ValueHandlingMode.BIND );
return config; return config;
} }

View File

@ -20,7 +20,6 @@ import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl; import org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl;
import org.hibernate.metamodel.MappingMetamodel; import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext; import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.query.criteria.LiteralHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode; import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.hql.HqlTranslator; import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.hql.internal.StandardHqlTranslator; import org.hibernate.query.hql.internal.StandardHqlTranslator;