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.
This commit is contained in:
Oliver Gierke
2018-02-13 12:12:45 +01:00
parent 99824a498e
commit ce237436d3
16 changed files with 60 additions and 41 deletions

View File

@@ -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 <jbrisbin@vmware.com>
* @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 "";
/**

View File

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

View File

@@ -206,7 +206,7 @@ public class QueryByExampleTests {
assertThat(result, hasItems(p1, p3));
}
@Document(collection = "dramatis-personae")
@Document("dramatis-personae")
@EqualsAndHashCode
@ToString
static class Person {

View File

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

View File

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

View File

@@ -515,7 +515,7 @@ public class MongoExampleMapperUnitTests {
FlatDocument flatDoc;
}
@Document(collection = "refDoc")
@Document("refDoc")
static class ReferenceDocument {
@Id String id;

View File

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

View File

@@ -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<RGT extends RecursiveGenericType<RGT>> {
@Id Long id;

View File

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

View File

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

View File

@@ -21,7 +21,7 @@ import org.springframework.data.mongodb.core.index.Indexed;
/**
* @author Jon Brisbin
*/
@Document(collection = "foobar")
@Document("foobar")
public class CustomCollectionWithIndex {
@Id

View File

@@ -23,7 +23,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document(collection = "geolocation")
@Document("geolocation")
public class GeoLocation {
@Id

View File

@@ -21,7 +21,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document(collection = "places")
@Document("places")
public class Location {
private ObjectId id;

View File

@@ -20,7 +20,7 @@ import org.springframework.data.annotation.Id;
/**
* @author Jon Brisbin
*/
@Document(collection = "person1")
@Document("person1")
public class PersonCustomCollection1 extends BasePerson {
@Id

View File

@@ -20,7 +20,7 @@ import org.springframework.data.annotation.Id;
/**
* @author Jon Brisbin
*/
@Document(collection = "person2")
@Document("person2")
public class PersonCustomCollection2 extends BasePerson {
@Id

View File

@@ -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<DynamicallyMapped, ObjectId> {