diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexResolver.java index b444d1da8..132616ef7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,22 +16,24 @@ package org.springframework.data.mongodb.core.index; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder; +import org.springframework.data.util.TypeInformation; /** * {@link IndexResolver} finds those {@link IndexDefinition}s to be created for a given class. * * @author Christoph Strobl + * @author Thomas Darimont * @since 1.5 */ interface IndexResolver { /** - * Find and create {@link IndexDefinition}s for properties of given {@code type}. {@link IndexDefinition}s are created + * Find and create {@link IndexDefinition}s for properties of given {@link TypeInformation}. {@link IndexDefinition}s are created * for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}. * - * @param type + * @param typeInformation * @return Empty {@link Iterable} in case no {@link IndexDefinition} could be resolved for type. */ - Iterable resolveIndexForClass(Class type); + Iterable resolveIndexFor(TypeInformation typeInformation); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java index 945959ca6..1b0fbfb61 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java @@ -123,7 +123,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener entity) { if (entity.findAnnotation(Document.class) != null) { - for (IndexDefinitionHolder indexToCreate : indexResolver.resolveIndexForClass(entity.getType())) { + for (IndexDefinitionHolder indexToCreate : indexResolver.resolveIndexFor(entity.getTypeInformation())) { createIndex(indexToCreate); } } 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 0b8a6de34..fd7ae50fb 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 @@ -36,6 +36,7 @@ import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; +import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -51,6 +52,7 @@ import com.mongodb.util.JSON; * scanning related annotations. * * @author Christoph Strobl + * @author Thomas Darimont * @since 1.5 */ public class MongoPersistentEntityIndexResolver implements IndexResolver { @@ -70,13 +72,12 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { this.mappingContext = mappingContext; } - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.core.index.IndexResolver#resolveIndexForClass(java.lang.Class) + /* (non-Javadoc) + * @see org.springframework.data.mongodb.core.index.IndexResolver#resolveIndexForClass(org.springframework.data.util.TypeInformation) */ @Override - public List resolveIndexForClass(Class type) { - return resolveIndexForEntity(mappingContext.getPersistentEntity(type)); + public Iterable resolveIndexFor(TypeInformation typeInformation) { + return resolveIndexForEntity(mappingContext.getPersistentEntity(typeInformation)); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/ConcreteCustomer.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/ConcreteCustomer.java new file mode 100644 index 000000000..1f123b192 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/ConcreteCustomer.java @@ -0,0 +1,26 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.index; + +/** + * @author Thomas Darimont + */ +public class ConcreteCustomer extends GenericCustomer { + + public ConcreteCustomer(Long id, String firstName, ConcreteCustomer referrer) { + super(id, firstName, referrer); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/GenericCustomer.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/GenericCustomer.java new file mode 100644 index 000000000..374762401 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/GenericCustomer.java @@ -0,0 +1,49 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.index; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +/** + * @author Thomas Darimont + * + * @param + */ +@Document +public abstract class GenericCustomer> { + + @Id private Long id; + + @org.springframework.data.mongodb.core.mapping.DBRef private RC referrer; + + @Indexed private String firstName; + + public GenericCustomer(Long id, String firstName, RC referrer) { + this.firstName = firstName; + this.id = id; + this.referrer = referrer; + } + + public RC getReferrer() { + return referrer; + } + + @Override + public String toString() { + return "GenericCustomer{" + "id=" + id + ", referrer=" + referrer + ", firstName='" + firstName + '\'' + '}'; + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorIntegrationTests.java index e7371fdfd..34d7810a6 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import java.util.List; import org.hamcrest.Matchers; import org.junit.After; +import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; @@ -65,4 +66,16 @@ public class MongoPersistentEntityIndexCreatorIntegrationTests { indexInfo = templateTwo.indexOps("sampleEntity").getIndexInfo(); assertThat(indexInfo, hasSize(0)); } + + /** + * @see DATAMONGO-1202 + */ + @Test + public void shouldHonorIndexedPropertiesWithRecursiveMappings() { + + List indexInfo = templateOne.indexOps(ConcreteCustomer.class).getIndexInfo(); + + assertThat(indexInfo, hasSize(greaterThan(0))); + assertThat(indexInfo, Matchers. hasItem(hasProperty("name", is("firstName")))); + } }