Java Mongo Custom Serialization notes

Source: Internet
Author: User
Tags serialization

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.