From 3e97b1c205f91c19ad594bb9066b12d051e52cfc Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 12 Nov 2021 17:33:55 +0100 Subject: [PATCH] Unify parameter type resolving --- .../spi/SessionFactoryDelegatingImpl.java | 4 +-- .../internal/SessionFactoryImpl.java | 4 +-- .../domain/internal/MappingMetamodelImpl.java | 29 ++++++++++----- .../hibernate/proxy/HibernateProxyHelper.java | 5 +-- .../QueryParameterBindingTypeResolver.java | 4 +-- .../sqm/internal/SqmCriteriaNodeBuilder.java | 32 ++--------------- .../exec/internal/AbstractJdbcParameter.java | 36 ++++--------------- 7 files changed, 39 insertions(+), 75 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java index 7fd4a45ca7..51811b741c 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java @@ -437,12 +437,12 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor, } @Override - public AllowableParameterType resolveParameterBindType(Object bindValue) { + public AllowableParameterType resolveParameterBindType(T bindValue) { return delegate.resolveParameterBindType( bindValue ); } @Override - public AllowableParameterType resolveParameterBindType(Class clazz) { + public AllowableParameterType resolveParameterBindType(Class clazz) { return delegate.resolveParameterBindType( clazz ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index e51015de4a..b03cafbb66 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -1081,7 +1081,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { } @Override - public AllowableParameterType resolveParameterBindType(Object bindValue) { + public AllowableParameterType resolveParameterBindType(T bindValue) { if ( bindValue == null ) { // we can't guess return null; @@ -1091,7 +1091,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { } @Override - public AllowableParameterType resolveParameterBindType(Class javaType) { + public AllowableParameterType resolveParameterBindType(Class javaType) { return getMetamodel().resolveQueryParameterType( javaType ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java index dfb2a9176e..7be4355d6a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/MappingMetamodelImpl.java @@ -71,6 +71,8 @@ import org.hibernate.query.sqm.tree.expression.SqmFieldLiteral; import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.type.BasicType; import org.hibernate.type.Type; +import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.spi.TypeConfiguration; import static org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting; @@ -834,17 +836,28 @@ public class MappingMetamodelImpl implements MappingMetamodel, MetamodelImplemen } @Override - public AllowableParameterType resolveQueryParameterType(Class javaType) { - final BasicType basicType = getTypeConfiguration().getBasicTypeForJavaType( javaType ); - if ( basicType != null ) { - //noinspection unchecked + public AllowableParameterType resolveQueryParameterType(Class javaClass) { + final BasicType basicType = getTypeConfiguration().getBasicTypeForJavaType( javaClass ); + // For enums, we simply don't know the exact mapping if there is no basic type registered + if ( basicType != null || javaClass.isEnum() ) { return basicType; } - final ManagedDomainType managedType = jpaMetamodel.findManagedType( javaType ); - if ( managedType instanceof AllowableParameterType ) { - //noinspection unchecked - return (AllowableParameterType) managedType; + final ManagedDomainType managedType = jpaMetamodel.findManagedType( javaClass ); + if ( managedType != null ) { + return managedType; + } + + final JavaType javaType = getTypeConfiguration().getJavaTypeDescriptorRegistry() + .findDescriptor( javaClass ); + if ( javaType != null ) { + final JdbcType recommendedJdbcType = javaType.getRecommendedJdbcType( getTypeConfiguration().getCurrentBaseSqlTypeIndicators() ); + if ( recommendedJdbcType != null ) { + return getTypeConfiguration().getBasicTypeRegistry().resolve( + javaType, + recommendedJdbcType + ); + } } return null; diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/HibernateProxyHelper.java b/hibernate-core/src/main/java/org/hibernate/proxy/HibernateProxyHelper.java index 26e520fc22..edb504fde8 100644 --- a/hibernate-core/src/main/java/org/hibernate/proxy/HibernateProxyHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/proxy/HibernateProxyHelper.java @@ -19,14 +19,15 @@ public final class HibernateProxyHelper { * of a proxy (without initializing the proxy!). It is * almost always better to use the entity name! */ - public static Class getClassWithoutInitializingProxy(Object object) { + @SuppressWarnings("unchecked") + public static Class getClassWithoutInitializingProxy(T object) { if (object instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy) object; LazyInitializer li = proxy.getHibernateLazyInitializer(); return li.getPersistentClass(); } else { - return object.getClass(); + return (Class) object.getClass(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryParameterBindingTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryParameterBindingTypeResolver.java index 9b5f93695f..daa717c9db 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryParameterBindingTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryParameterBindingTypeResolver.java @@ -16,7 +16,7 @@ import org.hibernate.type.spi.TypeConfiguration; * @author Steve Ebersole */ public interface QueryParameterBindingTypeResolver { - AllowableParameterType resolveParameterBindType(Object bindValue); - AllowableParameterType resolveParameterBindType(Class clazz); + AllowableParameterType resolveParameterBindType(T bindValue); + AllowableParameterType resolveParameterBindType(Class clazz); TypeConfiguration getTypeConfiguration(); } 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 0097342690..83098726ea 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 @@ -900,10 +900,9 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return new SqmLiteralNull<>( this ); } - //noinspection unchecked return new SqmLiteral<>( value, - getTypeConfiguration().standardBasicTypeForJavaType( (Class) value.getClass() ), + queryEngine.getTypeConfiguration().getSessionFactory().resolveParameterBindType( value ), this ); } @@ -1444,41 +1443,14 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return literal( value ); } else { - final BasicType basicType; - if ( value == null ) { - basicType = null; - } - else { - //noinspection unchecked - basicType = guessType( value ); - } return new JpaCriteriaParameter<>( - basicType, + queryEngine.getTypeConfiguration().getSessionFactory().resolveParameterBindType( value ), value, this ); } } - @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() - .findDescriptor( 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(); 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 3e4352aa3c..cb094b5eaf 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 @@ -15,6 +15,7 @@ import org.hibernate.mapping.IndexedConsumer; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.MappingModelExpressable; import org.hibernate.metamodel.mapping.SqlExpressable; +import org.hibernate.metamodel.model.domain.AllowableParameterType; import org.hibernate.sql.ast.Clause; import org.hibernate.sql.ast.spi.SqlSelection; import org.hibernate.sql.ast.tree.expression.JdbcParameter; @@ -102,38 +103,15 @@ public abstract class AbstractJdbcParameter } private JdbcMapping guessBindType(ExecutionContext executionContext, JdbcParameterBinding binding, JdbcMapping jdbcMapping) { - final Class valueClass; - if ( binding.getBindValue() == null ) { - if ( jdbcMapping != null ) { - return jdbcMapping; - } - valueClass = Object.class; - } - else { - valueClass = binding.getBindValue().getClass(); + if ( binding.getBindValue() == null && jdbcMapping != null ) { + return jdbcMapping; } - 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 AllowableParameterType parameterType = executionContext.getSession().getFactory() + .resolveParameterBindType( binding.getBindValue() ); + if ( parameterType instanceof JdbcMapping ) { + return (JdbcMapping) parameterType; } - - final BasicType defaultForJavaType = typeConfiguration.getBasicTypeForJavaType( valueClass ); - if ( defaultForJavaType != null ) { - return defaultForJavaType; - } - - final JavaType javaType = typeConfiguration.getJavaTypeDescriptorRegistry().findDescriptor( valueClass ); - if ( javaType != null ) { - final JdbcType recommendedJdbcType = javaType.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() ); - if ( recommendedJdbcType != null ) { - return typeConfiguration.getBasicTypeRegistry().resolve( javaType, recommendedJdbcType ); - } - } - return null; }