diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/AbstractMongoConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/AbstractMongoConfiguration.java
index 24b66c918..3ee5bcd71 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/AbstractMongoConfiguration.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/AbstractMongoConfiguration.java
@@ -74,9 +74,18 @@ public abstract class AbstractMongoConfiguration {
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(mongoMappingContext());
converter.setMongo(mongo());
+ afterMappingMongoConverterCreation(converter);
return converter;
}
+ /**
+ * Hook that allows post-processing after the MappingMongoConverter has been
+ * successfully created.
+ * @param converter
+ */
+ protected void afterMappingMongoConverterCreation(MappingMongoConverter converter) {
+ }
+
@Bean
public MappingContextAwareBeanPostProcessor mappingContextAwareBeanPostProcessor() {
MappingContextAwareBeanPostProcessor bpp = new MappingContextAwareBeanPostProcessor();
diff --git a/src/docbkx/reference/mapping.xml b/src/docbkx/reference/mapping.xml
index 9264fb41a..2b1038a90 100644
--- a/src/docbkx/reference/mapping.xml
+++ b/src/docbkx/reference/mapping.xml
@@ -20,7 +20,7 @@
You can configure the MongoMappingConverter as well as Mongo and
MongoTemplate eithe using Java or XML based metadata.
- Here is an example using Spring's Java based configuration
+ Here is an example using Spring's Java based configuration
@Configuration class to configure MongoDB mapping support
@@ -207,7 +207,7 @@ public class Person {
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.
+ database before it is used to construct a domain object.
@@ -268,7 +268,7 @@ public class Person<T extends Address> {
}
-
+
@@ -375,5 +375,36 @@ public class Person {
Simply declaring these beans in your Spring ApplicationContext
will cause them to be invoked whenever the event is dispatched.
+
+
+ Overriding Mapping with explicit Converters
+
+ 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.
+ To do this, register one or more one or more
+ org.springframework.core.convert.converter.Converter
+ instances with the MongoConverter.
+
+
+ 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 method
+ afterMappingMongoConverterCreation in
+ AbstractMongoConfiguration can be overriden to
+ configure a MappingMongoConverter.
+
+
+
diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml
index 65b860214..5fa9215c1 100644
--- a/src/docbkx/reference/mongodb.xml
+++ b/src/docbkx/reference/mongodb.xml
@@ -252,7 +252,7 @@ public class AppConfig {
/*
* Use the standard Mongo driver API to create a com.mongodb.Mongo instance.
*/
- public @Bean Mongo mongo() throws UnknownHostException, MongoException {
+ public @Bean Mongo mongo() throws Exception {
return new Mongo("localhost");
}
}
@@ -297,6 +297,11 @@ public class AppConfig {
}
}
+
+ To access the com.mongodb.Mongo object created by the
+ MongoFactoryBean in other @Configuration or
+ your own classes, use a "private @Autowired Mongo
+ mongo;" field.
@@ -363,17 +368,34 @@ public class AppConfig {
- A configuration using replica sets is shown below:
- XML schema to configure replica sets in MongoDB
-
- <beans>
+ A configuration using replica sets within the XML schema is not
+ yet available. If you would like to configure ReplicaSets use Spring's
+ Java based bean metadata shown below:
+ Configuring a com.mongodb.Mongo object with Replica Sets
+ using Java based bean metadata
- <mongo:mongo>
- <! replica set TBD -- should be available for release 1.0.0.RC1 -->
- <mongo:mongo>
+ @Configuration
+public class AppConfig {
-</beans>
-
+ /*
+ * Use the standard Mongo driver API to create a com.mongodb.Mongo instance that supports replica sets
+ */
+ @Bean
+ public Mongo mongo() throws Exception {
+
+ List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
+ serverAddresses.add( new ServerAddress( "127.0.0.1", 27017 ) );
+ serverAddresses.add( new ServerAddress( "127.0.0.1", 27018 ) );
+
+ MongoOptions options = new MongoOptions();
+ options.autoConnectRetry = true;
+
+ Mongo mongo = new Mongo( serverAddresses, options );
+ mongo.slaveOk();
+
+ return mongo;
+ }
+}
@@ -567,9 +589,9 @@ public class AppConfig {
Saving, Updating, and Removing Documents
- MongoTemplate provides a simple way for you to save, update, and
- delete your domain objects and map those objects to documents stored in
- MongoDB.
+ MongoTemplate provides a simple way for you
+ to save, update, and delete your domain objects and map those objects to
+ documents stored in MongoDB.
Given a simple class such as Person
@@ -583,7 +605,12 @@ public class AppConfig {
}
- You can save, update and delete the object as shown below
+ You can save, update and delete the object as shown below.
+
+
+ MongoOperations is the interface
+ that MongoTemplate implements.
+
public class PersonExample {
@@ -621,7 +648,7 @@ public class AppConfig {
}
This would produce the following log output (including some debug
- message from MongoTemplate itself)
+ message from MongoTemplate itself)
Saved: PersonWithIdPropertyOfTypeString [id=4d9e82ac94fa72c65a9e7d5f, firstName=Sven, age=22]
findOne using query: { "_id" : { "$oid" : "4d9e82ac94fa72c65a9e7d5f"}} in db.collection: database.personexample
@@ -631,7 +658,7 @@ Updated: PersonWithIdPropertyOfTypeString [id=4d9e82ac94fa72c65a9e7d5f, firstNam
remove using query: { "_id" : { "$oid" : "4d9e82ac94fa72c65a9e7d5f"}}
Number of people = : 0
- There was implicit conversion using SimpleMongoConverter between a
+ There was implicit conversion using the MongoConverter between a
String and ObjectId as stored in the database and recognizing a convention
of the property "Id" name.
@@ -753,6 +780,34 @@ import static org.springframework.data.document.mongodb.query.Criteria.query;
+ Unless and explicit MongoWriter is passed into the save or insert
+ method, the the template's MongoConverter will be used.
+
+
+ Saving using MongoWriter
+
+ The MongoWriter interface allows you to have lower level control
+ over the mapping of an object into a DBObject. The MongoWriter
+ interface is
+
+ /**
+ * A MongoWriter is responsible for converting an object of type T to the native MongoDB representation DBObject.
+ *
+ * @param <T> the type of the object to convert to a DBObject
+ */
+public interface MongoWriter<T> {
+
+ /**
+ * Write the given object of type T to the native MongoDB object representation DBObject.
+ *
+ * @param t The object to convert to a DBObject
+ * @param dbo The DBObject to use for writing.
+ */
+ void write(T t, DBObject dbo);
+
+}
+
+
Inserting Lists of objects in batch
@@ -1391,7 +1446,39 @@ import static org.springframework.data.document.mongodb.query.Query.query;
-
+ The MongoReader can be used
+
+
+ Reading using MongoWriter
+
+ The MongoReader interface allows you to have lower level control
+ over the mapping of an DBObject into a Java object. This is similar to
+ the role of RowMapper in JdbcTemplate. The MongoReader interface
+ is
+
+ /**
+ * A MongoWriter is responsible for converting a native MongoDB DBObject to an object of type T.
+ *
+ * @param <T> the type of the object to convert from a DBObject
+ */
+public interface MongoReader<T> {
+
+ /**
+ * Ready from the native MongoDB DBObject representation to an instance of the class T. The given type has to be the
+ * starting point for marshalling the {@link DBObject} into it. So in case there's no real valid data inside
+ * {@link DBObject} for the given type, just return an empty instance of the given type.
+ *
+ * @param clazz the type of the return value
+ * @param dbo theDBObject
+ * @return the converted object
+ */
+ <S extends T> S read(Class<S> clazz, DBObject dbo);
+}
+
+
+
+
+