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 a910dd7ac3..2609845efa 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 @@ -1133,11 +1133,18 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return new SqmLiteral<>( value, expressible, this ); } // Just like in HQL, we allow coercion of literal values to the inferred type - return new SqmLiteral<>( - expressible.getExpressibleJavaType().coerce( value, this::getTypeConfiguration ), - expressible, - this - ); + T coercedValue = expressible.getExpressibleJavaType().coerce( value, this::getTypeConfiguration ); + if (expressible.getExpressibleJavaType().isInstance( coercedValue )) { + return new SqmLiteral<>( + coercedValue, + expressible, + this + ); + } + else { + // ignore typeInferenceSource and fallback the value type + return literal( value ); + } } private static SqmExpressible resolveInferredType( @@ -1700,11 +1707,22 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, ); } final SqmExpressible expressible = bindableType.resolveExpressible( getTypeConfiguration().getSessionFactory() ); - return new ValueBindJpaCriteriaParameter<>( - bindableType, - expressible.getExpressibleJavaType().coerce( value, this::getTypeConfiguration ), - this - ); + T coercedValue = expressible.getExpressibleJavaType().coerce( value, this::getTypeConfiguration ); + if ( isInstance( bindableType, coercedValue ) ) { + return new ValueBindJpaCriteriaParameter<>( + bindableType, + coercedValue, + this + ); + } + else { + // ignore typeInferenceSource and fallback the value type + return new ValueBindJpaCriteriaParameter<>( + queryEngine.getTypeConfiguration().getSessionFactory().resolveParameterBindType( value ), + value, + this + ); + } } private boolean isInstance(BindableType bindableType, T value) {