Class MappingRelationalConverter
java.lang.Object
org.springframework.data.relational.core.conversion.AbstractRelationalConverter
org.springframework.data.relational.core.conversion.MappingRelationalConverter
- All Implemented Interfaces:
Aware
,ApplicationContextAware
,EnvironmentCapable
,RelationalConverter
- Direct Known Subclasses:
BasicRelationalConverter
,MappingJdbcConverter
,MappingR2dbcConverter
public class MappingRelationalConverter
extends AbstractRelationalConverter
implements ApplicationContextAware, EnvironmentCapable
RelationalConverter
that uses a
MappingContext
to apply sophisticated mapping of domain objects from
RowDocument
.- Since:
- 3.2
- Author:
- Mark Paluch, Jens Schauder, Chirag Tailor, Vincent Galloy, Chanhyeong Cho
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static interface
MappingRelationalConverter.RelationalPropertyValueProvider
extension to obtain values forAggregatePath
s.protected static interface
Conversion context defining an interface for graph-traversal-based conversion of row documents.protected static class
Conversion context holding references to simpleMappingRelationalConverter.DefaultConversionContext.ValueConverter
andMappingRelationalConverter.DefaultConversionContext.ContainerValueConverter
.protected static final class
PropertyValueProvider
to evaluate a SpEL expression if present on the property or simply accesses the field of the configured sourceRowDocument
.protected class
Projecting variant ofMappingRelationalConverter.ConversionContext
applying mapping-metadata rules from the related entity.protected static interface
ExtendedParameterValueProvider
that can report whether a property value is present and contextualize the instance for specific behavior like projection mapping in the context of a property. -
Constructor Summary
ConstructorDescriptionCreates a newMappingRelationalConverter
given the newRelationalMappingContext
.MappingRelationalConverter
(RelationalMappingContext context, CustomConversions conversions) Creates a newMappingRelationalConverter
given the newRelationalMappingContext
andCustomConversions
. -
Method Summary
Modifier and TypeMethodDescription<T> T
createInstance
(PersistentEntity<T, RelationalPersistentProperty> entity, Function<Parameter<?, RelationalPersistentProperty>, Object> parameterValueProvider) Create a new instance ofPersistentEntity
givenParameterValueProvider
to obtain constructor properties.protected <R> R
doReadProjection
(MappingRelationalConverter.ConversionContext context, RowDocument document, EntityProjection<R, ?> projection) Creates a newMappingRelationalConverter.ConversionContext
.protected Object
getPotentiallyConvertedSimpleRead
(Object value, TypeInformation<?> type) Checks whether we have a custom conversion for the given simple object.getPropertyAccessor
(PersistentEntity<T, ?> persistentEntity, T instance) Return aPersistentPropertyAccessor
to access property values of theinstance
.<M,
D> EntityProjection<M, D> introspectProjection
(Class<M> resultType, Class<D> entityType) Introspect the givenresult type
in the context of theentity type
whether the returned type is a projection and what property paths are participating in the projection.protected <R> MappingRelationalConverter.ProjectingConversionContext
newProjectingConversionContext
(EntityProjection<R, ?> projection) newValueProvider
(RowDocumentAccessor documentAccessor, ValueExpressionEvaluator evaluator, MappingRelationalConverter.ConversionContext context) <R> R
project
(EntityProjection<R, ?> projection, RowDocument document) Apply a projection toRowDocument
and return the projection return typeR
.<R> R
read
(Class<R> type, RowDocument source) Read aRowDocument
into the requestedaggregate type
.protected <S> S
read
(TypeInformation<S> type, RowDocument source) protected <S> S
readAggregate
(MappingRelationalConverter.ConversionContext context, RowDocumentAccessor documentAccessor, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument
.protected <S> S
readAggregate
(MappingRelationalConverter.ConversionContext context, RowDocument document, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument
.protected Object
readCollectionOrArray
(MappingRelationalConverter.ConversionContext context, Collection<?> source, TypeInformation<?> targetType) Reads the givenCollection
into a collection of the givenTypeInformation
.readMap
(MappingRelationalConverter.ConversionContext context, Map<?, ?> source, TypeInformation<?> targetType) readValue
(Object value, TypeInformation<?> type) Read a relational value into the desireddestination type
.void
setApplicationContext
(ApplicationContext applicationContext) writeValue
(Object value, TypeInformation<?> type) Write a property value into a relational type that can be stored natively.Methods inherited from class org.springframework.data.relational.core.conversion.AbstractRelationalConverter
getConversions, getConversionService, getEntityInstantiators, getMappingContext
-
Constructor Details
-
MappingRelationalConverter
Creates a newMappingRelationalConverter
given the newRelationalMappingContext
.- Parameters:
context
- must not be null.
-
MappingRelationalConverter
Creates a newMappingRelationalConverter
given the newRelationalMappingContext
andCustomConversions
.- Parameters:
context
- must not be null.conversions
- must not be null.
-
-
Method Details
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-
getEnvironment
- Specified by:
getEnvironment
in interfaceEnvironmentCapable
-
getConversionContext
Creates a newMappingRelationalConverter.ConversionContext
.- Returns:
- the
MappingRelationalConverter.ConversionContext
.
-
getPropertyAccessor
public <T> PersistentPropertyPathAccessor<T> getPropertyAccessor(PersistentEntity<T, ?> persistentEntity, T instance) Description copied from interface:RelationalConverter
Return aPersistentPropertyAccessor
to access property values of theinstance
.- Specified by:
getPropertyAccessor
in interfaceRelationalConverter
- Parameters:
persistentEntity
- the kind of entity to operate on. Must not benull
.instance
- the instance to operate on. Must not benull
.- Returns:
- guaranteed to be not
null
.
-
introspectProjection
Description copied from interface:RelationalConverter
Introspect the givenresult type
in the context of theentity type
whether the returned type is a projection and what property paths are participating in the projection.- Specified by:
introspectProjection
in interfaceRelationalConverter
- Parameters:
resultType
- the type to project on. Must not be null.entityType
- the source domain type. Must not be null.- Returns:
- the introspection result.
- See Also:
-
project
Description copied from interface:RelationalConverter
Apply a projection toRowDocument
and return the projection return typeR
.Non-projecting
descriptors fall back toregular object materialization
.- Specified by:
project
in interfaceRelationalConverter
- Parameters:
projection
- the projection descriptor, must not be null.document
- must not be null.- Returns:
- a new instance of the projection return type
R
.
-
newProjectingConversionContext
protected <R> MappingRelationalConverter.ProjectingConversionContext newProjectingConversionContext(EntityProjection<R, ?> projection) -
doReadProjection
protected <R> R doReadProjection(MappingRelationalConverter.ConversionContext context, RowDocument document, EntityProjection<R, ?> projection) -
read
Read aRowDocument
into the requestedaggregate type
.- Specified by:
read
in interfaceRelationalConverter
- Type Parameters:
R
- aggregate type.- Parameters:
type
- target aggregate type.source
- sourceRowDocument
.- Returns:
- the converted object.
-
read
-
readAggregate
protected <S> S readAggregate(MappingRelationalConverter.ConversionContext context, RowDocument document, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument
. Can be overridden by subclasses.- Parameters:
context
- must not be nulldocument
- must not be nulltypeHint
- theTypeInformation
to be used to unmarshall thisRowDocument
.- Returns:
- the converted object, will never be null.
-
readAggregate
protected <S> S readAggregate(MappingRelationalConverter.ConversionContext context, RowDocumentAccessor documentAccessor, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument
. Can be overridden by subclasses.- Parameters:
context
- must not be nulldocumentAccessor
- must not be nulltypeHint
- theTypeInformation
to be used to unmarshall thisRowDocument
.- Returns:
- the converted object, will never be null.
-
readMap
protected Map<Object,Object> readMap(MappingRelationalConverter.ConversionContext context, Map<?, ?> source, TypeInformation<?> targetType) Reads the givenRowDocument
into aMap
. will recursively resolve nestedMap
s as well. Can be overridden by subclasses.- Parameters:
context
- must not be nullsource
- must not be nulltargetType
- theMap
TypeInformation
to be used to unmarshall thisRowDocument
.- Returns:
- the converted
Map
, will never be null.
-
readCollectionOrArray
protected Object readCollectionOrArray(MappingRelationalConverter.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 thisRowDocument
.- Returns:
- the converted
Collection
or array, will never be null.
-
createInstance
public <T> T createInstance(PersistentEntity<T, RelationalPersistentProperty> entity, Function<Parameter<?, RelationalPersistentProperty>, Object> parameterValueProvider) Description copied from interface:RelationalConverter
Create a new instance ofPersistentEntity
givenParameterValueProvider
to obtain constructor properties.- Specified by:
createInstance
in interfaceRelationalConverter
- Type Parameters:
T
- the type of entity to create.- Parameters:
entity
- the kind of entity to create. Must not benull
.parameterValueProvider
- a function that provides the value to pass to a constructor, given aParameter
. Must not benull
.- Returns:
- the instantiated entity. Guaranteed to be not
null
.
-
newValueProvider
protected MappingRelationalConverter.RelationalPropertyValueProvider newValueProvider(RowDocumentAccessor documentAccessor, ValueExpressionEvaluator evaluator, MappingRelationalConverter.ConversionContext context) -
readValue
Description copied from interface:RelationalConverter
Read a relational value into the desireddestination type
.- Specified by:
readValue
in interfaceRelationalConverter
- Parameters:
value
- a value as it is returned by the driver accessing the persistence store. May benull
.type
-TypeInformation
into which the value is to be converted. Must not benull
.- Returns:
- The converted value. May 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.- Parameters:
value
- to be converted. May benull
..type
-TypeInformation
into which the value is to be converted. Must not benull
.- Returns:
- the converted value if a conversion applies or the original value. Might return
null
.
-
writeValue
Description copied from interface:RelationalConverter
Write a property value into a relational type that can be stored natively.- Specified by:
writeValue
in interfaceRelationalConverter
- Parameters:
value
- a value as it is used in the object model. May benull
.type
-TypeInformation
into which the value is to be converted. Must not benull
.- Returns:
- The converted value. May be
null
.
-