diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java index 6bbdb3ceba..d580988a97 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java @@ -37,6 +37,7 @@ * @author Mark Paluch * @author Owen Q * @author Kirill Egorov + * @author GaEun Kim */ public class Field { @@ -286,7 +287,7 @@ public boolean equals(@Nullable Object o) { @Override public int hashCode() { - int result = ObjectUtils.nullSafeHashCode(criteria); + int result = ObjectUtils.nullSafeHashCode(criteria.toString()); result = 31 * result + ObjectUtils.nullSafeHashCode(slices); result = 31 * result + ObjectUtils.nullSafeHashCode(elemMatches); result = 31 * result + ObjectUtils.nullSafeHashCode(positionKey); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java index 6dba758d87..96f51bd624 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java @@ -28,6 +28,7 @@ * @author Owen Q * @author Mark Paluch * @author Kirill Egorov + * @author GaEun Kim */ class FieldUnitTests { @@ -85,4 +86,18 @@ void overriddenExclusionMethodsCreateEqualFields() { assertThat(left).isEqualTo(right); } + + @Test + void assertDifferentHashCodesForExcludeAndIncludeQueries() { + + Query queryWithExclude = new Query(); + queryWithExclude.fields().exclude("key1"); + queryWithExclude.fields().exclude("key2"); + + Query queryWithInclude = new Query(); + queryWithInclude.fields().include("key1"); + queryWithInclude.fields().include("key2"); + + assertThat(queryWithExclude.hashCode()).isNotEqualTo(queryWithInclude.hashCode()); + } }