From ce237436d3f08608c785482c11940ddf6244681b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 13 Feb 2018 12:12:45 +0100 Subject: [PATCH] =?UTF-8?q?DATAMONGO-1873=20-=20Added=20value()=20as=20ali?= =?UTF-8?q?as=20for=20@Document(collection=20=3D=20"=E2=80=A6").?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows to avoid having to use the explicit collection attribute in case the collection name is supposed to be customized. --- .../data/mongodb/core/mapping/Document.java | 23 +++++++++++-- .../mongodb/core/MongoTemplateDbRefTests.java | 6 ++-- .../mongodb/core/QueryByExampleTests.java | 2 +- .../data/mongodb/core/Venue.java | 2 +- .../MongoConvertersIntegrationTests.java | 6 ++-- .../convert/MongoExampleMapperUnitTests.java | 2 +- .../core/convert/ObjectPathUnitTests.java | 4 +-- ...entEntityIndexCreatorIntegrationTests.java | 2 +- ...ersistentEntityIndexResolverUnitTests.java | 34 +++++++++---------- .../BasicMongoPersistentEntityUnitTests.java | 8 ++--- .../mapping/CustomCollectionWithIndex.java | 2 +- .../mongodb/core/mapping/GeoLocation.java | 2 +- .../data/mongodb/core/mapping/Location.java | 2 +- .../core/mapping/PersonCustomCollection1.java | 2 +- .../core/mapping/PersonCustomCollection2.java | 2 +- .../query/AbstractMongoQueryUnitTests.java | 2 +- 16 files changed, 60 insertions(+), 41 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Document.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Document.java index 0cc67a1b9..4d31b1e18 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Document.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Document.java @@ -21,13 +21,14 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.core.annotation.AliasFor; import org.springframework.data.annotation.Persistent; /** * Identifies a domain object to be persisted to MongoDB. * - * @author Jon Brisbin - * @author Oliver Gierke ogierke@vmware.com + * @author Jon Brisbin + * @author Oliver Gierke * @author Christoph Strobl */ @Persistent @@ -36,6 +37,24 @@ import org.springframework.data.annotation.Persistent; @Target({ ElementType.TYPE }) public @interface Document { + /** + * The collection the document representing the entity is supposed to be stored in. If not configured, a default + * collection name will be derived from the type's name. The attribute supports SpEL expressions to dynamically + * calculate the collection to based on a per operation basis. + * + * @return the name of the collection to be used. + */ + @AliasFor("collection") + String value() default ""; + + /** + * The collection the document representing the entity is supposed to be stored in. If not configured, a default + * collection name will be derived from the type's name. The attribute supports SpEL expressions to dynamically + * calculate the collection to based on a per operation basis. + * + * @return the name of the collection to be used. + */ + @AliasFor("value") String collection() default ""; /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java index ee082dcfc..382c3ae30 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java @@ -83,7 +83,7 @@ public class MongoTemplateDbRefTests { } @Data - @Document(collection = "cycle-with-different-type-root") + @Document("cycle-with-different-type-root") static class RefCycleLoadingIntoDifferentTypeRoot { @Id String id; @@ -92,7 +92,7 @@ public class MongoTemplateDbRefTests { } @Data - @Document(collection = "cycle-with-different-type-intermediate") + @Document("cycle-with-different-type-intermediate") static class RefCycleLoadingIntoDifferentTypeIntermediate { @Id String id; @@ -100,7 +100,7 @@ public class MongoTemplateDbRefTests { } @Data - @Document(collection = "cycle-with-different-type-root") + @Document("cycle-with-different-type-root") static class RefCycleLoadingIntoDifferentTypeRootView { @Id String id; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java index 00fa5db78..41dd5765f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java @@ -206,7 +206,7 @@ public class QueryByExampleTests { assertThat(result, hasItems(p1, p3)); } - @Document(collection = "dramatis-personae") + @Document("dramatis-personae") @EqualsAndHashCode @ToString static class Person { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/Venue.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/Venue.java index 80d79dbe3..c433bf301 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/Venue.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/Venue.java @@ -22,7 +22,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.mongodb.core.mapping.Document; -@Document(collection = "newyork") +@Document("newyork") public class Venue { @Id private String id; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersIntegrationTests.java index da640e0f7..0f1f17465 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersIntegrationTests.java @@ -102,7 +102,7 @@ public class MongoConvertersIntegrationTests { assertThat(template.findOne(query(where("id").is(wbd.id)), WithBinaryDataType.class)).isEqualTo(wbd); } - @Document(collection = COLLECTION) + @Document(COLLECTION) static class Wrapper { String id; @@ -110,7 +110,7 @@ public class MongoConvertersIntegrationTests { } @Data - @Document(collection = COLLECTION) + @Document(COLLECTION) static class WithBinaryDataInArray { @Id String id; @@ -118,7 +118,7 @@ public class MongoConvertersIntegrationTests { } @Data - @Document(collection = COLLECTION) + @Document(COLLECTION) static class WithBinaryDataType { @Id String id; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java index 6f43220c9..06cf669fb 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java @@ -515,7 +515,7 @@ public class MongoExampleMapperUnitTests { FlatDocument flatDoc; } - @Document(collection = "refDoc") + @Document("refDoc") static class ReferenceDocument { @Id String id; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java index cfaab334b..7f5a2068d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java @@ -81,7 +81,7 @@ public class ObjectPathUnitTests { assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull(); } - @Document(collection = "one") + @Document("one") static class EntityOne { } @@ -94,7 +94,7 @@ public class ObjectPathUnitTests { } - @Document(collection = "three") + @Document("three") static class EntityThree implements ValueInterface { } 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 5baf269ce..da61dd7ff 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 @@ -113,7 +113,7 @@ public class MongoPersistentEntityIndexCreatorIntegrationTests { new Index().named("stormlight").on("lastname", Direction.ASC).sparse(), "datamongo-1125")); } - @Document(collection = RECURSIVE_TYPE_COLLECTION_NAME) + @Document(RECURSIVE_TYPE_COLLECTION_NAME) static abstract class RecursiveGenericType> { @Id Long id; 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 7c1c60421..5443db2e0 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 @@ -192,22 +192,22 @@ public class MongoPersistentEntityIndexResolverUnitTests { isBsonObject().containing("sparse", true).containing("name", "different_name").notContaining("unique")); } - @Document(collection = "Zero") + @Document("Zero") static class IndexOnLevelZero { @Indexed String indexedProperty; } - @Document(collection = "One") + @Document("One") static class IndexOnLevelOne { IndexOnLevelZero zero; } - @Document(collection = "Two") + @Document("Two") static class IndexOnLevelTwo { IndexOnLevelOne one; } - @Document(collection = "WithOptionsOnIndexedProperty") + @Document("WithOptionsOnIndexedProperty") static class WithOptionsOnIndexedProperty { @Indexed(background = true, direction = IndexDirection.DESCENDING, @@ -239,7 +239,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { NoIndex indexedDbRef; } - @Document(collection = "no-index") + @Document("no-index") static class NoIndex { @Id String id; } @@ -358,22 +358,22 @@ public class MongoPersistentEntityIndexResolverUnitTests { isBsonObject().containing("name", "my_geo_index_name").containing("bucketSize", 2.0)); } - @Document(collection = "Zero") + @Document("Zero") static class GeoSpatialIndexOnLevelZero { @GeoSpatialIndexed Point geoIndexedProperty; } - @Document(collection = "One") + @Document("One") static class GeoSpatialIndexOnLevelOne { GeoSpatialIndexOnLevelZero zero; } - @Document(collection = "Two") + @Document("Two") static class GeoSpatialIndexOnLevelTwo { GeoSpatialIndexOnLevelOne one; } - @Document(collection = "WithOptionsOnGeoSpatialIndexProperty") + @Document("WithOptionsOnGeoSpatialIndexProperty") static class WithOptionsOnGeoSpatialIndexProperty { @GeoSpatialIndexed(bits = 2, max = 100, min = 1, @@ -381,7 +381,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { Point location; } - @Document(collection = "WithComposedAnnotation") + @Document("WithComposedAnnotation") static class GeoSpatialIndexedDocumentWithComposedAnnotation { @ComposedGeoSpatialIndexed // @@ -505,19 +505,19 @@ public class MongoPersistentEntityIndexResolverUnitTests { .containing("unique", true).containing("background", true)); } - @Document(collection = "CompoundIndexOnLevelOne") + @Document("CompoundIndexOnLevelOne") static class CompoundIndexOnLevelOne { CompoundIndexOnLevelZero zero; } - @Document(collection = "CompoundIndexOnLevelZeroWithEmptyIndexDef") + @Document("CompoundIndexOnLevelZeroWithEmptyIndexDef") static class CompoundIndexOnLevelOneWithEmptyIndexDefinition { CompoundIndexOnLevelZeroWithEmptyIndexDef zero; } - @Document(collection = "CompoundIndexOnLevelZero") + @Document("CompoundIndexOnLevelZero") @CompoundIndexes({ @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, dropDups = true, sparse = true, unique = true) }) static class CompoundIndexOnLevelZero {} @@ -526,7 +526,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { @CompoundIndex(name = "compound_index", background = true, dropDups = true, sparse = true, unique = true) }) static class CompoundIndexOnLevelZeroWithEmptyIndexDef {} - @Document(collection = "CompoundIndexOnLevelZero") + @Document("CompoundIndexOnLevelZero") @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, dropDups = true, sparse = true, unique = true) static class SingleCompoundIndex {} @@ -535,14 +535,14 @@ public class MongoPersistentEntityIndexResolverUnitTests { } - @Document(collection = "ComountIndexWithAutogeneratedName") + @Document("ComountIndexWithAutogeneratedName") @CompoundIndexes({ @CompoundIndex(useGeneratedName = true, def = "{'foo': 1, 'bar': -1}", background = true, dropDups = true, sparse = true, unique = true) }) static class ComountIndexWithAutogeneratedName { } - @Document(collection = "WithComposedAnnotation") + @Document("WithComposedAnnotation") @ComposedCompoundIndex static class CompoundIndexDocumentWithComposedAnnotation { @@ -1066,7 +1066,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Indexed String foo; } - @Document(collection = "rules") + @Document("rules") static class NoCycleManyPathsToDeepValueObject { private NoCycleLevel3 l3; 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 0ce70da46..af744e4a9 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 @@ -221,15 +221,15 @@ public class BasicMongoPersistentEntityUnitTests { assertThat(entity.getCollection(), is("custom-collection")); } - @Document(collection = "contacts") + @Document("contacts") class Contact {} class Person extends Contact {} - @Document(collection = "#{35}") + @Document("#{35}") class Company {} - @Document(collection = "#{myBean.collectionName}") + @Document("#{myBean.collectionName}") class DynamicallyMapped {} class CollectionProvider { @@ -253,7 +253,7 @@ public class BasicMongoPersistentEntityUnitTests { @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) - @Document(collection = "collection-1") + @Document("collection-1") static @interface CustomDocumentAnnotation { } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CustomCollectionWithIndex.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CustomCollectionWithIndex.java index d6969290d..35f7925e6 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CustomCollectionWithIndex.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CustomCollectionWithIndex.java @@ -21,7 +21,7 @@ import org.springframework.data.mongodb.core.index.Indexed; /** * @author Jon Brisbin */ -@Document(collection = "foobar") +@Document("foobar") public class CustomCollectionWithIndex { @Id diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoLocation.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoLocation.java index c0a9aa163..2296f068e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoLocation.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoLocation.java @@ -23,7 +23,7 @@ import org.springframework.data.mongodb.core.mapping.Document; /** * @author Jon Brisbin */ -@Document(collection = "geolocation") +@Document("geolocation") public class GeoLocation { @Id diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/Location.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/Location.java index 6933ed378..0e9832be1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/Location.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/Location.java @@ -21,7 +21,7 @@ import org.springframework.data.mongodb.core.mapping.Document; /** * @author Jon Brisbin */ -@Document(collection = "places") +@Document("places") public class Location { private ObjectId id; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection1.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection1.java index 2de75baa4..96cfff421 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection1.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection1.java @@ -20,7 +20,7 @@ import org.springframework.data.annotation.Id; /** * @author Jon Brisbin */ -@Document(collection = "person1") +@Document("person1") public class PersonCustomCollection1 extends BasePerson { @Id diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection2.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection2.java index 9c77f804a..091d2dca7 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection2.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection2.java @@ -20,7 +20,7 @@ import org.springframework.data.annotation.Id; /** * @author Jon Brisbin */ -@Document(collection = "person2") +@Document("person2") public class PersonCustomCollection2 extends BasePerson { @Id diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java index e1bf60106..e53c4b102 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java @@ -348,7 +348,7 @@ public class AbstractMongoQueryUnitTests { // DATAMONGO-1872 - @org.springframework.data.mongodb.core.mapping.Document(collection = "#{T(java.lang.Math).random()}") + @org.springframework.data.mongodb.core.mapping.Document("#{T(java.lang.Math).random()}") static class DynamicallyMapped {} interface DynamicallyMappedRepository extends Repository {