HHH-16617 Add filter parameters to parameter bindings memento cache key

This commit is contained in:
Marco Belladelli 2023-05-24 16:16:33 +02:00
parent f1710fbb4f
commit 094ac13646
No known key found for this signature in database
GPG Key ID: D1D0C3030AE3AA35
3 changed files with 52 additions and 19 deletions

View File

@ -6,9 +6,15 @@
*/ */
package org.hibernate.engine.internal; package org.hibernate.engine.internal;
import java.io.Serializable;
import org.hibernate.cache.MutableCacheKeyBuilder;
import org.hibernate.cache.spi.access.CachedDomainDataAccess; import org.hibernate.cache.spi.access.CachedDomainDataAccess;
import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.JavaType;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -35,4 +41,30 @@ public final class CacheHelper {
return cachedValue; return cachedValue;
} }
public static void addBasicValueToCacheKey(
MutableCacheKeyBuilder cacheKey,
Object value,
JdbcMapping jdbcMapping,
SharedSessionContractImplementor session) {
if ( value == null ) {
cacheKey.addValue( null );
cacheKey.addHashCode( 0 );
return;
}
final BasicValueConverter converter = jdbcMapping.getValueConverter();
final Serializable disassemble;
final int hashCode;
if ( converter == null ) {
disassemble = jdbcMapping.getJavaTypeDescriptor().getMutabilityPlan().disassemble( value, session );
hashCode = ( (JavaType) jdbcMapping.getMappedJavaType() ).extractHashCode( value );
}
else {
final Object relationalValue = converter.toRelationalValue( value );
final JavaType relationalJavaType = converter.getRelationalJavaType();
disassemble = relationalJavaType.getMutabilityPlan().disassemble( relationalValue, session );
hashCode = relationalJavaType.extractHashCode( relationalValue );
}
cacheKey.addValue( disassemble );
cacheKey.addHashCode( hashCode );
}
} }

View File

@ -15,6 +15,8 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import static org.hibernate.engine.internal.CacheHelper.addBasicValueToCacheKey;
/** /**
* Any basic-typed ValueMapping. Generally this would be one of<ul> * Any basic-typed ValueMapping. Generally this would be one of<ul>
* <li>a {@link jakarta.persistence.Basic} attribute</li> * <li>a {@link jakarta.persistence.Basic} attribute</li>
@ -60,24 +62,6 @@ public interface BasicValuedMapping extends ValueMapping, SqlExpressible {
MutableCacheKeyBuilder cacheKey, MutableCacheKeyBuilder cacheKey,
Object value, Object value,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
if ( value == null ) { addBasicValueToCacheKey( cacheKey, value, getJdbcMapping(), session );
return;
}
final JdbcMapping jdbcMapping = getJdbcMapping();
final BasicValueConverter converter = jdbcMapping.getValueConverter();
final Serializable disassemble;
final int hashCode;
if ( converter == null ) {
disassemble = jdbcMapping.getJavaTypeDescriptor().getMutabilityPlan().disassemble( value, session );
hashCode = ( (JavaType) jdbcMapping.getMappedJavaType() ).extractHashCode( value );
}
else {
final Object relationalValue = converter.toRelationalValue( value );
final JavaType relationalJavaType = converter.getRelationalJavaType();
disassemble = relationalJavaType.getMutabilityPlan().disassemble( relationalValue, session );
hashCode = relationalJavaType.extractHashCode( relationalValue );
}
cacheKey.addValue( disassemble );
cacheKey.addHashCode( hashCode );
} }
} }

View File

@ -20,8 +20,12 @@ import org.hibernate.QueryException;
import org.hibernate.QueryParameterException; import org.hibernate.QueryParameterException;
import org.hibernate.cache.MutableCacheKeyBuilder; import org.hibernate.cache.MutableCacheKeyBuilder;
import org.hibernate.cache.spi.QueryKey; import org.hibernate.cache.spi.QueryKey;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.FilterImpl;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingModelExpressible; import org.hibernate.metamodel.mapping.MappingModelExpressible;
import org.hibernate.query.BindableType; import org.hibernate.query.BindableType;
import org.hibernate.query.QueryParameter; import org.hibernate.query.QueryParameter;
@ -33,6 +37,8 @@ import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.JavaTypedExpressible; import org.hibernate.type.descriptor.java.JavaTypedExpressible;
import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.engine.internal.CacheHelper.addBasicValueToCacheKey;
/** /**
* Manages the group of QueryParameterBinding for a particular query. * Manages the group of QueryParameterBinding for a particular query.
* *
@ -189,6 +195,17 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
mappingType.addToCacheKey( mutableCacheKey, bindValue, session ); mappingType.addToCacheKey( mutableCacheKey, bindValue, session );
} }
} ); } );
// Add any enabled filter parameter values to the cache key, sorting by filter name and parameter name
final LoadQueryInfluencers loadQueryInfluencers = session.getLoadQueryInfluencers();
loadQueryInfluencers.getEnabledFilterNames().stream().sorted().forEach( filterName -> {
final FilterImpl filter = (FilterImpl) loadQueryInfluencers.getEnabledFilter( filterName );
final FilterDefinition filterDefinition = filter.getFilterDefinition();
filterDefinition.getParameterNames().stream().sorted().forEach( paramName -> {
final Object paramValue = filter.getParameter( paramName );
final JdbcMapping jdbcMapping = filterDefinition.getParameterJdbcMapping( paramName );
addBasicValueToCacheKey( mutableCacheKey, paramValue, jdbcMapping, session );
} );
} );
// Finally, build the overall cache key // Finally, build the overall cache key
return mutableCacheKey.build(); return mutableCacheKey.build();
} }