7.3 Metadata based Mapping
To take full advantage of the object mapping functionality inside the Spring Data/MongoDB support, you should annotate your mapped objects with the@org.springframework.data.mongodb.core.mapping.Document
annotation. Although it is not necessary for the mapping framework to have this annotation (your POJOs will be mapped correctly, even without any annotations), it allows the classpath scanner to find and pre-process your domain objects to extract the necessary metadata. If you don't use this annotation, your application will take a slight performance hit the first time you store a domain object because the mapping framework needs to build up its internal metadata model so it knows about the properties of your domain object and how to persist them.
Example 7.3. Example domain object
package com.mycompany.domain; @Document public class Person { @Id private ObjectId id; @Indexed private Integer ssn; private String firstName; @Indexed private String lastName; }
Important | |
---|---|
The |
The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview 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 the database. 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 the field where it is applied from being stored in the database@PersistenceConstructor
- marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject.@Value
- this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object.@Field
- applied at the field level and described the name of the field as it will be represented in the MongoDB 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 is technology agnostic. Specific subclasses are using in the MongoDB support to support annotation based metadata. Other strategies are also possible to put in place if there is demand.
Here is an example of a more complex mapping.
@Document @CompoundIndexes({ @CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}") }) public class Person<T extends Address> { @Id private String id; @Indexed(unique = true) private Integer ssn; @Field("fName") private String firstName; @Indexed private String lastName; private Integer age; @Transient private Integer accountTotal; @DBRef private List<Account> accounts; private T address; public Person(Integer ssn) { this.ssn = ssn; } @PersistenceConstructor public Person(Integer ssn, String firstName, String lastName, Integer age, T address) { this.ssn = ssn; this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getId() { return id; } // no setter for Id. (getter is only exposed for some unit testing) public Integer getSsn() { return ssn; } // other getters/setters ommitted
Compound indexes are also supported. They are defined at the class level, rather than on indidvidual properties.
Note | |
---|---|
Compound indexes are very important to improve the performance of queries that involve criteria on multiple fields |
Here's an example that creates a compound index of lastName
in ascending order and age
in descending order:
Example 7.4. Example Compound Index Usage
package com.mycompany.domain; @Document @CompoundIndexes({ @CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}") }) public class Person { @Id private ObjectId id; private Integer age; private String firstName; private String lastName; }
The mapping framework doesn't have to store child objects embedded within the document. You can also store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks the same as if it had been stored embedded within your master document.
Here's an example of using a DBRef to refer to a specific document that exists independently of the object in which it is referenced (both classes are shown in-line for brevity's sake):
Example 7.5.
@Document public class Account { @Id private ObjectId id; private Float total; } @Document public class Person { @Id private ObjectId id; @Indexed private Integer ssn; @DBRef private List<Account> accounts; }
There's no need to use something like @OneToMany
because the mapping framework sees that you're wanting a one-to-many relationship because there is a List of objects. When the object is stored in MongoDB, there will be a list of DBRefs rather than the Account
objects themselves.
Important | |
---|---|
The mapping framework does not handle cascading saves. If you change an |
Events are fired throughout the lifecycle of the mapping process. This is described in the Lifecycle Events section.
Simply declaring these beans in your Spring ApplicationContext will cause them to be invoked whenever the event is dispatched.
When storing and querying your objects it is convenient to have a MongoConverter
instance handle the mapping of all Java types to DBObjects. However, sometimes you may want the MongoConverter
's do most of the work but allow you to selectivly handle the conversion for a particular type or to optimize performance.
To selectivly handle the conversion yourself, register one or more one or more org.springframework.core.convert.converter.Converter
instances with the MongoConverter.
Note | |
---|---|
Spring 3.0 introduced a core.convert package that provides a general type conversion system. This is described in detail in the Spring reference documentation section entitled Spring 3 Type Conversion. |
The setConverters
method on SimpleMongoConverter
and MappingMongoConverter
should be used for this purpose. The methodafterMappingMongoConverterCreation
in AbstractMongoConfiguration
can be overriden to configure a MappingMongoConverter. The examples here at the begining of this chapter show how to perform the configuration using Java and XML.
Below is an example of a Spring Converter implementation that converts from a DBObject to a Person POJO.
public class PersonReadConverter implements Converter<DBObject, Person> { public Person convert(DBObject source) { Person p = new Person((ObjectId) source.get("_id"), (String) source.get("name")); p.setAge((Integer) source.get("age")); return p; } }
Here is an example that converts from a Person to a DBObject.
public class PersonWriteConverter implements Converter<Person, DBObject> { public DBObject convert(Person source) { DBObject dbo = new BasicDBObject(); dbo.put("_id", source.getId()); dbo.put("name", source.getFirstName()); dbo.put("age", source.getAge()); return dbo; } }
'Framework & Platform > Spring' 카테고리의 다른 글
spring - @EnableTransactionManagement (0) | 2013.06.10 |
---|---|
spring - @MVC에서 favicon.ico 처리 (0) | 2013.06.10 |
spring data - Spring Data MongoDB Remove _class field (0) | 2013.06.03 |
spring data - mongoTemplate CRUD example (0) | 2013.05.29 |
spring data - mongodb 설정 예제 (0) | 2013.05.19 |