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
Modifier and TypeClassDescriptionprotected static interface
Conversion context defining an interface for graph-traversal-based conversion of documents.protected static class
Conversion context holding references to simpleMappingMongoConverter.DefaultConversionContext.ValueConverter
andMappingMongoConverter.DefaultConversionContext.ContainerValueConverter
. -
Field Summary
Modifier and TypeFieldDescriptionprotected ApplicationContext
static final TypeInformation<org.bson.conversions.Bson>
protected CodecRegistryProvider
protected final org.springframework.data.mongodb.core.convert.DefaultDbRefProxyHandler
protected final DbRefResolver
protected Environment
protected final QueryMapper
protected static final Log
protected String
protected final MappingContext<? extends MongoPersistentEntity<?>,
MongoPersistentProperty> protected final ReferenceLookupDelegate
protected MongoTypeMapper
Fields inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
conversions, conversionService, instantiators
-
Constructor Summary
ConstructorDescriptionMappingMongoConverter
(DbRefResolver dbRefResolver, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) MappingMongoConverter
(MongoDatabaseFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addCustomTypeKeyIfNecessary
(TypeInformation<?> type, Object value, org.bson.conversions.Bson bson) Adds custom type information to the givenDocument
if 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 givenTypeInformation
differs from the given object type.createCollection
(Collection<?> collection, MongoPersistentProperty property) Writes the givenCollection
using the givenMongoPersistentProperty
information.protected com.mongodb.DBRef
createDBRef
(Object target, MongoPersistentProperty property) protected org.bson.conversions.Bson
createMap
(Map<Object, Object> map, MongoPersistentProperty property) Writes the givenMap
using the givenMongoPersistentProperty
information.org.bson.codecs.configuration.CodecRegistry
Get the underlyingCodecRegistry
used by the MongoDB Java driver.protected MappingMongoConverter.ConversionContext
getConversionContext
(org.springframework.data.mongodb.core.convert.ObjectPath path) Creates a newMappingMongoConverter.ConversionContext
givenObjectPath
.Returns theCustomConversions
for this converter.MappingContext<? extends MongoPersistentEntity<?>,
MongoPersistentProperty> protected Object
getPotentiallyConvertedSimpleRead
(Object value, TypeInformation<?> target) Checks whether we have a custom conversion for the given simple object.Returns theProjectionFactory
for this converter.Returns theTypeMapper
being used to write type information intoDocument
s created with that converter.Class<?>
getWriteTarget
(Class<?> source) Get the conversion target type if defined or return the source.protected <T> T
maybeCallAfterConvert
(T object, org.bson.Document document, String collection) maybeConvertList
(Iterable<?> source, TypeInformation<?> typeInformation) 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.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.void
preserveMapKeys
(boolean preserve) Ifpreserve
is set to true the conversion will treat map keys containing dot (.) characters as is.<R> R
project
(EntityProjection<R, ?> projection, org.bson.conversions.Bson bson) Apply a projection toBson
and return the projection return typeR
.<S> S
protected <S> S
read
(TypeInformation<S> type, org.bson.conversions.Bson bson) protected Object
readCollectionOrArray
(MappingMongoConverter.ConversionContext context, Collection<?> source, TypeInformation<?> targetType) Reads the givenCollection
into a collection of the givenTypeInformation
.protected <S> S
readDocument
(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) void
setApplicationContext
(ApplicationContext applicationContext) void
setCodecRegistryProvider
(CodecRegistryProvider codecRegistryProvider) Configure aCodecRegistryProvider
that provides native MongoDBcodecs
for reading values.void
setEntityCallbacks
(EntityCallbacks entityCallbacks) void
setMapKeyDotReplacement
(String mapKeyDotReplacement) Configure the characters dots potentially contained in aMap
shall be replaced with.void
setTypeMapper
(MongoTypeMapper typeMapper) Configures theMongoTypeMapper
to be used to add type information toDocument
s created by the converter and how to lookup type information fromDocument
s when reading them.com.mongodb.DBRef
toDBRef
(Object object, MongoPersistentProperty referringProperty) Creates aDBRef
to refer to the given object.toDocumentPointer
(Object source, MongoPersistentProperty referringProperty) Creates a theDocumentPointer
representing the link to another entity.with
(MongoDatabaseFactory dbFactory) void
Root entry method into write conversion.protected void
writeInternal
(Object obj, org.bson.conversions.Bson bson, MongoPersistentEntity<?> entity) protected void
writeInternal
(Object obj, org.bson.conversions.Bson bson, TypeInformation<?> typeHint) Internal write conversion method which should be used for nested invocations.protected org.bson.conversions.Bson
writeMapInternal
(Map<Object, Object> obj, org.bson.conversions.Bson bson, TypeInformation<?> propertyType) protected void
writePropertyInternal
(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, setInstantiators
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.data.mongodb.CodecRegistryProvider
getCodecFor, hasCodecFor
Methods inherited from interface org.springframework.data.mongodb.core.convert.MongoConverter
convertId, mapValueToTargetType
Methods 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 aDbRefResolver
instead.- 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.ConversionContext
givenObjectPath
.- Parameters:
path
- the currentObjectPath
, must not be null.- Returns:
- the
MappingMongoConverter.ConversionContext
.
-
setTypeMapper
Configures theMongoTypeMapper
to be used to add type information toDocument
s created by the converter and how to lookup type information fromDocument
s when reading them. Uses aDefaultMongoTypeMapper
by default. Setting this to null will reset theTypeMapper
to the default one.- Parameters:
typeMapper
- the typeMapper to set. Can be null.
-
getTypeMapper
Description copied from interface:MongoConverter
Returns theTypeMapper
being used to write type information intoDocument
s created with that converter.- Specified by:
getTypeMapper
in interfaceMongoConverter
- Returns:
- will never be null.
-
getProjectionFactory
Description copied from interface:MongoConverter
Returns theProjectionFactory
for this converter.- Specified by:
getProjectionFactory
in interfaceMongoConverter
- Returns:
- will never be null.
-
getCustomConversions
Description copied from interface:MongoConverter
Returns theCustomConversions
for this converter.- Specified by:
getCustomConversions
in interfaceMongoConverter
- Returns:
- will never be null.
-
setMapKeyDotReplacement
Configure the characters dots potentially contained in aMap
shall be replaced with. By default, we don't do any translation but rather reject aMap
with 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)
.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.
-
preserveMapKeys
public void preserveMapKeys(boolean preserve) Ifpreserve
is set to true the conversion will treat map keys containing dot (.) characters as is.- Since:
- 4.2
- See Also:
-
setCodecRegistryProvider
Configure aCodecRegistryProvider
that provides native MongoDBcodecs
for reading values.- Parameters:
codecRegistryProvider
- can be null.- Since:
- 2.2
-
getMappingContext
public MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()- Specified by:
getMappingContext
in interfaceEntityConverter<MongoPersistentEntity<?>,
MongoPersistentProperty, Object, org.bson.conversions.Bson>
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-
getEnvironment
- Specified by:
getEnvironment
in interfaceEnvironmentCapable
-
setEntityCallbacks
Set theEntityCallbacks
instance to use when invokingcallbacks
like 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:MongoConverter
Apply a projection toBson
and return the projection return typeR
.Non-projecting
descriptors fall back toregular object materialization
.- Specified by:
project
in 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:
read
in 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
- theTypeInformation
to 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:MongoWriter
Creates aDBRef
to refer to the given object.- Specified by:
toDBRef
in interfaceMongoWriter<Object>
- Parameters:
object
- the object to create aDBRef
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 theDBRef
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 theDocumentPointer
representing the link to another entity.- Specified by:
toDocumentPointer
in 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 theDBRef
object 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:
write
in 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 givenCollection
using the givenMongoPersistentProperty
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 givenMap
using the givenMongoPersistentProperty
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, 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 givenDocument
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
Checks whether we have a custom conversion for the given simple object. Converts the given value if so, appliesEnum
handling 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 givenCollection
into a collection of the givenTypeInformation
. Can be overridden by subclasses.- Parameters:
context
- must not be nullsource
- must not be nulltargetType
- theMap
TypeInformation
to be used to unmarshall thisDocument
.- 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, TypeInformation<?> targetType) Reads the givenDocument
into aMap
. will recursively resolve nestedMap
s as well. Can be overridden by subclasses.- Parameters:
context
- must not be nullbson
- must not be nulltargetType
- theMap
TypeInformation
to 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:MongoWriter
Converts the given object into one Mongo will be able to store natively but retains the type information in case the givenTypeInformation
differs from the given object type.- Specified by:
convertToMongoType
in interfaceMongoWriter<Object>
- Parameters:
obj
- can be null.typeInformation
- can be null.- Returns:
-
convertToMongoType
- Specified by:
convertToMongoType
in 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:CodecRegistryProvider
Get the underlyingCodecRegistry
used by the MongoDB Java driver.- Specified by:
getCodecRegistry
in interfaceCodecRegistryProvider
- Specified by:
getCodecRegistry
in interfaceMongoConverter
- Returns:
- never null.
-
with
- Returns:
- new instance of
MappingMongoConverter
. Never null. - Since:
- 2.1.6
-
DbRefResolver
instead.