From 1c100f5af0ab34a64b08bf7dbbef426ea6369640 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 27 Jul 2021 09:17:15 -0500 Subject: [PATCH] Tests for binding composite-valued query parameters --- .../sqm/internal/SqmCriteriaNodeBuilder.java | 43 +++++-------------- .../spi/JavaTypeDescriptorBasicAdaptor.java | 4 +- .../spi/JdbcTypeRecommendationException.java | 26 +++++++++++ .../sqm/param/CompositeParameterTests.java | 4 -- 4 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JdbcTypeRecommendationException.java 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 30485b3599..df1018a2e9 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 @@ -119,6 +119,7 @@ import org.hibernate.service.ServiceRegistry; import org.hibernate.type.BasicType; import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; +import org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException; import org.hibernate.type.spi.TypeConfiguration; import static java.util.Arrays.asList; @@ -898,36 +899,14 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext { @Override public JpaCriteriaParameter parameter(Class paramClass) { - if ( Collection.class.isAssignableFrom( paramClass ) ) { - // a Collection-valued, multi-valued parameter - return new JpaCriteriaParameter( - new MultiValueParameterType( Collection.class ), - true, - this - ); - } - - if ( paramClass.isArray() ) { - // an array-valued, multi-valued parameter - return new JpaCriteriaParameter( - new MultiValueParameterType( Object[].class ), - true, - this - ); - } - - //noinspection unchecked - return new JpaCriteriaParameter<>( - getTypeConfiguration().standardBasicTypeForJavaType( paramClass ), - false, - this - ); + return parameter( paramClass, null ); } @Override public JpaCriteriaParameter parameter(Class paramClass, String name) { + if ( Collection.class.isAssignableFrom( paramClass ) ) { - // a multi-valued parameter + // a Collection-valued, multi-valued parameter return new JpaCriteriaParameter( name, new MultiValueParameterType<>( Collection.class ), @@ -946,13 +925,13 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext { ); } - //noinspection unchecked - return new JpaCriteriaParameter<>( - name, - getTypeConfiguration().standardBasicTypeForJavaType( paramClass ), - false, - this - ); + try { + final BasicType basicType = getTypeConfiguration().standardBasicTypeForJavaType( paramClass ); + return new JpaCriteriaParameter<>( name, basicType, false, this ); + } + catch (JdbcTypeRecommendationException e) { + return new JpaCriteriaParameter<>( name, null, false, this ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java index 07de31230e..72a75a3746 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java @@ -29,8 +29,8 @@ public class JavaTypeDescriptorBasicAdaptor extends AbstractClassTypeDescript @Override public JdbcTypeDescriptor getRecommendedJdbcType(JdbcTypeDescriptorIndicators context) { - throw new UnsupportedOperationException( - "Recommended SqlTypeDescriptor not known for this Java type : " + getJavaType().getTypeName() + throw new JdbcTypeRecommendationException( + "Could not determine recommended JdbcTypeDescriptor for `" + getJavaType().getTypeName() + "`" ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JdbcTypeRecommendationException.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JdbcTypeRecommendationException.java new file mode 100644 index 0000000000..5fcd1bb38f --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JdbcTypeRecommendationException.java @@ -0,0 +1,26 @@ +/* + * 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.type.descriptor.java.spi; + +import org.hibernate.HibernateException; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; + +/** + * Exception indicating {@link JavaTypeDescriptor#getRecommendedJdbcType} could not + * determine a recommended JDBC type descriptor + * + * @author Steve Ebersole + */ +public class JdbcTypeRecommendationException extends HibernateException { + public JdbcTypeRecommendationException(String message) { + super( message ); + } + + public JdbcTypeRecommendationException(String message, Throwable cause) { + super( message, cause ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/param/CompositeParameterTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/param/CompositeParameterTests.java index d494665e61..6bce82bbaa 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/param/CompositeParameterTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sqm/param/CompositeParameterTests.java @@ -22,7 +22,6 @@ import org.hibernate.query.criteria.JpaRoot; import org.hibernate.query.spi.QueryImplementor; import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.NotImplementedYet; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; @@ -52,7 +51,6 @@ public class CompositeParameterTests { } @Test - @NotImplementedYet public void testSimplePredicateCriteria(SessionFactoryScope scope) { final HibernateCriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); @@ -77,7 +75,6 @@ public class CompositeParameterTests { } @Test - @NotImplementedYet public void testInPredicateCriteria(SessionFactoryScope scope) { final HibernateCriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel(); @@ -102,7 +99,6 @@ public class CompositeParameterTests { } @Test - @NotImplementedYet public void testDeTypedInPredicateCriteria(SessionFactoryScope scope) { final HibernateCriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder(); final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();