HHH-15728 Improve query cache key hash code to reduce collisions

This commit is contained in:
Christian Beikov 2022-11-15 19:11:21 +01:00
parent 68324b9297
commit 7d9ffc6158
2 changed files with 7 additions and 10 deletions

View File

@ -105,15 +105,12 @@ public class QueryKey implements Serializable {
private int generateHashCode() { private int generateHashCode() {
int result = 13; int result = 13;
result = 37 * result + sqlQueryString.hashCode(); result = 37 * result + sqlQueryString.hashCode();
result = 37 * result + ( firstRow==null ? 0 : firstRow ); // Don't include the firstRow and maxRows in the hash as these values are rarely useful for query caching
result = 37 * result + ( maxRows==null ? 0 : maxRows ); // result = 37 * result + ( firstRow==null ? 0 : firstRow );
// result = 37 * result + ( maxRows==null ? 0 : maxRows );
result = 37 * result + ( tenantIdentifier==null ? 0 : tenantIdentifier.hashCode() ); result = 37 * result + ( tenantIdentifier==null ? 0 : tenantIdentifier.hashCode() );
// the collections are too complicated to incorporate into the hashcode. but they really result = 37 * result + parameterBindingsMemento.hashCode();
// aren't needed in the hashcode calculation - they are handled in `#equals` and the calculation result = 37 * result + ( enabledFilterNames == null ? 0 : enabledFilterNames.hashCode() );
// without them is a good hashing code.
//
// todo (6.0) : maybe even just base it on `sqlQueryString`?
return result; return result;
} }

View File

@ -186,7 +186,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
? javaType.extractHashCode( bindValue ) ? javaType.extractHashCode( bindValue )
: 0; : 0;
hashCode = 31 * hashCode + valueHashCode; hashCode = 37 * hashCode + valueHashCode;
} }
} }
else { else {
@ -199,7 +199,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
? javaType.extractHashCode( bindValue ) ? javaType.extractHashCode( bindValue )
: 0; : 0;
hashCode = 31 * hashCode + valueHashCode; hashCode = 37 * hashCode + valueHashCode;
} }
} }