Start with the Insert method
1. Org.springframework.data.mongodb.repository.support.SimpleMongoRepository.java Insert
2. Org.springframework.data.mongodb.core.MongoTemplate.java Todbobject
3. Org.springframework.data.mongodb.core.convert.MappingMongoConverter.java writeinternal
See the key code:
mongopersistententity<?> entity = mappingcontext.getpersistententity (entityType); Writeinternal (obj, dbo, entity); Addcustomtypekeyifnecessary (typehint, obj, dbo);
The 2nd place writeinternal appears:
//Write the PropertiesEntity.dowithproperties (NewPropertyhandler<mongopersistentproperty>() { Public voidDowithpersistentproperty (Mongopersistentproperty prop) {if(Prop.equals (idproperty) | |!prop.iswritable ()) { return; } Object propertyobj=Accessor.getproperty (prop); if(NULL!=propertyobj) { if(!Conversions.issimpletype (Propertyobj.getclass ())) {writepropertyinternal (propertyobj, dbo, prop); } Else{writesimpleinternal (propertyobj, dbo, prop); } } } });
which
entity = Org.springframework.data.mapping.model.BasicPersistentEntity
Writepropertyinternal enters a write that is not a simple property.
Then call Writeinternal to write the property, it can be seen that the non-simple property as an object to write the loop.
When writing the ID, call:
Org.springframework.data.mongodb.core.convert.QueryMapper.convertId method
/*** Converts the given raw ID value into either {@linkObjectId} or {@linkString}. * * @paramID *@return */ PublicObject Convertid (object ID) {if(id = =NULL) { return NULL; } if(IDinstanceofString) { returnObjectid.isvalid (id.tostring ())? Conversionservice.convert (ID, ObjectId.class): ID; } Try { returnConversionservice.canconvert (Id.getclass (), ObjectId.class) ? Conversionservice.convert (ID, ObjectId.class): Delegateconverttomongotype (ID,NULL); } Catch(conversionexception o_o) {returnDelegateconverttomongotype (ID,NULL); } }
There is no distinction between specific entity types or the depth of attributes in the root entity, which is relatively simple and rude.
Java Mongo Custom Serialization notes