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!Configuring custom type mappingThe following example demonstrates how to configure a custom MongoTypeMapper inMappingMongoConverter.class CustomMongoTypeMapper extends DefaultMongoTypeMapper {//implement custom type mapping here}@Configurationclass SampleMongoConfiguration extends AbstractMongoConfiguration {@Overrideprotected String get<strong>Data</strong>baseName() {return "database";}@Overridepublic Mongo mongo() throws Exception {return new Mongo();}@Bean@Overridepublic MappingMongoConverter mappingMongoConverter() throws Exception {MappingMongoConverter mmc = super.mappingMongoConverter();mmc.setTypeMapper(customTypeMapper());return mmc;}}@Beanpublic MongoTypeMapper customTypeMapper() {return new CustomMongoTypeMapper();}Note that we are extending the AbstractMongoConfiguration class and override the beandefinition of the MappingMongoConverter where we configure our custom MongoTypeMapper.Example 5.9 Configuring a custom MongoTypeMapper via <strong>Spring</strong> Java ConfigExample 5.10 Configuring a custom MongoTypeMapper via XMLMethods for saving and inserting documentsThere are several convenient methods on MongoTemplate for saving and inserting yourobjects. To have more fine grained control over the conversion process you can register <strong>Spring</strong>converters with the MappingMongoConverter, for example Converter andConverter.NoteThe difference between insert and save operations is that a save operation will perform an insertif the object is not already present.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 40

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

Saved successfully!

Ooh no, something went wrong!