diff --git a/pom.xml b/pom.xml index 41512eaee8..7a3c48fdf1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1297-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index fd36debedd..bffd6765f4 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1297-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1297-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 28c91bc332..acf33266df 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1297-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index dfe146ff96..27b052e507 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1297-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 628b1ec633..40cf545d75 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1297-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index 8650cd9f89..b4db62fab8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -27,6 +27,8 @@ import org.slf4j.LoggerFactory; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.domain.Sort; +import org.springframework.data.mapping.Association; +import org.springframework.data.mapping.AssociationHandler; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mongodb.core.index.Index.Duplicates; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.TextIndexIncludeOptions.IncludeStrategy; @@ -123,6 +125,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty) } }); + indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root)); + return indexInformation; } @@ -168,6 +172,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty) } }); + indexInformation.addAll(resolveIndexesForDbrefs(path, collection, entity)); + return indexInformation; } @@ -438,6 +444,37 @@ private String pathAwareIndexName(String indexName, String dotPath, MongoPersist } + private List resolveIndexesForDbrefs(final String path, final String collection, + MongoPersistentEntity entity) { + + final List indexes = new ArrayList(0); + entity.doWithAssociations(new AssociationHandler() { + + @Override + public void doWithAssociation(Association association) { + + MongoPersistentProperty property = association.getInverse(); + + String propertyDotPath = (StringUtils.hasText(path) ? path + "." : "") + property.getFieldName(); + + if (property.isAnnotationPresent(GeoSpatialIndexed.class) || property.isAnnotationPresent(TextIndexed.class)) { + throw new IllegalArgumentException(String.format( + "Cannot create geospatial-/text- index on DBRef in collection '%s' for path '%s'.", collection, + propertyDotPath)); + } + + IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty(propertyDotPath, + collection, property); + + if (indexDefinitionHolder != null) { + indexes.add(indexDefinitionHolder); + } + } + }); + + return indexes; + } + /** * {@link CycleGuard} holds information about properties and the paths for accessing those. This information is used * to detect potential cycles within the references. 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 92b14be834..3a0fb5727e 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 @@ -26,6 +26,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; +import org.springframework.data.annotation.Id; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.core.DBObjectTestUtils; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder; @@ -148,6 +149,34 @@ public void resolvesIndexCollectionNameCorrectlyWhenDefinedInAnnotation() { assertThat(indexDefinitions.get(0).getCollection(), equalTo("CollectionOverride")); } + /** + * @see DATAMONGO-1297 + */ + @Test + public void resolvesIndexOnDbrefWhenDefined() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType(WithDbRef.class); + + assertThat(indexDefinitions, hasSize(1)); + assertThat(indexDefinitions.get(0).getCollection(), equalTo("withDbRef")); + assertThat(indexDefinitions.get(0).getIndexKeys(), equalTo(new BasicDBObjectBuilder().add("indexedDbRef", 1) + .get())); + } + + /** + * @see DATAMONGO-1297 + */ + @Test + public void resolvesIndexOnDbrefWhenDefinedOnNestedElement() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType(WrapperOfWithDbRef.class); + + assertThat(indexDefinitions, hasSize(1)); + assertThat(indexDefinitions.get(0).getCollection(), equalTo("wrapperOfWithDbRef")); + assertThat(indexDefinitions.get(0).getIndexKeys(), + equalTo(new BasicDBObjectBuilder().add("nested.indexedDbRef", 1).get())); + } + @Document(collection = "Zero") static class IndexOnLevelZero { @Indexed String indexedProperty; @@ -182,6 +211,24 @@ static class IndexOnLevelZeroWithExplicityNamedField { @Indexed @Field("customFieldName") String namedProperty; } + @Document + static class WrapperOfWithDbRef { + WithDbRef nested; + } + + @Document + static class WithDbRef { + + @Indexed// + @DBRef// + NoIndex indexedDbRef; + } + + @Document(collection = "no-index") + static class NoIndex { + @Id String id; + } + } /**