From 4144c4af4c77391a62b1f98a4ce9d7dc9a2e5fd4 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 18 Mar 2021 15:06:22 +0100 Subject: [PATCH] Fix Criteria NPE for named parameters when paramClass is a Collection --- .../sqm/internal/SqmCriteriaNodeBuilder.java | 2 ++ .../criteria/BasicCriteriaExecutionTests.java | 3 +-- .../criteria/CriteriaParameterTests.java | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java index a90c9f08a0..3aabbf0af9 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java @@ -883,6 +883,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext { if ( Collection.class.isAssignableFrom( paramClass ) ) { // a multi-valued parameter return new JpaCriteriaParameter( + name, new MultiValueParameterType<>( Collection.class ), true, this @@ -892,6 +893,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext { if ( paramClass.isArray() ) { // an array-valued, multi-valued parameter return new JpaCriteriaParameter( + name, new MultiValueParameterType( Object[].class ), true, this diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/BasicCriteriaExecutionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/BasicCriteriaExecutionTests.java index 3337e4cb9c..ea4f8920ee 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/BasicCriteriaExecutionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/BasicCriteriaExecutionTests.java @@ -11,13 +11,12 @@ import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Root; import org.hibernate.dialect.DerbyDialect; - -import org.hibernate.testing.SkipForDialect; import org.hibernate.dialect.H2Dialect; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.criteria.JpaCriteriaQuery; import org.hibernate.query.criteria.JpaRoot; +import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.orm.domain.gambit.BasicEntity; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.RequiresDialect; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaParameterTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaParameterTests.java index 8c4d64c3c7..1874456192 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaParameterTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaParameterTests.java @@ -6,11 +6,14 @@ */ package org.hibernate.orm.test.query.criteria; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.ParameterExpression; +import javax.persistence.criteria.Path; import javax.persistence.criteria.Root; import org.hibernate.query.criteria.HibernateCriteriaBuilder; @@ -42,6 +45,28 @@ public class CriteriaParameterTests { ); } + @Test + public void testNamedParameterBaseline(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + final ParameterExpression parameter = criteriaBuilder.parameter( Collection.class, "datas" ); + final CriteriaQuery criteria = criteriaBuilder.createQuery( BasicEntity.class ); + final Root root = criteria.from( BasicEntity.class ); + Path property = root.get( "data" ); + criteria.where( property.in( parameter ) ); + + final QueryImplementor query = session.createQuery( criteria ); + + List parameterValue = new ArrayList<>(); + parameterValue.add( "fe" ); + + query.setParameter( "datas", parameterValue ); + query.list(); + } + ); + } + @Test public void testMultiValuedParameterBaseline(SessionFactoryScope scope) { scope.inTransaction(