HHH-14905 - Verify that custom JavaType and JdbcType registration combo works

This commit is contained in:
Steve Ebersole 2021-11-04 13:09:22 -05:00
parent 31eb3b82c5
commit 0b46966096
2 changed files with 35 additions and 6 deletions

View File

@ -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,15 +112,31 @@ public abstract class AbstractJdbcParameter
else {
valueClass = binding.getBindValue().getClass();
}
final BasicType<?> basicType = executionContext.getSession()
.getFactory()
.getTypeConfiguration()
.getBasicTypeRegistry()
.getRegisteredType( valueClass );
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<Object> 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
public MappingModelExpressable getExpressionType() {
return this;

View File

@ -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() );