From 477984bdf7928eba34164a5d44080414fa9ca38f Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 21 Sep 2015 14:06:35 +0200 Subject: [PATCH 1/2] DATAMONGO-1291 - Find inherited @Document annotation. Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 67665a4998..3317bb162c 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-1291-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..d14e78b483 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-1291-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1291-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 28c91bc332..5501abf62d 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-1291-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index dfe146ff96..203577a808 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-1291-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 628b1ec633..c5b562dd29 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-1291-SNAPSHOT ../pom.xml From 9defa683995f96d0f7c2d1957f9e64320f845d06 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 22 Sep 2015 08:47:20 +0200 Subject: [PATCH 2/2] DATAMONGO-1291 - Read document metadata from composed annotations. We now use AnnotationUtils.findAnnotation for @Document lookup which enables the full power of Spring composable annotations. --- .../mapping/BasicMongoPersistentEntity.java | 3 +- .../BasicMongoPersistentEntityUnitTests.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java index 7c56c8c283..d182a9460a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java @@ -26,6 +26,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.expression.BeanFactoryAccessor; import org.springframework.context.expression.BeanFactoryResolver; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.annotation.Id; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.AssociationHandler; @@ -77,7 +78,7 @@ public BasicMongoPersistentEntity(TypeInformation typeInformation) { Class rawType = typeInformation.getType(); String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType); - Document document = rawType.getAnnotation(Document.class); + Document document = AnnotationUtils.findAnnotation(rawType, Document.class); this.expression = detectExpression(document); this.context = new StandardEvaluationContext(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java index de378304fb..a420c24d85 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java @@ -19,6 +19,11 @@ 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 org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -226,6 +231,18 @@ public void verifyShouldPassForNonLazyDBRefOnFinalClass() { verify(dbRefMock, times(1)).lazy(); } + /** + * @see DATAMONGO-1291 + */ + @Test + public void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation() { + + BasicMongoPersistentEntity entity = new BasicMongoPersistentEntity( + ClassTypeInformation.from(DocumentWithCustomAnnotation.class)); + + assertThat(entity.getCollection(), is("collection-1")); + } + @Document(collection = "contacts") class Contact { @@ -261,4 +278,15 @@ static class DocumentWithLanguage { static class AnyDocument { } + + @CustomDocumentAnnotation + static class DocumentWithCustomAnnotation { + + } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.TYPE }) + @Document(collection = "collection-1") + static @interface CustomDocumentAnnotation { + } }