From d73270025d82194301a8b10fd95016e37a1f4516 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 28 Oct 2025 13:15:12 +0100 Subject: [PATCH] Deprecate background flag for creating indexes. And fix type name in tests. See #5074 Original pull request #5085 --- .../data/mongodb/core/index/CompoundIndex.java | 4 ++++ .../org/springframework/data/mongodb/core/index/Index.java | 7 ++++++- .../springframework/data/mongodb/core/index/Indexed.java | 4 ++++ .../index/MongoPersistentEntityIndexResolverUnitTests.java | 6 +++--- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java index 05cf13f66..2f618386a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java @@ -152,11 +152,15 @@ public @interface CompoundIndex { /** * If {@literal true} the index will be created in the background. + *

+ * NOTE: Since MongoDB 4.2 the background flag is ignored by the server if set. * * @return {@literal false} by default. * @see https://docs.mongodb.org/manual/core/indexes/#background-construction + * @deprecated since 5.0 for removal without replacement. */ + @Deprecated(since = "5.0", forRemoval = true) boolean background() default false; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java index 91195a40f..c6f83e2e7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java @@ -94,10 +94,15 @@ public class Index implements IndexDefinition { /** * Build the index in background (non blocking). + *

+ * NOTE: Since MongoDB 4.2 the background flag is ignored by the server if set. * * @return this. * @since 1.5 - */@Contract("-> this") + * @deprecated since 5.0 for removal without replacement. + */ + @Deprecated(since = "5.0", forRemoval = true) + @Contract("-> this") public Index background() { this.background = true; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java index 0866556c1..de89420c8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java @@ -123,11 +123,15 @@ public @interface Indexed { /** * If {@literal true} the index will be created in the background. + *

+ * NOTE: Since MongoDB 4.2 the background flag is ignored by the server if set. * * @return {@literal false} by default. + * @deprecated since 5.0 for removal without replacement. * @see https://docs.mongodb.org/manual/core/indexes/#background-construction */ + @Deprecated(since = "5.0", forRemoval = true) boolean background() default false; /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index ba5446194..6b1ade323 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -670,7 +670,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { public void compoundIndexDoesNotSpecifyNameWhenUsingGenerateName() { List indexDefinitions = prepareMappingContextAndResolveIndexForType( - ComountIndexWithAutogeneratedName.class); + CompoundIndexWithAutogeneratedName.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); assertThat(indexDefinition.getIndexOptions()) @@ -835,10 +835,10 @@ public class MongoPersistentEntityIndexResolverUnitTests { class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {} - @Document("ComountIndexWithAutogeneratedName") + @Document("CompoundIndexWithAutogeneratedName") @CompoundIndexes({ @CompoundIndex(useGeneratedName = true, def = "{'foo': 1, 'bar': -1}", background = true, sparse = true, unique = true) }) - class ComountIndexWithAutogeneratedName {} + class CompoundIndexWithAutogeneratedName {} @Document("WithComposedAnnotation") @ComposedCompoundIndex