13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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 <strong>MongoDB</strong> _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 <strong>Spring</strong> Framework . Within the mapping framework it canbe applied to constructor arguments. This lets you use a <strong>Spring</strong> 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-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 81

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

Saved successfully!

Ooh no, something went wrong!