HHH-15968 Error when parameter used as both single and multi value

This commit is contained in:
Marco Belladelli 2023-08-11 10:35:49 +02:00 committed by Christian Beikov
parent 7b942b0c6a
commit 1735b984d8
4 changed files with 117 additions and 4 deletions

View File

@ -3763,6 +3763,9 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
parameterCollector.addParameter( parameter );
return parameter;
}
else if ( existingParameter.allowMultiValuedBinding() && !parameter.allowMultiValuedBinding() ) {
existingParameter.disallowMultiValuedBinding();
}
//noinspection unchecked
return (T) existingParameter;
}

View File

@ -9,6 +9,7 @@ package org.hibernate.query.internal;
import java.util.Collection;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
@ -19,7 +20,6 @@ import org.hibernate.query.QueryParameter;
import org.hibernate.query.spi.QueryParameterBinding;
import org.hibernate.query.spi.QueryParameterBindingValidator;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.type.descriptor.java.CoercionException;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.JavaTypeHelper;
import org.hibernate.type.spi.TypeConfiguration;
@ -119,7 +119,7 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
coerced = value;
}
}
catch (CoercionException ce) {
catch (HibernateException ce) {
throw new IllegalArgumentException(
String.format(
"Parameter value [%s] did not match expected type [%s]",
@ -233,7 +233,7 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
try {
coerced = coerce( value, bindType );
}
catch (CoercionException ex) {
catch (HibernateException ex) {
throw new IllegalArgumentException(
String.format(
"Parameter value [%s] did not match expected type [%s (%s)]",
@ -277,6 +277,12 @@ public class QueryParameterBindingImpl<T> implements QueryParameterBinding<T>, J
@Override
public void setBindValues(Collection<? extends T> values) {
if ( !queryParameter.allowsMultiValuedBinding() ) {
throw new IllegalArgumentException(
"Illegal attempt to bind a collection value to a single-valued parameter"
);
}
this.isBound = true;
this.isMultiValued = true;

View File

@ -17,7 +17,7 @@ import org.hibernate.query.sqm.SqmExpressible;
* @author Steve Ebersole
*/
public abstract class AbstractSqmParameter<T> extends AbstractSqmExpression<T> implements SqmParameter<T> {
private final boolean canBeMultiValued;
private boolean canBeMultiValued;
public AbstractSqmParameter(
boolean canBeMultiValued,
@ -56,6 +56,10 @@ public abstract class AbstractSqmParameter<T> extends AbstractSqmExpression<T> i
return canBeMultiValued;
}
public void disallowMultiValuedBinding() {
this.canBeMultiValued = false;
}
@Override
public BindableType<T> getAnticipatedType() {
return this.getNodeType();

View File

@ -0,0 +1,100 @@
/*
* 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.orm.test.hql;
import java.util.Collections;
import java.util.List;
import org.hibernate.query.SemanticException;
import org.hibernate.testing.orm.domain.gambit.BasicEntity;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* @author Marco Belladelli
*/
@DomainModel( annotatedClasses = BasicEntity.class )
@SessionFactory
@Jira( "https://hibernate.atlassian.net/browse/HHH-15968" )
public class ParameterIsNullTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.persist( new BasicEntity( 1, "data_1" ) );
session.persist( new BasicEntity( 2, "data_2" ) );
session.persist( new BasicEntity( 3, null ) );
} );
}
@Test
public void testNonNullBasicParam(SessionFactoryScope scope) {
scope.inTransaction( session -> assertThat( session.createQuery(
"where :param is null or data = :param",
BasicEntity.class
).setParameter( "param", "data_1" ).getResultList() ).hasSize( 1 ) );
}
@Test
public void testNullBasicParam(SessionFactoryScope scope) {
scope.inTransaction( session -> assertThat( session.createQuery(
"where :param is null or data = :param",
BasicEntity.class
).setParameter( "param", null ).getResultList() ).hasSize( 3 ) );
}
@Test
public void testNullCollectionParam(SessionFactoryScope scope) {
scope.inTransaction( session -> assertThat( session.createQuery(
"where :param is null or data in :param",
BasicEntity.class
).setParameter( "param", null ).getResultList() ).hasSize( 3 ) );
}
@Test
public void testEmptyCollectionParam(SessionFactoryScope scope) {
scope.inTransaction( session -> {
try {
session.createQuery(
"where :param is null or data in :param",
BasicEntity.class
).setParameter( "param", Collections.emptyList() ).getResultList();
fail( "A collection value should not be bound to a single-valued parameter" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( IllegalArgumentException.class );
assertThat( e.getMessage() ).contains( "Parameter value", "did not match expected type" );
}
} );
}
@Test
public void testEmptyCollectionListParam(SessionFactoryScope scope) {
scope.inTransaction( session -> {
try {
session.createQuery(
"where :param is null or data in :param",
BasicEntity.class
).setParameterList( "param", Collections.emptyList() ).getResultList();
fail( "Using #setParameterList should not be allowed for single-valued parameter" );
}
catch (Exception e) {
assertThat( e ).isInstanceOf( IllegalArgumentException.class );
assertThat( e.getMessage() ).contains(
"Illegal attempt to bind a collection value to a single-valued parameter"
);
}
} );
}
}