Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource

static.springsource.org
from static.springsource.org More from this publisher
13.07.2015 Views

please define productname in your docbook file!package com.mycompany.domain;@Documentpublic class Person {@Idprivate ObjectId id;@Indexedprivate Integer ssn;private String firstName;@Indexedprivate String lastName;}Example 7.3 Example domain objectImportantThe @Id annotation tells the mapper which property you want to use for the MongoDB _idproperty and the @Indexed annotation tells the mapping framework to call ensureIndex onthat property of your document, making searches faster.Mapping annotation overviewThe MappingMongoConverter can use metadata to drive the mapping of objects to documents. Anoverview of the annotations is provided below• @Id - applied at the field level to mark the field used for identiy purpose.• @Document - applied at the class level to indicate this class is a candidate for mapping to thedatabase. You can specify the name of the collection where the database will be stored.• @DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.• @Indexed - applied at the field level to describe how to index the field.• @CompoundIndex - applied at the type level to declare Compound Indexes• @GeoSpatialIndexed - applied at the field level to describe how to geoindex the field.• @Transient - by default all private fields are mapped to the document, this annotation excludes thefield where it is applied from being stored in the database• @PersistenceConstructor - marks a given constructor - even a package protected one - to usewhen instantiating the object from the database. Constructor arguments are mapped by name to thekey values in the retrieved DBObject.• @Value - this annotation is part of the Spring Framework . Within the mapping framework it canbe applied to constructor arguments. This lets you use a Spring Expression Language statementto transform a key's value retrieved in the database before it is used to construct a domainobject. In order to reference a property of a given document one has to use expressions like:@Value("#root.myProperty") where root refers to the root of the given document.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 81

please define productname in your docbook file!• @Field - applied at the field level and described the name of the field as it will be represented in theMongoDB BSON document thus allowing the name to be different than the fieldname of the class.The mapping metadata infrastructure is defined in a seperate spring-data-commons project that istechnology agnostic. Specific subclasses are using in the MongoDB support to support annotation basedmetadata. Other strategies are also possible to put in place if there is demand.Here is an example of a more complex mapping.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 82

please define productname in your docbook file!• @Field - applied at the field level and described the name of the field as it will be represented in the<strong>MongoDB</strong> BSON document thus allowing the name to be different than the fieldname of the class.The mapping metadata infrastructure is defined in a seperate spring-data-commons project that istechnology agnostic. Specific subclasses are using in the <strong>MongoDB</strong> support to support annotation basedmetadata. Other strategies are also possible to put in place if there is demand.Here is an example of a more complex mapping.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 82

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!