diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayContainsArgumentTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayContainsArgumentTypeResolver.java index af076cb3aa..f52ce438e3 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayContainsArgumentTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayContainsArgumentTypeResolver.java @@ -47,10 +47,15 @@ public class ArrayContainsArgumentTypeResolver implements FunctionArgumentTypeRe } } else if ( argumentIndex == 1 ) { + final SqmTypedNode nodeToResolve = function.getArguments().get( 1 ); + if ( nodeToResolve.getExpressible() instanceof MappingModelExpressible ) { + // If the node already has suitable type, don't infer it to be treated as an array + return null; + } final SqmTypedNode node = function.getArguments().get( 0 ); if ( node instanceof SqmExpression ) { final MappingModelExpressible expressible = converter.determineValueMapping( (SqmExpression) node ); - if ( expressible != null ) { + if ( expressible instanceof BasicPluralType ) { return expressible; } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java index f52904113f..17b152e564 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java @@ -2886,11 +2886,14 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implem final SqmExpression arrayExpr = (SqmExpression) arrayInListContext.expression().accept( this ); final SqmExpressible arrayExpressible = arrayExpr.getExpressible(); - if ( arrayExpressible != null && !( arrayExpressible.getSqmType() instanceof BasicPluralType) ) { - throw new SemanticException( - "Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(), - query - ); + if ( arrayExpressible != null ) { + if ( !(arrayExpressible.getSqmType() instanceof BasicPluralType) ) { + throw new SemanticException( + "Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(), + query + ); + } + testExpression.applyInferableType( ( (BasicPluralType) arrayExpressible.getSqmType() ).getElementType() ); } final SelfRenderingSqmFunction contains = getFunctionDescriptor( "array_contains" ).generateSqmExpression( asList( arrayExpr, testExpression ), diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayContainsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayContainsTest.java index 2167a8ed5a..9bfdf9680d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayContainsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayContainsTest.java @@ -17,6 +17,7 @@ import org.hibernate.testing.jdbc.SharedDriverManagerTypeCacheClearingIntegrator import org.hibernate.testing.orm.junit.BootstrapServiceRegistry; import org.hibernate.testing.orm.junit.DialectFeatureChecks; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.RequiresDialectFeature; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; @@ -157,4 +158,22 @@ public class ArrayContainsTest { } ); } + @Test + @JiraKey( "HHH-18851" ) + public void testInArray(SessionFactoryScope scope) { + scope.inSession( em -> { + List results = em.createQuery( + "select e.id " + + "from EntityWithArrays e " + + "where :p in e.theArray", + Tuple.class + ) + .setParameter( "p", "abc" ) + .getResultList(); + + assertEquals( 1, results.size() ); + assertEquals( 2L, results.get( 0 ).get( 0 ) ); + } ); + } + }