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 31eb1ef17..a32262242 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 @@ -29,8 +29,9 @@ import java.lang.annotation.Target; * @author Johno Crawford * @author Thomas Darimont * @author Christoph Strobl + * @author Jordi Llach */ -@Target(ElementType.FIELD) +@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Indexed { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java index 5ac8c4c57..7444ed79f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java @@ -15,10 +15,16 @@ */ package org.springframework.data.mongodb.core.index; +import com.mongodb.DBCollection; +import com.mongodb.DBObject; +import com.mongodb.MongoException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - import org.junit.After; +import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -30,10 +36,6 @@ import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; -import com.mongodb.MongoException; - /** * Integration tests for index handling. * @@ -45,26 +47,33 @@ import com.mongodb.MongoException; public class IndexingIntegrationTests { @Autowired MongoOperations operations; - - @After + + @After public void tearDown() { - operations.dropCollection(IndexedPerson.class); + operations.dropCollection(IndexedPerson.class); } - - /** + + /** * @see DATADOC-237 + * @see DATAMONGO-1163 */ @Test public void createsIndexWithFieldName() { - operations.save(new IndexedPerson()); assertThat(hasIndex("_firstname", IndexedPerson.class), is(true)); + assertThat(hasIndex("_lastname", IndexedPerson.class), is(true)); } + @Target({ElementType.FIELD}) + @Retention(RetentionPolicy.RUNTIME) + @Indexed + @interface IndexedFieldAnnotation {} + @Document class IndexedPerson { @Field("_firstname") @Indexed String firstname; + @Field("_lastname") @IndexedFieldAnnotation String lastname; } /** @@ -87,4 +96,4 @@ public class IndexingIntegrationTests { } }); } -} +} \ No newline at end of file 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 3a0fb5727..53273fd1a 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 @@ -19,6 +19,10 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.util.Collections; import java.util.List; @@ -177,6 +181,19 @@ public class MongoPersistentEntityIndexResolverUnitTests { equalTo(new BasicDBObjectBuilder().add("nested.indexedDbRef", 1).get())); } + /** + * @see DATAMONGO-1163 + */ + @Test + public void resolveIndexDefinitionInMetaAnnotatedFields() { + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + IndexOnMetaAnnotatedField.class); + assertThat(indexDefinitions, hasSize(1)); + assertThat(indexDefinitions.get(0).getCollection(), equalTo("indexOnMetaAnnotatedField")); + assertThat(indexDefinitions.get(0).getIndexOptions(), + equalTo(new BasicDBObjectBuilder().add("name", "_name").get())); + } + @Document(collection = "Zero") static class IndexOnLevelZero { @Indexed String indexedProperty; @@ -231,6 +248,18 @@ public class MongoPersistentEntityIndexResolverUnitTests { } + @Target({ ElementType.FIELD }) + @Retention(RetentionPolicy.RUNTIME) + @Indexed + @interface IndexedFieldAnnotation { + + } + + @Document + static class IndexOnMetaAnnotatedField { + @Field("_name") @IndexedFieldAnnotation String lastname; + } + /** * Test resolution of {@link GeoSpatialIndexed}. *