Class MappingMongoConverter

java.lang.Object
org.springframework.data.mongodb.core.convert.AbstractMongoConverter
org.springframework.data.mongodb.core.convert.MappingMongoConverter
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.data.convert.EntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty,Object,org.bson.conversions.Bson>, org.springframework.data.convert.EntityReader<Object,org.bson.conversions.Bson>, org.springframework.data.convert.EntityWriter<Object,org.bson.conversions.Bson>, CodecRegistryProvider, MongoConverter, MongoWriter<Object>

public class MappingMongoConverter extends AbstractMongoConverter implements org.springframework.context.ApplicationContextAware
MongoConverter that uses a MappingContext to do sophisticated mapping of domain objects to Document.
Author:
Oliver Gierke, Jon Brisbin, Patrik Wasik, Thomas Darimont, Christoph Strobl, Jordi Llach, Mark Paluch, Roman Puchkovskiy, Heesu Jung, Divya Srivastava, Julia Lee
  • Field Details

    • BSON

      public static final org.springframework.data.util.TypeInformation<org.bson.conversions.Bson> BSON
    • LOGGER

      protected static final org.apache.commons.logging.Log LOGGER
    • mappingContext

      protected final org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext
    • idMapper

      protected final QueryMapper idMapper
    • dbRefResolver

      protected final DbRefResolver dbRefResolver
    • dbRefProxyHandler

      protected final org.springframework.data.mongodb.core.convert.DefaultDbRefProxyHandler dbRefProxyHandler
    • referenceLookupDelegate

      protected final ReferenceLookupDelegate referenceLookupDelegate
    • applicationContext

      @Nullable protected org.springframework.context.ApplicationContext applicationContext
    • typeMapper

      protected MongoTypeMapper typeMapper
    • mapKeyDotReplacement

      @Nullable protected String mapKeyDotReplacement
    • codecRegistryProvider

      @Nullable protected CodecRegistryProvider codecRegistryProvider
  • Constructor Details

  • Method Details

    • getConversionContext

      protected MappingMongoConverter.ConversionContext getConversionContext(org.springframework.data.mongodb.core.convert.ObjectPath path)
      Creates a new MappingMongoConverter.ConversionContext given ObjectPath.
      Parameters:
      path - the current ObjectPath, must not be null.
      Returns:
      the MappingMongoConverter.ConversionContext.
    • setTypeMapper

      public void setTypeMapper(@Nullable MongoTypeMapper typeMapper)
      Configures the MongoTypeMapper to be used to add type information to Documents created by the converter and how to lookup type information from Documents when reading them. Uses a DefaultMongoTypeMapper by default. Setting this to null will reset the TypeMapper to the default one.
      Parameters:
      typeMapper - the typeMapper to set. Can be null.
    • getTypeMapper

      public MongoTypeMapper getTypeMapper()
      Description copied from interface: MongoConverter
      Returns the TypeMapper being used to write type information into Documents created with that converter.
      Specified by:
      getTypeMapper in interface MongoConverter
      Returns:
      will never be null.
    • getProjectionFactory

      public org.springframework.data.projection.ProjectionFactory getProjectionFactory()
      Description copied from interface: MongoConverter
      Returns the ProjectionFactory for this converter.
      Specified by:
      getProjectionFactory in interface MongoConverter
      Returns:
      will never be null.
    • getCustomConversions

      public org.springframework.data.convert.CustomConversions getCustomConversions()
      Description copied from interface: MongoConverter
      Returns the CustomConversions for this converter.
      Specified by:
      getCustomConversions in interface MongoConverter
      Returns:
      will never be null.
    • setMapKeyDotReplacement

      public void setMapKeyDotReplacement(@Nullable String mapKeyDotReplacement)
      Configure the characters dots potentially contained in a Map shall be replaced with. By default we don't do any translation but rather reject a Map with keys containing dots causing the conversion for the entire object to fail. If further customization of the translation is needed, have a look at potentiallyEscapeMapKey(String) as well as potentiallyUnescapeMapKey(String).

      mapKeyDotReplacement is used as-is during replacement operations without further processing (i.e. regex or normalization).

      Parameters:
      mapKeyDotReplacement - the mapKeyDotReplacement to set. Can be null.
    • setCodecRegistryProvider

      public void setCodecRegistryProvider(@Nullable CodecRegistryProvider codecRegistryProvider)
      Configure a CodecRegistryProvider that provides native MongoDB codecs for reading values.
      Parameters:
      codecRegistryProvider - can be null.
      Since:
      2.2
    • getMappingContext

      public org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
      Specified by:
      getMappingContext in interface org.springframework.data.convert.EntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty,Object,org.bson.conversions.Bson>
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • setEntityCallbacks

      public void setEntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks entityCallbacks)
      Set the EntityCallbacks instance to use when invoking callbacks like the AfterConvertCallback.
      Overrides potentially existing EntityCallbacks.
      Parameters:
      entityCallbacks - must not be null.
      Throws:
      IllegalArgumentException - if the given instance is null.
      Since:
      3.0
    • project

      public <R> R project(org.springframework.data.projection.EntityProjection<R,?> projection, org.bson.conversions.Bson bson)
      Description copied from interface: MongoConverter
      Apply a projection to Bson and return the projection return type R. Non-projecting descriptors fall back to regular object materialization.
      Specified by:
      project in interface MongoConverter
      Parameters:
      projection - the projection descriptor, must not be null.
      bson - must not be null.
      Returns:
      a new instance of the projection return type R.
    • read

      public <S> S read(Class<S> clazz, org.bson.conversions.Bson bson)
      Specified by:
      read in interface org.springframework.data.convert.EntityReader<Object,org.bson.conversions.Bson>
    • read

      protected <S> S read(org.springframework.data.util.TypeInformation<S> type, org.bson.conversions.Bson bson)
    • readDocument

      protected <S> S readDocument(MappingMongoConverter.ConversionContext context, org.bson.conversions.Bson bson, org.springframework.data.util.TypeInformation<? extends S> typeHint)
      Conversion method to materialize an object from a document. Can be overridden by subclasses.
      Parameters:
      context - must not be null
      bson - must not be null
      typeHint - the TypeInformation to be used to unmarshall this Document.
      Returns:
      the converted object, will never be null.
      Since:
      3.2
    • toDBRef

      public com.mongodb.DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referringProperty)
      Description copied from interface: MongoWriter
      Creates a DBRef to refer to the given object.
      Specified by:
      toDBRef in interface MongoWriter<Object>
      Parameters:
      object - the object to create a DBRef to link to. The object's type has to carry an id attribute.
      referringProperty - the client-side property referring to the object which might carry additional metadata for the DBRef object to create. Can be null.
      Returns:
      will never be null.
    • toDocumentPointer

      public DocumentPointer toDocumentPointer(Object source, @Nullable MongoPersistentProperty referringProperty)
      Description copied from interface: MongoWriter
      Creates a the DocumentPointer representing the link to another entity.
      Specified by:
      toDocumentPointer in interface MongoWriter<Object>
      Parameters:
      source - the object to create a document link to.
      referringProperty - the client-side property referring to the object which might carry additional metadata for the DBRef object to create. Can be null.
      Returns:
      will never be null.
    • write

      public void write(Object obj, org.bson.conversions.Bson bson)
      Root entry method into write conversion. Adds a type discriminator to the Document. Shouldn't be called for nested conversions.
      Specified by:
      write in interface org.springframework.data.convert.EntityWriter<Object,org.bson.conversions.Bson>
      See Also:
      • EntityWriter.write(java.lang.Object, java.lang.Object)
    • writeInternal

      protected void writeInternal(@Nullable Object obj, org.bson.conversions.Bson bson, @Nullable org.springframework.data.util.TypeInformation<?> typeHint)
      Internal write conversion method which should be used for nested invocations.
    • writeInternal

      protected void writeInternal(@Nullable Object obj, org.bson.conversions.Bson bson, @Nullable MongoPersistentEntity<?> entity)
    • writePropertyInternal

      protected void writePropertyInternal(@Nullable Object obj, org.springframework.data.mongodb.core.convert.DocumentAccessor accessor, MongoPersistentProperty prop, org.springframework.data.mapping.PersistentPropertyAccessor<?> persistentPropertyAccessor)
    • createCollection

      protected List<Object> createCollection(Collection<?> collection, MongoPersistentProperty property)
      Writes the given Collection using the given MongoPersistentProperty information.
      Parameters:
      collection - must not be null.
      property - must not be null.
    • createMap

      protected org.bson.conversions.Bson createMap(Map<Object,Object> map, MongoPersistentProperty property)
      Writes the given Map using the given MongoPersistentProperty information.
      Parameters:
      map - must not null.
      property - must not be null.
    • writeMapInternal

      protected org.bson.conversions.Bson writeMapInternal(Map<Object,Object> obj, org.bson.conversions.Bson bson, org.springframework.data.util.TypeInformation<?> propertyType)
      Writes the given Map to the given Document considering the given TypeInformation.
      Parameters:
      obj - must not be null.
      bson - must not be null.
      propertyType - must not be null.
    • potentiallyEscapeMapKey

      protected String potentiallyEscapeMapKey(String source)
      Potentially replaces dots in the given map key with the configured map key replacement if configured or aborts conversion if none is configured.
      Parameters:
      source - must not be null.
      See Also:
    • potentiallyUnescapeMapKey

      protected String potentiallyUnescapeMapKey(String source)
      Translates the map key replacements in the given key just read with a dot in case a map key replacement has been configured.
      Parameters:
      source - must not be null.
    • addCustomTypeKeyIfNecessary

      protected void addCustomTypeKeyIfNecessary(@Nullable org.springframework.data.util.TypeInformation<?> type, Object value, org.bson.conversions.Bson bson)
      Adds custom type information to the given Document if necessary. That is if the value is not the same as the one given. This is usually the case if you store a subtype of the actual declared type of the property.
      Parameters:
      type - can be null.
      value - must not be null.
      bson - must not be null.
    • getPotentiallyConvertedSimpleRead

      protected Object getPotentiallyConvertedSimpleRead(Object value, org.springframework.data.util.TypeInformation<?> target)
      Checks whether we have a custom conversion for the given simple object. Converts the given value if so, applies Enum handling or returns the value as is. Can be overridden by subclasses.
      Since:
      3.2
    • createDBRef

      protected com.mongodb.DBRef createDBRef(Object target, @Nullable MongoPersistentProperty property)
    • readCollectionOrArray

      protected Object readCollectionOrArray(MappingMongoConverter.ConversionContext context, Collection<?> source, org.springframework.data.util.TypeInformation<?> targetType)
      Reads the given Collection into a collection of the given TypeInformation. Can be overridden by subclasses.
      Parameters:
      context - must not be null
      source - must not be null
      targetType - the Map TypeInformation to be used to unmarshall this Document.
      Returns:
      the converted Collection or array, will never be null.
      Since:
      3.2
    • readMap

      protected Map<Object,Object> readMap(MappingMongoConverter.ConversionContext context, org.bson.conversions.Bson bson, org.springframework.data.util.TypeInformation<?> targetType)
      Reads the given Document into a Map. will recursively resolve nested Maps as well. Can be overridden by subclasses.
      Parameters:
      context - must not be null
      bson - must not be null
      targetType - the Map TypeInformation to be used to unmarshall this Document.
      Returns:
      the converted Map, will never be null.
      Since:
      3.2
    • convertToMongoType

      @Nullable public Object convertToMongoType(@Nullable Object obj, @Nullable org.springframework.data.util.TypeInformation<?> typeInformation)
      Description copied from interface: MongoWriter
      Converts the given object into one Mongo will be able to store natively but retains the type information in case the given TypeInformation differs from the given object type.
      Specified by:
      convertToMongoType in interface MongoWriter<Object>
      Parameters:
      obj - can be null.
      typeInformation - can be null.
      Returns:
    • convertToMongoType

      public Object convertToMongoType(@Nullable Object obj, MongoPersistentEntity entity)
      Specified by:
      convertToMongoType in interface MongoWriter<Object>
    • maybeConvertList

      public List<Object> maybeConvertList(Iterable<?> source, @Nullable org.springframework.data.util.TypeInformation<?> typeInformation)
    • maybeCallAfterConvert

      protected <T> T maybeCallAfterConvert(T object, org.bson.Document document, String collection)
    • getWriteTarget

      public Class<?> getWriteTarget(Class<?> source)
      Get the conversion target type if defined or return the source.
      Parameters:
      source - must not be null.
      Returns:
      Since:
      2.2
    • getCodecRegistry

      public org.bson.codecs.configuration.CodecRegistry getCodecRegistry()
      Description copied from interface: CodecRegistryProvider
      Get the underlying CodecRegistry used by the MongoDB Java driver.
      Specified by:
      getCodecRegistry in interface CodecRegistryProvider
      Specified by:
      getCodecRegistry in interface MongoConverter
      Returns:
      never null.
    • with

      Create a new MappingMongoConverter using the given MongoDatabaseFactory when loading DBRef.
      Returns:
      new instance of MappingMongoConverter. Never null.
      Since:
      2.1.6