HHH-13932: Check for non existing query parameter in CriteriaQueryTypeQueryAdapter and throw an exception with the corresponding message

This commit is contained in:
Katiforis 2020-04-18 21:02:29 +03:00
parent 3f2a4947a7
commit f572141ce9
2 changed files with 20 additions and 1 deletions

View File

@ -370,7 +370,10 @@ public class CriteriaQueryTypeQueryAdapter<X> implements QueryImplementor<X> {
return (ExplicitParameterInfo<?>) param; return (ExplicitParameterInfo<?>) param;
} }
else if ( ParameterExpression.class.isInstance( param ) ) { else if ( ParameterExpression.class.isInstance( param ) ) {
return explicitParameterInfoMap.get( param ); ExplicitParameterInfo<?> parameterInfo = explicitParameterInfoMap.get( param );
if ( parameterInfo != null ) {
return parameterInfo;
}
} }
else { else {
for ( ExplicitParameterInfo<?> parameterInfo : explicitParameterInfoMap.values() ) { for ( ExplicitParameterInfo<?> parameterInfo : explicitParameterInfoMap.values() ) {

View File

@ -113,6 +113,22 @@ public class CriteriaQueryTypeQueryAdapterTest extends BaseEntityManagerFunction
} ); } );
} }
@Test(expected = IllegalArgumentException.class)
@TestForIssue(jiraKey = "HHH-13932")
public void testCriteriaQuerySetNonExistingParameter() {
doInJPA( this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Item> query = builder.createQuery( Item.class );
Root<Item> root = query.from( Item.class );
ParameterExpression<String> parameter = builder.parameter( String.class, "name" );
Predicate predicate = builder.equal( root.get( "name" ), parameter );
query.where( predicate );
TypedQuery<Item> criteriaQuery = entityManager.createQuery( query );
ParameterExpression<String> nonExistingParam = builder.parameter( String.class, "nonExistingParam" );
criteriaQuery.setParameter( nonExistingParam, "George" );
} );
}
@Test @Test
public void testSetParameterPassingTypeNotFails() { public void testSetParameterPassingTypeNotFails() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {