diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 6462081fa..077877db7 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -1115,6 +1115,70 @@ DEBUG work.data.mongodb.core.MongoTemplate: 376 - Dropped collection [database.p domain classes. +
+ Type mapping + + As MongoDB collections can contain documents that represent + instances of a variety of types. A great example here is if you store a + hierarchy of classes or simply have a class with a property of type + Object. In the latter case the values held inside + that property have to be read in correctly when retrieving the object. + Thus we need a mechanism to store type information alongside the actual + document. + + To achieve that the MappingMongoConverter + uses a MongoTypeMapper abstraction with + DefaultMongoTypeMapper as it's main + implementation. It's default behaviour is storing the fully qualified + classname under _class inside the document for the + top-level document as well as for every value if it's a complex type and + a subtype of the property type declared. + + + Type mapping + + public class Sample { + Contact value; +} + +public abstract class Contact { … } + +public class Person extends Contact { … } + +Sample sample = new Sample(); +sample.value = new Person(); + +mongoTemplate.save(sample); + +{ "_class" : "com.acme.Sample", + "value" : { "_class" : "com.acme.Person" } +} + + + As you can see we store the type information for the actual root + class persistet as well as for the nested type as it is complex and a + subtype of Contact. So if you're now using + mongoTemplate.findAll(Object.class, "sample") + we are able to find out that the document stored shall be a + Sample instance. We are also able to find out + that the value property shall be a Person + actually. + + + Customizing type mapping + + In case you want to avoid writing the entire Java class name as + type information but rather like to use some key you can use the + @TypeAlias annotation at the entity + class being persisted. If you need to customize the mapping even more + have a look at the + TypeInformationMapper interface. An + instance of that interface can be configured at the + DefaultMongoTypeMapper which can be configured + in turn on MappingMongoConverter. + +
+
Methods for saving and inserting documents @@ -1468,8 +1532,6 @@ assertThat(p.getAge(), is(1)); name. - -