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: #325.
This commit is contained in:
Jordi Llach
2015-11-16 19:48:33 +01:00
committed by Oliver Gierke
parent 87865b9761
commit b5ea0eccd2
3 changed files with 53 additions and 14 deletions

View File

@@ -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 {

View File

@@ -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 {
}
});
}
}
}

View File

@@ -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<IndexDefinitionHolder> 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}.
*