Skip to content

Commit b5ea0ec

Browse files
yacotaodrotbohm
authored andcommitted
DATAMONGO-1163 - Allow usage of @indexed as meta-annotation.
@indexed can now be used as meta-annotation so that user annotations can be annotated with it and the index creation facilities still pick up the configuration information. Original pull request: spring-projects#325.
1 parent 87865b9 commit b5ea0ec

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
* @author Johno Crawford
3030
* @author Thomas Darimont
3131
* @author Christoph Strobl
32+
* @author Jordi Llach
3233
*/
33-
@Target(ElementType.FIELD)
34+
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD})
3435
@Retention(RetentionPolicy.RUNTIME)
3536
public @interface Indexed {
3637

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java

+22-13
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
*/
1616
package org.springframework.data.mongodb.core.index;
1717

18+
import com.mongodb.DBCollection;
19+
import com.mongodb.DBObject;
20+
import com.mongodb.MongoException;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
1825
import static org.hamcrest.CoreMatchers.*;
19-
import static org.junit.Assert.*;
20-
2126
import org.junit.After;
27+
import static org.junit.Assert.*;
2228
import org.junit.Test;
2329
import org.junit.runner.RunWith;
2430
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,10 +36,6 @@
3036
import org.springframework.test.context.ContextConfiguration;
3137
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3238

33-
import com.mongodb.DBCollection;
34-
import com.mongodb.DBObject;
35-
import com.mongodb.MongoException;
36-
3739
/**
3840
* Integration tests for index handling.
3941
*
@@ -45,26 +47,33 @@
4547
public class IndexingIntegrationTests {
4648

4749
@Autowired MongoOperations operations;
48-
49-
@After
50+
51+
@After
5052
public void tearDown() {
51-
operations.dropCollection(IndexedPerson.class);
53+
operations.dropCollection(IndexedPerson.class);
5254
}
53-
54-
/**
55+
56+
/**
5557
* @see DATADOC-237
58+
* @see DATAMONGO-1163
5659
*/
5760
@Test
5861
public void createsIndexWithFieldName() {
59-
6062
operations.save(new IndexedPerson());
6163
assertThat(hasIndex("_firstname", IndexedPerson.class), is(true));
64+
assertThat(hasIndex("_lastname", IndexedPerson.class), is(true));
6265
}
6366

67+
@Target({ElementType.FIELD})
68+
@Retention(RetentionPolicy.RUNTIME)
69+
@Indexed
70+
@interface IndexedFieldAnnotation {}
71+
6472
@Document
6573
class IndexedPerson {
6674

6775
@Field("_firstname") @Indexed String firstname;
76+
@Field("_lastname") @IndexedFieldAnnotation String lastname;
6877
}
6978

7079
/**
@@ -87,4 +96,4 @@ public Boolean doInCollection(DBCollection collection) throws MongoException, Da
8796
}
8897
});
8998
}
90-
}
99+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import static org.junit.Assert.*;
2020
import static org.mockito.Mockito.*;
2121

22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
2226
import java.util.Collections;
2327
import java.util.List;
2428

@@ -177,6 +181,19 @@ public void resolvesIndexOnDbrefWhenDefinedOnNestedElement() {
177181
equalTo(new BasicDBObjectBuilder().add("nested.indexedDbRef", 1).get()));
178182
}
179183

184+
/**
185+
* @see DATAMONGO-1163
186+
*/
187+
@Test
188+
public void resolveIndexDefinitionInMetaAnnotatedFields() {
189+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(
190+
IndexOnMetaAnnotatedField.class);
191+
assertThat(indexDefinitions, hasSize(1));
192+
assertThat(indexDefinitions.get(0).getCollection(), equalTo("indexOnMetaAnnotatedField"));
193+
assertThat(indexDefinitions.get(0).getIndexOptions(),
194+
equalTo(new BasicDBObjectBuilder().add("name", "_name").get()));
195+
}
196+
180197
@Document(collection = "Zero")
181198
static class IndexOnLevelZero {
182199
@Indexed String indexedProperty;
@@ -231,6 +248,18 @@ static class NoIndex {
231248

232249
}
233250

251+
@Target({ ElementType.FIELD })
252+
@Retention(RetentionPolicy.RUNTIME)
253+
@Indexed
254+
@interface IndexedFieldAnnotation {
255+
256+
}
257+
258+
@Document
259+
static class IndexOnMetaAnnotatedField {
260+
@Field("_name") @IndexedFieldAnnotation String lastname;
261+
}
262+
234263
/**
235264
* Test resolution of {@link GeoSpatialIndexed}.
236265
*

0 commit comments

Comments
 (0)