From fa48448f77794c87b543e0d5e22f41b7d48fc2c1 Mon Sep 17 00:00:00 2001 From: Karel Maesen Date: Thu, 11 Nov 2021 18:53:32 +0100 Subject: [PATCH] HHH-14925 Fix type resolution in SqmCriteriaNodeBuilder --- .../sqm/internal/SqmCriteriaNodeBuilder.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 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 4ddaa177d3..fd672f7390 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 @@ -128,15 +128,15 @@ import org.hibernate.service.ServiceRegistry; import org.hibernate.type.BasicType; import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.spi.TypeConfiguration; - import static java.util.Arrays.asList; import static org.hibernate.query.internal.QueryHelper.highestPrecedenceType; /** * Acts as a JPA {@link jakarta.persistence.criteria.CriteriaBuilder} by * using SQM nodes as the JPA Criteria nodes - * + * * @author Steve Ebersole */ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, Serializable { @@ -308,7 +308,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, @Override public SqmExpression cast(JpaExpression expression, Class castTargetJavaType) { final BasicDomainType type = getTypeConfiguration().standardBasicTypeForJavaType( castTargetJavaType ); - return getFunctionDescriptor("cast").generateSqmExpression( + return getFunctionDescriptor( "cast" ).generateSqmExpression( asList( (SqmTypedNode) expression, new SqmCastTarget<>( type, this ) ), type, queryEngine, @@ -1450,7 +1450,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, } else { //noinspection unchecked - basicType = getTypeConfiguration().getBasicTypeForJavaType( (Class) value.getClass() ); + basicType = guessType( value ); } return new JpaCriteriaParameter<>( basicType, @@ -1460,6 +1460,25 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, } } + @SuppressWarnings("unchecked") + private BasicType guessType(T value) { + BasicType basicType; + Class valueClass = (Class) value.getClass(); + basicType = getTypeConfiguration().getBasicTypeForJavaType( valueClass ); + if ( basicType == null ) { + final JavaType javaType = getTypeConfiguration().getJavaTypeDescriptorRegistry() + .getDescriptor( valueClass ); + if ( javaType != null ) { + final JdbcType recommendedJdbcType = javaType.getRecommendedJdbcType( getTypeConfiguration().getCurrentBaseSqlTypeIndicators() ); + if ( recommendedJdbcType != null ) { + basicType = (BasicType) getTypeConfiguration().getBasicTypeRegistry().resolve( javaType, recommendedJdbcType); + } + } + } + assert basicType != null; + return basicType; + } + @Override public > SqmExpression> values(C collection) { throw new NotYetImplementedFor6Exception(); @@ -1538,7 +1557,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, } private SqmFunctionDescriptor getFunctionDescriptor(String name) { - return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor( name); + return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor( name ); } @Override