Skip to content

Commit ce23743

Browse files
committed
DATAMONGO-1873 - Added value() as alias for @document(collection = "…").
This allows to avoid having to use the explicit collection attribute in case the collection name is supposed to be customized.
1 parent 99824a4 commit ce23743

16 files changed

+60
-41
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Document.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import org.springframework.core.annotation.AliasFor;
2425
import org.springframework.data.annotation.Persistent;
2526

2627
/**
2728
* Identifies a domain object to be persisted to MongoDB.
2829
*
29-
* @author Jon Brisbin <jbrisbin@vmware.com>
30-
* @author Oliver Gierke ogierke@vmware.com
30+
* @author Jon Brisbin
31+
* @author Oliver Gierke
3132
* @author Christoph Strobl
3233
*/
3334
@Persistent
@@ -36,6 +37,24 @@
3637
@Target({ ElementType.TYPE })
3738
public @interface Document {
3839

40+
/**
41+
* The collection the document representing the entity is supposed to be stored in. If not configured, a default
42+
* collection name will be derived from the type's name. The attribute supports SpEL expressions to dynamically
43+
* calculate the collection to based on a per operation basis.
44+
*
45+
* @return the name of the collection to be used.
46+
*/
47+
@AliasFor("collection")
48+
String value() default "";
49+
50+
/**
51+
* The collection the document representing the entity is supposed to be stored in. If not configured, a default
52+
* collection name will be derived from the type's name. The attribute supports SpEL expressions to dynamically
53+
* calculate the collection to based on a per operation basis.
54+
*
55+
* @return the name of the collection to be used.
56+
*/
57+
@AliasFor("value")
3958
String collection() default "";
4059

4160
/**

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void shouldLoadRefIntoDifferentTypeCorrectly() {
8383
}
8484

8585
@Data
86-
@Document(collection = "cycle-with-different-type-root")
86+
@Document("cycle-with-different-type-root")
8787
static class RefCycleLoadingIntoDifferentTypeRoot {
8888

8989
@Id String id;
@@ -92,15 +92,15 @@ static class RefCycleLoadingIntoDifferentTypeRoot {
9292
}
9393

9494
@Data
95-
@Document(collection = "cycle-with-different-type-intermediate")
95+
@Document("cycle-with-different-type-intermediate")
9696
static class RefCycleLoadingIntoDifferentTypeIntermediate {
9797

9898
@Id String id;
9999
@DBRef RefCycleLoadingIntoDifferentTypeRootView refToRootView;
100100
}
101101

102102
@Data
103-
@Document(collection = "cycle-with-different-type-root")
103+
@Document("cycle-with-different-type-root")
104104
static class RefCycleLoadingIntoDifferentTypeRootView {
105105

106106
@Id String id;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void untypedExampleMatchesCorrectly() {
206206
assertThat(result, hasItems(p1, p3));
207207
}
208208

209-
@Document(collection = "dramatis-personae")
209+
@Document("dramatis-personae")
210210
@EqualsAndHashCode
211211
@ToString
212212
static class Person {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.data.annotation.PersistenceConstructor;
2323
import org.springframework.data.mongodb.core.mapping.Document;
2424

25-
@Document(collection = "newyork")
25+
@Document("newyork")
2626
public class Venue {
2727

2828
@Id private String id;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersIntegrationTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,23 @@ public void shouldReadBinaryType() {
102102
assertThat(template.findOne(query(where("id").is(wbd.id)), WithBinaryDataType.class)).isEqualTo(wbd);
103103
}
104104

105-
@Document(collection = COLLECTION)
105+
@Document(COLLECTION)
106106
static class Wrapper {
107107

108108
String id;
109109
UUID uuid;
110110
}
111111

112112
@Data
113-
@Document(collection = COLLECTION)
113+
@Document(COLLECTION)
114114
static class WithBinaryDataInArray {
115115

116116
@Id String id;
117117
byte[] data;
118118
}
119119

120120
@Data
121-
@Document(collection = COLLECTION)
121+
@Document(COLLECTION)
122122
static class WithBinaryDataType {
123123

124124
@Id String id;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ static class WrapperDocument {
515515
FlatDocument flatDoc;
516516
}
517517

518-
@Document(collection = "refDoc")
518+
@Document("refDoc")
519519
static class ReferenceDocument {
520520

521521
@Id String id;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void getPathItemShouldReturnNullWhenIdAndCollectionMatchAndAssignableToIn
8181
assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull();
8282
}
8383

84-
@Document(collection = "one")
84+
@Document("one")
8585
static class EntityOne {
8686

8787
}
@@ -94,7 +94,7 @@ interface ValueInterface {
9494

9595
}
9696

97-
@Document(collection = "three")
97+
@Document("three")
9898
static class EntityThree implements ValueInterface {
9999

100100
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void createIndexShouldThrowMeaningfulExceptionWhenIndexCreationFails() th
113113
new Index().named("stormlight").on("lastname", Direction.ASC).sparse(), "datamongo-1125"));
114114
}
115115

116-
@Document(collection = RECURSIVE_TYPE_COLLECTION_NAME)
116+
@Document(RECURSIVE_TYPE_COLLECTION_NAME)
117117
static abstract class RecursiveGenericType<RGT extends RecursiveGenericType<RGT>> {
118118

119119
@Id Long id;

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

+17-17
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,22 @@ public void resolveIndexDefinitionInCustomComposedAnnotatedFields() {
192192
isBsonObject().containing("sparse", true).containing("name", "different_name").notContaining("unique"));
193193
}
194194

195-
@Document(collection = "Zero")
195+
@Document("Zero")
196196
static class IndexOnLevelZero {
197197
@Indexed String indexedProperty;
198198
}
199199

200-
@Document(collection = "One")
200+
@Document("One")
201201
static class IndexOnLevelOne {
202202
IndexOnLevelZero zero;
203203
}
204204

205-
@Document(collection = "Two")
205+
@Document("Two")
206206
static class IndexOnLevelTwo {
207207
IndexOnLevelOne one;
208208
}
209209

210-
@Document(collection = "WithOptionsOnIndexedProperty")
210+
@Document("WithOptionsOnIndexedProperty")
211211
static class WithOptionsOnIndexedProperty {
212212

213213
@Indexed(background = true, direction = IndexDirection.DESCENDING,
@@ -239,7 +239,7 @@ static class WithDbRef {
239239
NoIndex indexedDbRef;
240240
}
241241

242-
@Document(collection = "no-index")
242+
@Document("no-index")
243243
static class NoIndex {
244244
@Id String id;
245245
}
@@ -358,30 +358,30 @@ public void resolvesComposedAnnotationIndexDefinitionOptionsCorrectly() {
358358
isBsonObject().containing("name", "my_geo_index_name").containing("bucketSize", 2.0));
359359
}
360360

361-
@Document(collection = "Zero")
361+
@Document("Zero")
362362
static class GeoSpatialIndexOnLevelZero {
363363
@GeoSpatialIndexed Point geoIndexedProperty;
364364
}
365365

366-
@Document(collection = "One")
366+
@Document("One")
367367
static class GeoSpatialIndexOnLevelOne {
368368
GeoSpatialIndexOnLevelZero zero;
369369
}
370370

371-
@Document(collection = "Two")
371+
@Document("Two")
372372
static class GeoSpatialIndexOnLevelTwo {
373373
GeoSpatialIndexOnLevelOne one;
374374
}
375375

376-
@Document(collection = "WithOptionsOnGeoSpatialIndexProperty")
376+
@Document("WithOptionsOnGeoSpatialIndexProperty")
377377
static class WithOptionsOnGeoSpatialIndexProperty {
378378

379379
@GeoSpatialIndexed(bits = 2, max = 100, min = 1,
380380
type = GeoSpatialIndexType.GEO_2D) //
381381
Point location;
382382
}
383383

384-
@Document(collection = "WithComposedAnnotation")
384+
@Document("WithComposedAnnotation")
385385
static class GeoSpatialIndexedDocumentWithComposedAnnotation {
386386

387387
@ComposedGeoSpatialIndexed //
@@ -505,19 +505,19 @@ public void singleCompoundIndexUsingComposedAnnotationsOnTypeResolvedCorrectly()
505505
.containing("unique", true).containing("background", true));
506506
}
507507

508-
@Document(collection = "CompoundIndexOnLevelOne")
508+
@Document("CompoundIndexOnLevelOne")
509509
static class CompoundIndexOnLevelOne {
510510

511511
CompoundIndexOnLevelZero zero;
512512
}
513513

514-
@Document(collection = "CompoundIndexOnLevelZeroWithEmptyIndexDef")
514+
@Document("CompoundIndexOnLevelZeroWithEmptyIndexDef")
515515
static class CompoundIndexOnLevelOneWithEmptyIndexDefinition {
516516

517517
CompoundIndexOnLevelZeroWithEmptyIndexDef zero;
518518
}
519519

520-
@Document(collection = "CompoundIndexOnLevelZero")
520+
@Document("CompoundIndexOnLevelZero")
521521
@CompoundIndexes({ @CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true,
522522
dropDups = true, sparse = true, unique = true) })
523523
static class CompoundIndexOnLevelZero {}
@@ -526,7 +526,7 @@ static class CompoundIndexOnLevelZero {}
526526
@CompoundIndex(name = "compound_index", background = true, dropDups = true, sparse = true, unique = true) })
527527
static class CompoundIndexOnLevelZeroWithEmptyIndexDef {}
528528

529-
@Document(collection = "CompoundIndexOnLevelZero")
529+
@Document("CompoundIndexOnLevelZero")
530530
@CompoundIndex(name = "compound_index", def = "{'foo': 1, 'bar': -1}", background = true, dropDups = true,
531531
sparse = true, unique = true)
532532
static class SingleCompoundIndex {}
@@ -535,14 +535,14 @@ static class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {
535535

536536
}
537537

538-
@Document(collection = "ComountIndexWithAutogeneratedName")
538+
@Document("ComountIndexWithAutogeneratedName")
539539
@CompoundIndexes({ @CompoundIndex(useGeneratedName = true, def = "{'foo': 1, 'bar': -1}", background = true,
540540
dropDups = true, sparse = true, unique = true) })
541541
static class ComountIndexWithAutogeneratedName {
542542

543543
}
544544

545-
@Document(collection = "WithComposedAnnotation")
545+
@Document("WithComposedAnnotation")
546546
@ComposedCompoundIndex
547547
static class CompoundIndexDocumentWithComposedAnnotation {
548548

@@ -1066,7 +1066,7 @@ static class NoCycleButIndenticallNamedPropertiesDeeplyNested {
10661066
@Indexed String foo;
10671067
}
10681068

1069-
@Document(collection = "rules")
1069+
@Document("rules")
10701070
static class NoCycleManyPathsToDeepValueObject {
10711071

10721072
private NoCycleLevel3 l3;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ public void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation()
221221
assertThat(entity.getCollection(), is("custom-collection"));
222222
}
223223

224-
@Document(collection = "contacts")
224+
@Document("contacts")
225225
class Contact {}
226226

227227
class Person extends Contact {}
228228

229-
@Document(collection = "#{35}")
229+
@Document("#{35}")
230230
class Company {}
231231

232-
@Document(collection = "#{myBean.collectionName}")
232+
@Document("#{myBean.collectionName}")
233233
class DynamicallyMapped {}
234234

235235
class CollectionProvider {
@@ -253,7 +253,7 @@ static class DocumentWithComposedAnnotation {}
253253

254254
@Retention(RetentionPolicy.RUNTIME)
255255
@Target({ ElementType.TYPE })
256-
@Document(collection = "collection-1")
256+
@Document("collection-1")
257257
static @interface CustomDocumentAnnotation {
258258
}
259259

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/CustomCollectionWithIndex.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @author Jon Brisbin
2323
*/
24-
@Document(collection = "foobar")
24+
@Document("foobar")
2525
public class CustomCollectionWithIndex {
2626

2727
@Id

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoLocation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* @author Jon Brisbin <jbrisbin@vmware.com>
2525
*/
26-
@Document(collection = "geolocation")
26+
@Document("geolocation")
2727
public class GeoLocation {
2828

2929
@Id

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/Location.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @author Jon Brisbin <jbrisbin@vmware.com>
2323
*/
24-
@Document(collection = "places")
24+
@Document("places")
2525
public class Location {
2626

2727
private ObjectId id;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection1.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* @author Jon Brisbin
2222
*/
23-
@Document(collection = "person1")
23+
@Document("person1")
2424
public class PersonCustomCollection1 extends BasePerson {
2525

2626
@Id

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomCollection2.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* @author Jon Brisbin
2222
*/
23-
@Document(collection = "person2")
23+
@Document("person2")
2424
public class PersonCustomCollection2 extends BasePerson {
2525

2626
@Id

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ private interface Repo extends MongoRepository<Person, Long> {
348348

349349
// DATAMONGO-1872
350350

351-
@org.springframework.data.mongodb.core.mapping.Document(collection = "#{T(java.lang.Math).random()}")
351+
@org.springframework.data.mongodb.core.mapping.Document("#{T(java.lang.Math).random()}")
352352
static class DynamicallyMapped {}
353353

354354
interface DynamicallyMappedRepository extends Repository<DynamicallyMapped, ObjectId> {

0 commit comments

Comments
 (0)