diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/AbstractJdbcParameter.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/AbstractJdbcParameter.java index 55ee80fa1b..8d2b1cb2d9 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/AbstractJdbcParameter.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/AbstractJdbcParameter.java @@ -9,6 +9,7 @@ package org.hibernate.sql.exec.internal; import java.sql.PreparedStatement; import java.sql.SQLException; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.mapping.IndexedConsumer; import org.hibernate.metamodel.mapping.JdbcMapping; @@ -25,6 +26,7 @@ import org.hibernate.sql.exec.spi.JdbcParameterBindings; import org.hibernate.sql.results.internal.SqlSelectionImpl; import org.hibernate.type.BasicType; import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.spi.TypeConfiguration; /** @@ -110,13 +112,29 @@ public abstract class AbstractJdbcParameter else { valueClass = binding.getBindValue().getClass(); } - final BasicType basicType = executionContext.getSession() - .getFactory() - .getTypeConfiguration() - .getBasicTypeRegistry() - .getRegisteredType( valueClass ); - return basicType.getJdbcMapping(); + final SessionFactoryImplementor factory = executionContext.getSession().getFactory(); + final TypeConfiguration typeConfiguration = factory.getTypeConfiguration(); + + final BasicType basicType = typeConfiguration.getBasicTypeRegistry().getRegisteredType( valueClass ); + if ( basicType != null ) { + return basicType.getJdbcMapping(); + } + + final BasicType defaultForJavaType = typeConfiguration.getBasicTypeForJavaType( valueClass ); + if ( defaultForJavaType != null ) { + return defaultForJavaType; + } + + final JavaType javaType = typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( valueClass ); + if ( javaType != null ) { + final JdbcType recommendedJdbcType = javaType.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() ); + if ( recommendedJdbcType != null ) { + return typeConfiguration.getBasicTypeRegistry().resolve( javaType, recommendedJdbcType ); + } + } + + return null; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/contribution/array/StringArrayContributorTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/contribution/array/StringArrayContributorTests.java index b60d766eff..8847736223 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/contribution/array/StringArrayContributorTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/contribution/array/StringArrayContributorTests.java @@ -18,6 +18,8 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.assertj.core.util.Arrays; + /** * @author Steve Ebersole */ @@ -42,6 +44,15 @@ public class StringArrayContributorTests { } ); } + @Test + public void testAsQueryParameter(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createQuery( "select p from Post p where array_contains(:arr, p.title) = true" ) + .setParameter( "arr", Arrays.array( "a", "b" ) ) + .list(); + }); + } + @AfterEach public void dropTestData(SessionFactoryScope scope) { scope.inTransaction( (session) -> session.createQuery( "delete Post" ).executeUpdate() );