diff --git a/hibernate-core/src/main/java/org/hibernate/criterion/InExpression.java b/hibernate-core/src/main/java/org/hibernate/criterion/InExpression.java index 9027a0a031..3c00bb9e9b 100644 --- a/hibernate-core/src/main/java/org/hibernate/criterion/InExpression.java +++ b/hibernate-core/src/main/java/org/hibernate/criterion/InExpression.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import org.hibernate.Criteria; import org.hibernate.EntityMode; +import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.TypedValue; import org.hibernate.internal.util.StringHelper; import org.hibernate.type.CompositeType; @@ -41,22 +42,20 @@ public class InExpression implements Criterion { @Override public String toSqlString( Criteria criteria, CriteriaQuery criteriaQuery ) { final String[] columns = criteriaQuery.findColumns( propertyName, criteria ); - if ( criteriaQuery.getFactory().getDialect().supportsRowValueConstructorSyntaxInInList() || columns.length <= 1 ) { + Dialect dialect = criteriaQuery.getFactory().getDialect(); + if ( dialect.supportsRowValueConstructorSyntaxInInList() || columns.length <= 1 ) { String singleValueParam = StringHelper.repeat( "?, ", columns.length - 1 ) + "?"; if ( columns.length > 1 ) { singleValueParam = '(' + singleValueParam + ')'; } - String params = values.length > 0 + final String params = values.length > 0 ? StringHelper.repeat( singleValueParam + ", ", values.length - 1 ) + singleValueParam - : ""; + // Deal with HHH-8901 : not all databases like to have an empty list. + : dialect.supportsEmptyInList() ? "" : "null"; String cols = String.join( ", ", columns ); if ( columns.length > 1 ) { cols = '(' + cols + ')'; } - // HHH-8901 - if ( ! criteriaQuery.getFactory().getDialect().supportsEmptyInList() && params.isEmpty() ) { - params = "null"; - } return cols + " in (" + params + ')'; } else { diff --git a/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/InPredicate.java b/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/InPredicate.java index a81e73aed5..9b29e5bcd8 100755 --- a/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/InPredicate.java +++ b/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/predicate/InPredicate.java @@ -181,24 +181,32 @@ public class InPredicate // subquery expressions are already wrapped in parenthesis, so we only need to // render the parenthesis here if the values represent an explicit value list - boolean isInSubqueryPredicate = getValues().size() == 1 - && Subquery.class.isInstance( getValues().get( 0 ) ); + List> values = getValues(); + boolean isInSubqueryPredicate = values.size() == 1 + && Subquery.class.isInstance( values.get( 0 ) ); if ( isInSubqueryPredicate ) { - buffer.append( ( (Renderable) getValues().get(0) ).render( renderingContext ) ); + buffer.append( ( (Renderable) values.get( 0 ) ).render( renderingContext ) ); } else { - buffer.append( '(' ); - String sep = ""; - for ( Expression value : getValues() ) { - buffer.append( sep ) - .append( ( (Renderable) value ).render( renderingContext ) ); - sep = ", "; + if ( values.isEmpty() ) { + if ( renderingContext.getDialect().supportsEmptyInList() ) { + buffer.append( "()" ); + } + else { + buffer.append( "(null)" ); + } } - // HHH-8901 - if ( ! renderingContext.getDialect().supportsEmptyInList() && getValues().isEmpty() ) { - buffer.append( "null" ); + else { + buffer.append( '(' ); + String sep = ""; + for ( Expression value : values) { + buffer.append( sep ) + .append( ( (Renderable) value ) + .render( renderingContext ) ); + sep = ", "; + } + buffer.append( ')' ); } - buffer.append( ')' ); } return buffer.toString(); } diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java index 7f0caeb957..3014162798 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java @@ -637,16 +637,18 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { parameterBindingMap.put( syntheticParam, syntheticBinding ); } + String expansionListAsString = expansionList.toString(); + // HHH-8901 - if ( ! dialect.supportsEmptyInList() && expansionList.length() == 0 ) { - expansionList.append( "null" ); + if ( ! dialect.supportsEmptyInList() && expansionListAsString.isEmpty() ) { + expansionListAsString = "null"; } queryString = StringHelper.replace( beforePlaceholder, afterPlaceholder, sourceToken, - expansionList.toString(), + expansionListAsString, true, true );