From 7d9ffc6158e74dcb2fe0388985b400fe672b3688 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 15 Nov 2022 19:11:21 +0100 Subject: [PATCH] HHH-15728 Improve query cache key hash code to reduce collisions --- .../main/java/org/hibernate/cache/spi/QueryKey.java | 13 +++++-------- .../query/internal/QueryParameterBindingsImpl.java | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/QueryKey.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/QueryKey.java index 036a43b937..86f10ecb61 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/QueryKey.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/QueryKey.java @@ -105,15 +105,12 @@ public class QueryKey implements Serializable { private int generateHashCode() { int result = 13; result = 37 * result + sqlQueryString.hashCode(); - result = 37 * result + ( firstRow==null ? 0 : firstRow ); - result = 37 * result + ( maxRows==null ? 0 : maxRows ); + // Don't include the firstRow and maxRows in the hash as these values are rarely useful for query caching +// result = 37 * result + ( firstRow==null ? 0 : firstRow ); +// result = 37 * result + ( maxRows==null ? 0 : maxRows ); result = 37 * result + ( tenantIdentifier==null ? 0 : tenantIdentifier.hashCode() ); - // the collections are too complicated to incorporate into the hashcode. but they really - // aren't needed in the hashcode calculation - they are handled in `#equals` and the calculation - // without them is a good hashing code. - // - // todo (6.0) : maybe even just base it on `sqlQueryString`? - + result = 37 * result + parameterBindingsMemento.hashCode(); + result = 37 * result + ( enabledFilterNames == null ? 0 : enabledFilterNames.hashCode() ); return result; } 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 ca8fc5ec50..9d23805a29 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 @@ -186,7 +186,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { ? javaType.extractHashCode( bindValue ) : 0; - hashCode = 31 * hashCode + valueHashCode; + hashCode = 37 * hashCode + valueHashCode; } } else { @@ -199,7 +199,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { ? javaType.extractHashCode( bindValue ) : 0; - hashCode = 31 * hashCode + valueHashCode; + hashCode = 37 * hashCode + valueHashCode; } }