Skip to content

Commit

Permalink
DATAMONGO-1873 - Added value() as alias for @document(collection = "…").
Browse files Browse the repository at this point in the history
This allows to avoid having to use the explicit collection attribute in case the collection name is supposed to be customized.
  • Loading branch information
odrotbohm committed Feb 13, 2018
1 parent 99824a4 commit ce23743
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 41 deletions.
Expand Up @@ -21,13 +21,14 @@
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
Expand All @@ -36,6 +37,24 @@
@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 "";

/**
Expand Down
Expand Up @@ -83,7 +83,7 @@ public void shouldLoadRefIntoDifferentTypeCorrectly() {
}

@Data
@Document(collection = "cycle-with-different-type-root")
@Document("cycle-with-different-type-root")
static class RefCycleLoadingIntoDifferentTypeRoot {

@Id String id;
Expand All @@ -92,15 +92,15 @@ static class RefCycleLoadingIntoDifferentTypeRoot {
}

@Data
@Document(collection = "cycle-with-different-type-intermediate")
@Document("cycle-with-different-type-intermediate")
static class RefCycleLoadingIntoDifferentTypeIntermediate {

@Id String id;
@DBRef RefCycleLoadingIntoDifferentTypeRootView refToRootView;
}

@Data
@Document(collection = "cycle-with-different-type-root")
@Document("cycle-with-different-type-root")
static class RefCycleLoadingIntoDifferentTypeRootView {

@Id String id;
Expand Down
Expand Up @@ -206,7 +206,7 @@ public void untypedExampleMatchesCorrectly() {
assertThat(result, hasItems(p1, p3));
}

@Document(collection = "dramatis-personae")
@Document("dramatis-personae")
@EqualsAndHashCode
@ToString
static class Person {
Expand Down
Expand Up @@ -22,7 +22,7 @@
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;
Expand Down
Expand Up @@ -102,23 +102,23 @@ public void shouldReadBinaryType() {
assertThat(template.findOne(query(where("id").is(wbd.id)), WithBinaryDataType.class)).isEqualTo(wbd);
}

@Document(collection = COLLECTION)
@Document(COLLECTION)
static class Wrapper {

String id;
UUID uuid;
}

@Data
@Document(collection = COLLECTION)
@Document(COLLECTION)
static class WithBinaryDataInArray {

@Id String id;
byte[] data;
}

@Data
@Document(collection = COLLECTION)
@Document(COLLECTION)
static class WithBinaryDataType {

@Id String id;
Expand Down
Expand Up @@ -515,7 +515,7 @@ static class WrapperDocument {
FlatDocument flatDoc;
}

@Document(collection = "refDoc")
@Document("refDoc")
static class ReferenceDocument {

@Id String id;
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void getPathItemShouldReturnNullWhenIdAndCollectionMatchAndAssignableToIn
assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull();
}

@Document(collection = "one")
@Document("one")
static class EntityOne {

}
Expand All @@ -94,7 +94,7 @@ interface ValueInterface {

}

@Document(collection = "three")
@Document("three")
static class EntityThree implements ValueInterface {

}
Expand Down
Expand Up @@ -113,7 +113,7 @@ public void createIndexShouldThrowMeaningfulExceptionWhenIndexCreationFails() th
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;
Expand Down
Expand Up @@ -192,22 +192,22 @@ public void resolveIndexDefinitionInCustomComposedAnnotatedFields() {
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,
Expand Down Expand Up @@ -239,7 +239,7 @@ static class WithDbRef {
NoIndex indexedDbRef;
}

@Document(collection = "no-index")
@Document("no-index")
static class NoIndex {
@Id String id;
}
Expand Down Expand Up @@ -358,30 +358,30 @@ public void resolvesComposedAnnotationIndexDefinitionOptionsCorrectly() {
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,
type = GeoSpatialIndexType.GEO_2D) //
Point location;
}

@Document(collection = "WithComposedAnnotation")
@Document("WithComposedAnnotation")
static class GeoSpatialIndexedDocumentWithComposedAnnotation {

@ComposedGeoSpatialIndexed //
Expand Down Expand Up @@ -505,19 +505,19 @@ public void singleCompoundIndexUsingComposedAnnotationsOnTypeResolvedCorrectly()
.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 {}
Expand All @@ -526,7 +526,7 @@ static class CompoundIndexOnLevelZero {}
@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 {}
Expand All @@ -535,14 +535,14 @@ static class IndexDefinedOnSuperClass extends CompoundIndexOnLevelZero {

}

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

Expand Down Expand Up @@ -1066,7 +1066,7 @@ static class NoCycleButIndenticallNamedPropertiesDeeplyNested {
@Indexed String foo;
}

@Document(collection = "rules")
@Document("rules")
static class NoCycleManyPathsToDeepValueObject {

private NoCycleLevel3 l3;
Expand Down
Expand Up @@ -221,15 +221,15 @@ public void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation()
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 {
Expand All @@ -253,7 +253,7 @@ static class DocumentWithComposedAnnotation {}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Document(collection = "collection-1")
@Document("collection-1")
static @interface CustomDocumentAnnotation {
}

Expand Down
Expand Up @@ -21,7 +21,7 @@
/**
* @author Jon Brisbin
*/
@Document(collection = "foobar")
@Document("foobar")
public class CustomCollectionWithIndex {

@Id
Expand Down
Expand Up @@ -23,7 +23,7 @@
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document(collection = "geolocation")
@Document("geolocation")
public class GeoLocation {

@Id
Expand Down
Expand Up @@ -21,7 +21,7 @@
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document(collection = "places")
@Document("places")
public class Location {

private ObjectId id;
Expand Down
Expand Up @@ -20,7 +20,7 @@
/**
* @author Jon Brisbin
*/
@Document(collection = "person1")
@Document("person1")
public class PersonCustomCollection1 extends BasePerson {

@Id
Expand Down
Expand Up @@ -20,7 +20,7 @@
/**
* @author Jon Brisbin
*/
@Document(collection = "person2")
@Document("person2")
public class PersonCustomCollection2 extends BasePerson {

@Id
Expand Down
Expand Up @@ -348,7 +348,7 @@ private interface Repo extends MongoRepository<Person, Long> {

// 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> {
Expand Down

0 comments on commit ce23743

Please sign in to comment.