Module spring.data.mongodb
Class MappingMongoConverter
java.lang.Object
org.springframework.data.mongodb.core.convert.AbstractMongoConverter
org.springframework.data.mongodb.core.convert.MappingMongoConverter
- All Implemented Interfaces:
Aware,InitializingBean,ApplicationContextAware,EnvironmentCapable,EntityConverter<MongoPersistentEntity<?>,,MongoPersistentProperty, Object, org.bson.conversions.Bson> EntityReader<Object,,org.bson.conversions.Bson> EntityWriter<Object,,org.bson.conversions.Bson> CodecRegistryProvider,MongoConverter,MongoWriter<Object>
public class MappingMongoConverter
extends AbstractMongoConverter
implements ApplicationContextAware, EnvironmentCapable
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceConversion context defining an interface for graph-traversal-based conversion of documents.protected static classConversion context holding references to simpleMappingMongoConverter.DefaultConversionContext.ValueConverterandMappingMongoConverter.DefaultConversionContext.ContainerValueConverter. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ApplicationContextstatic final TypeInformation<org.bson.conversions.Bson>protected CodecRegistryProviderprotected final org.springframework.data.mongodb.core.convert.DefaultDbRefProxyHandlerprotected final DbRefResolverprotected Environmentprotected final QueryMapperprotected static final Logprotected Stringprotected final MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> protected final ReferenceLookupDelegateprotected MongoTypeMapperFields inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
conversions, conversionService, instantiators -
Constructor Summary
ConstructorsConstructorDescriptionMappingMongoConverter(DbRefResolver dbRefResolver, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) MappingMongoConverter(MongoDatabaseFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddCustomTypeKeyIfNecessary(TypeInformation<?> type, Object value, org.bson.conversions.Bson bson) Adds custom type information to the givenDocumentif necessary.convertToMongoType(Object obj, MongoPersistentEntity entity) convertToMongoType(Object obj, TypeInformation<?> typeInformation) Converts the given object into one Mongo will be able to store natively but retains the type information in case the givenTypeInformationdiffers from the given object type.createCollection(Collection<?> collection, MongoPersistentProperty property) Writes the givenCollectionusing the givenMongoPersistentPropertyinformation.protected com.mongodb.DBRefcreateDBRef(Object target, MongoPersistentProperty property) protected org.bson.conversions.BsoncreateMap(Map<Object, Object> map, MongoPersistentProperty property) Writes the givenMapusing the givenMongoPersistentPropertyinformation.org.bson.codecs.configuration.CodecRegistryGet the underlyingCodecRegistryused by the MongoDB Java driver.protected MappingMongoConverter.ConversionContextgetConversionContext(org.springframework.data.mongodb.core.convert.ObjectPath path) Creates a newMappingMongoConverter.ConversionContextgivenObjectPath.Returns theCustomConversionsfor this converter.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> protected ObjectgetPotentiallyConvertedSimpleRead(Object value, TypeInformation<?> target) Checks whether we have a custom conversion for the given simple object.Returns theProjectionFactoryfor this converter.Returns theTypeMapperbeing used to write type information intoDocuments created with that converter.Class<?>getWriteTarget(Class<?> source) Get the conversion target type if defined or return the source.protected <T> TmaybeCallAfterConvert(T object, org.bson.Document document, String collection) maybeConvertList(Iterable<?> source, TypeInformation<?> typeInformation) protected StringpotentiallyEscapeMapKey(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.protected StringpotentiallyUnescapeMapKey(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.voidpreserveMapKeys(boolean preserve) Ifpreserveis set to true the conversion will treat map keys containing dot (.) characters as is.<R> Rproject(EntityProjection<R, ?> projection, org.bson.conversions.Bson bson) Apply a projection toBsonand return the projection return typeR.<S> Sprotected <S> Sread(TypeInformation<S> type, org.bson.conversions.Bson bson) protected ObjectreadCollectionOrArray(MappingMongoConverter.ConversionContext context, Collection<?> source, TypeInformation<?> targetType) Reads the givenCollectioninto a collection of the givenTypeInformation.protected <S> SreadDocument(MappingMongoConverter.ConversionContext context, org.bson.conversions.Bson bson, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument.readMap(MappingMongoConverter.ConversionContext context, org.bson.conversions.Bson bson, TypeInformation<?> targetType) voidsetApplicationContext(ApplicationContext applicationContext) voidsetCodecRegistryProvider(CodecRegistryProvider codecRegistryProvider) Configure aCodecRegistryProviderthat provides native MongoDBcodecsfor reading values.voidsetEntityCallbacks(EntityCallbacks entityCallbacks) voidsetMapKeyDotReplacement(String mapKeyDotReplacement) Configure the characters dots potentially contained in aMapshall be replaced with.voidsetTypeMapper(MongoTypeMapper typeMapper) Configures theMongoTypeMapperto be used to add type information toDocuments created by the converter and how to lookup type information fromDocuments when reading them.com.mongodb.DBReftoDBRef(Object object, MongoPersistentProperty referringProperty) Creates aDBRefto refer to the given object.toDocumentPointer(Object source, MongoPersistentProperty referringProperty) Creates a theDocumentPointerrepresenting the link to another entity.with(MongoDatabaseFactory dbFactory) voidRoot entry method into write conversion.protected voidwriteInternal(Object obj, org.bson.conversions.Bson bson, MongoPersistentEntity<?> entity) protected voidwriteInternal(Object obj, org.bson.conversions.Bson bson, TypeInformation<?> typeHint) Internal write conversion method which should be used for nested invocations.protected org.bson.conversions.BsonwriteMapInternal(Map<Object, Object> obj, org.bson.conversions.Bson bson, TypeInformation<?> propertyType) protected voidwritePropertyInternal(Object obj, org.springframework.data.mongodb.core.convert.DocumentAccessor accessor, MongoPersistentProperty prop, PersistentPropertyAccessor<?> persistentPropertyAccessor) Methods inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
afterPropertiesSet, getConversionService, setCustomConversions, setInstantiatorsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.data.mongodb.CodecRegistryProvider
getCodecFor, hasCodecForMethods inherited from interface org.springframework.data.mongodb.core.convert.MongoConverter
convertId, mapValueToTargetTypeMethods inherited from interface org.springframework.data.mongodb.core.convert.MongoWriter
convertToMongoType
-
Field Details
-
BSON
-
LOGGER
-
mappingContext
protected final MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext -
idMapper
-
dbRefResolver
-
dbRefProxyHandler
protected final org.springframework.data.mongodb.core.convert.DefaultDbRefProxyHandler dbRefProxyHandler -
referenceLookupDelegate
-
applicationContext
-
environment
-
typeMapper
-
mapKeyDotReplacement
-
codecRegistryProvider
-
-
Constructor Details
-
MappingMongoConverter
public MappingMongoConverter(DbRefResolver dbRefResolver, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) - Parameters:
dbRefResolver- must not be null.mappingContext- must not be null.
-
MappingMongoConverter
@Deprecated public MappingMongoConverter(MongoDatabaseFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) Deprecated.use the constructor taking aDbRefResolverinstead.- Parameters:
mongoDbFactory- must not be null.mappingContext- must not be null.
-
-
Method Details
-
getConversionContext
protected MappingMongoConverter.ConversionContext getConversionContext(org.springframework.data.mongodb.core.convert.ObjectPath path) Creates a newMappingMongoConverter.ConversionContextgivenObjectPath.- Parameters:
path- the currentObjectPath, must not be null.- Returns:
- the
MappingMongoConverter.ConversionContext.
-
setTypeMapper
Configures theMongoTypeMapperto be used to add type information toDocuments created by the converter and how to lookup type information fromDocuments when reading them. Uses aDefaultMongoTypeMapperby default. Setting this to null will reset theTypeMapperto the default one.- Parameters:
typeMapper- the typeMapper to set. Can be null.
-
getTypeMapper
Description copied from interface:MongoConverterReturns theTypeMapperbeing used to write type information intoDocuments created with that converter.- Specified by:
getTypeMapperin interfaceMongoConverter- Returns:
- will never be null.
-
getProjectionFactory
Description copied from interface:MongoConverterReturns theProjectionFactoryfor this converter.- Specified by:
getProjectionFactoryin interfaceMongoConverter- Returns:
- will never be null.
-
getCustomConversions
Description copied from interface:MongoConverterReturns theCustomConversionsfor this converter.- Specified by:
getCustomConversionsin interfaceMongoConverter- Returns:
- will never be null.
-
setMapKeyDotReplacement
Configure the characters dots potentially contained in aMapshall be replaced with. By default, we don't do any translation but rather reject aMapwith keys containing dots causing the conversion for the entire object to fail. If further customization of the translation is needed, have a look atpotentiallyEscapeMapKey(String)as well aspotentiallyUnescapeMapKey(String).mapKeyDotReplacementis used as-is during replacement operations without further processing (i.e. regex or normalization).- Parameters:
mapKeyDotReplacement- the mapKeyDotReplacement to set. Can be null.
-
preserveMapKeys
public void preserveMapKeys(boolean preserve) Ifpreserveis set to true the conversion will treat map keys containing dot (.) characters as is.- Since:
- 4.2
- See Also:
-
setCodecRegistryProvider
Configure aCodecRegistryProviderthat provides native MongoDBcodecsfor reading values.- Parameters:
codecRegistryProvider- can be null.- Since:
- 2.2
-
getMappingContext
public MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()- Specified by:
getMappingContextin interfaceEntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty, Object, org.bson.conversions.Bson>
-
setApplicationContext
- Specified by:
setApplicationContextin interfaceApplicationContextAware- Throws:
BeansException
-
getEnvironment
- Specified by:
getEnvironmentin interfaceEnvironmentCapable
-
setEntityCallbacks
Set theEntityCallbacksinstance to use when invokingcallbackslike theAfterConvertCallback.
Overrides potentially existingEntityCallbacks.- Parameters:
entityCallbacks- must not be null.- Throws:
IllegalArgumentException- if the given instance is null.- Since:
- 3.0
-
project
Description copied from interface:MongoConverterApply a projection toBsonand return the projection return typeR.Non-projectingdescriptors fall back toregular object materialization.- Specified by:
projectin interfaceMongoConverter- 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
- Specified by:
readin interfaceEntityReader<Object,org.bson.conversions.Bson>
-
read
-
readDocument
protected <S> S readDocument(MappingMongoConverter.ConversionContext context, org.bson.conversions.Bson bson, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument. Can be overridden by subclasses.- Parameters:
context- must not be nullbson- must not be nulltypeHint- theTypeInformationto be used to unmarshall thisDocument.- 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:MongoWriterCreates aDBRefto refer to the given object.- Specified by:
toDBRefin interfaceMongoWriter<Object>- Parameters:
object- the object to create aDBRefto 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 theDBRefobject to create. Can be null.- Returns:
- will never be null.
-
toDocumentPointer
public DocumentPointer toDocumentPointer(Object source, @Nullable MongoPersistentProperty referringProperty) Description copied from interface:MongoWriterCreates a theDocumentPointerrepresenting the link to another entity.- Specified by:
toDocumentPointerin interfaceMongoWriter<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 theDBRefobject to create. Can be null.- Returns:
- will never be null.
-
write
Root entry method into write conversion. Adds a type discriminator to theDocument. Shouldn't be called for nested conversions.- Specified by:
writein interfaceEntityWriter<Object,org.bson.conversions.Bson> - See Also:
-
writeInternal
protected void writeInternal(@Nullable Object obj, org.bson.conversions.Bson bson, @Nullable 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, PersistentPropertyAccessor<?> persistentPropertyAccessor) -
createCollection
Writes the givenCollectionusing the givenMongoPersistentPropertyinformation.- 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 givenMapusing the givenMongoPersistentPropertyinformation.- 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, TypeInformation<?> propertyType) - Parameters:
obj- must not be null.bson- must not be null.propertyType- must not be null.
-
potentiallyEscapeMapKey
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
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 TypeInformation<?> type, Object value, org.bson.conversions.Bson bson) Adds custom type information to the givenDocumentif 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
Checks whether we have a custom conversion for the given simple object. Converts the given value if so, appliesEnumhandling or returns the value as is. Can be overridden by subclasses.- Since:
- 3.2
-
createDBRef
-
readCollectionOrArray
protected Object readCollectionOrArray(MappingMongoConverter.ConversionContext context, Collection<?> source, TypeInformation<?> targetType) Reads the givenCollectioninto a collection of the givenTypeInformation. Can be overridden by subclasses.- Parameters:
context- must not be nullsource- must not be nulltargetType- theMapTypeInformationto be used to unmarshall thisDocument.- Returns:
- the converted
Collectionor array, will never be null. - Since:
- 3.2
-
readMap
protected Map<Object,Object> readMap(MappingMongoConverter.ConversionContext context, org.bson.conversions.Bson bson, TypeInformation<?> targetType) Reads the givenDocumentinto aMap. will recursively resolve nestedMaps as well. Can be overridden by subclasses.- Parameters:
context- must not be nullbson- must not be nulltargetType- theMapTypeInformationto be used to unmarshall thisDocument.- Returns:
- the converted
Map, will never be null. - Since:
- 3.2
-
convertToMongoType
@Nullable public Object convertToMongoType(@Nullable Object obj, @Nullable TypeInformation<?> typeInformation) Description copied from interface:MongoWriterConverts the given object into one Mongo will be able to store natively but retains the type information in case the givenTypeInformationdiffers from the given object type.- Specified by:
convertToMongoTypein interfaceMongoWriter<Object>- Parameters:
obj- can be null.typeInformation- can be null.- Returns:
-
convertToMongoType
- Specified by:
convertToMongoTypein interfaceMongoWriter<Object>
-
maybeConvertList
public List<Object> maybeConvertList(Iterable<?> source, @Nullable TypeInformation<?> typeInformation) -
maybeCallAfterConvert
-
getWriteTarget
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:CodecRegistryProviderGet the underlyingCodecRegistryused by the MongoDB Java driver.- Specified by:
getCodecRegistryin interfaceCodecRegistryProvider- Specified by:
getCodecRegistryin interfaceMongoConverter- Returns:
- never null.
-
with
- Returns:
- new instance of
MappingMongoConverter. Never null. - Since:
- 2.1.6
-
DbRefResolverinstead.