From 46041a138d5e39d3f064350e93ee74f777a3c052 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 21 Apr 2023 11:28:16 +0200 Subject: [PATCH] HHH-16471 Entities serialized in QueryKey causes java.lang.ClassCastException --- .../persister/entity/EntityPersister.java | 2 +- .../internal/QueryParameterBindingsImpl.java | 40 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java index 60d53b2ee9..f3239605aa 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java @@ -1017,7 +1017,7 @@ public interface EntityPersister extends EntityMappingType, RootTableGroupProduc MutableCacheKeyBuilder cacheKey, Object value, SharedSessionContractImplementor session) { - getIdentifierMapping().addToCacheKey( cacheKey, value, session ); + getIdentifierMapping().addToCacheKey( cacheKey, getIdentifier( value, session ), session ); } BytecodeEnhancementMetadata getInstrumentationMetadata(); diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java index ad6fe727e4..ed0e045373 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java @@ -12,6 +12,7 @@ import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiConsumer; @@ -22,8 +23,8 @@ import org.hibernate.cache.MutableCacheKeyBuilder; import org.hibernate.cache.spi.QueryKey; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.MappingModelExpressible; +import org.hibernate.query.BindableType; import org.hibernate.query.QueryParameter; import org.hibernate.query.spi.ParameterMetadataImplementor; import org.hibernate.query.spi.QueryParameterBinding; @@ -169,9 +170,13 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { public QueryKey.ParameterBindingsMemento generateQueryKeyMemento(SharedSessionContractImplementor persistenceContext) { final MutableCacheKeyImpl mutableCacheKey = new MutableCacheKeyImpl(parameterBindingMap.size()); - for ( QueryParameterBinding binding : parameterBindingMap.values() ) { - final MappingModelExpressible mappingType = determineMappingType( binding, persistenceContext ); - assert mappingType instanceof JavaTypedExpressible; + for ( Map.Entry, QueryParameterBinding> entry : parameterBindingMap.entrySet() ) { + final QueryParameterBinding binding = entry.getValue(); + final MappingModelExpressible mappingType = determineMappingType( + binding, + entry.getKey(), + persistenceContext + ); if ( binding.isMultiValued() ) { for ( Object bindValue : binding.getBindValues() ) { @@ -188,26 +193,24 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { return mutableCacheKey.build(); } - private MappingModelExpressible determineMappingType(QueryParameterBinding binding, SharedSessionContractImplementor session) { - if ( binding.getBindType() != null ) { - if ( binding.getBindType() instanceof MappingModelExpressible) { + private MappingModelExpressible determineMappingType(final QueryParameterBinding binding, final QueryParameter queryParameter, final SharedSessionContractImplementor session) { + final BindableType bindType = binding.getBindType(); + if ( bindType != null ) { + if ( bindType instanceof MappingModelExpressible ) { //noinspection unchecked - return (MappingModelExpressible) binding.getBindType(); + return (MappingModelExpressible) bindType; } } final MappingModelExpressible type = binding.getType(); if ( type != null ) { - if ( type instanceof EntityMappingType ) { - return ( (EntityMappingType) type ).getIdentifierMapping(); - } return type; } final TypeConfiguration typeConfiguration = session.getFactory().getTypeConfiguration(); - if ( binding.getBindType() instanceof JavaTypedExpressible) { - final JavaTypedExpressible javaTypedExpressible = (JavaTypedExpressible) binding.getBindType(); + if ( bindType instanceof JavaTypedExpressible) { + final JavaTypedExpressible javaTypedExpressible = (JavaTypedExpressible) bindType; final JavaType jtd = javaTypedExpressible.getExpressibleJavaType(); if ( jtd.getJavaTypeClass() != null ) { // avoid dynamic models @@ -228,7 +231,16 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { else if ( binding.getBindValue() != null ) { return typeConfiguration.getBasicTypeForJavaType( binding.getBindValue().getClass() ); } - return typeConfiguration.getBasicTypeForJavaType( binding.getBindType().getBindableJavaType() ); + + if ( bindType == null ) { + if ( queryParameter.getName() != null ) { + throw new QueryException( "Cannot determine mapping type for parameter : " + queryParameter.getName() ); + } + else { + throw new QueryException( "Cannot determine mapping type for parameter : " + queryParameter.getPosition() ); + } + } + return typeConfiguration.getBasicTypeForJavaType( bindType.getBindableJavaType() ); } private static class MutableCacheKeyImpl implements MutableCacheKeyBuilder {