Module spring.data.relational
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
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, Lukáš Křečan
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceMappingRelationalConverter.RelationalPropertyValueProviderextension to obtain values forAggregatePaths.protected static interfaceConversion context defining an interface for graph-traversal-based conversion of row documents.protected static classConversion context holding references to simpleMappingRelationalConverter.DefaultConversionContext.ValueConverterandMappingRelationalConverter.DefaultConversionContext.ContainerValueConverter.protected static final classPropertyValueProviderto evaluate a SpEL expression if present on the property or simply accesses the field of the configured sourceRowDocument.protected classProjecting variant ofMappingRelationalConverter.ConversionContextapplying mapping-metadata rules from the related entity.protected static interfaceExtendedParameterValueProviderthat 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
ConstructorsConstructorDescriptionCreates a newMappingRelationalConvertergiven the newRelationalMappingContext.MappingRelationalConverter(RelationalMappingContext context, CustomConversions conversions) Creates a newMappingRelationalConvertergiven the newRelationalMappingContextandCustomConversions. -
Method Summary
Modifier and TypeMethodDescription<T> TcreateInstance(PersistentEntity<T, RelationalPersistentProperty> entity, Function<Parameter<?, RelationalPersistentProperty>, Object> parameterValueProvider) Create a new instance ofPersistentEntitygivenParameterValueProviderto obtain constructor properties.protected <R> RdoReadProjection(MappingRelationalConverter.ConversionContext context, RowDocument document, EntityProjection<R, ?> projection) Creates a newMappingRelationalConverter.ConversionContext.protected ObjectgetPotentiallyConvertedSimpleRead(Object value, TypeInformation<?> type) Checks whether we have a custom conversion for the given simple object.getPropertyAccessor(PersistentEntity<T, ?> persistentEntity, T instance) Return aPersistentPropertyAccessorto access property values of theinstance.<M,D> EntityProjection<M, D> introspectProjection(Class<M> resultType, Class<D> entityType) Introspect the givenresult typein the context of theentity typewhether the returned type is a projection and what property paths are participating in the projection.protected <R> MappingRelationalConverter.ProjectingConversionContextnewProjectingConversionContext(EntityProjection<R, ?> projection) newValueProvider(RowDocumentAccessor documentAccessor, ValueExpressionEvaluator evaluator, MappingRelationalConverter.ConversionContext context) <R> Rproject(EntityProjection<R, ?> projection, RowDocument document) Apply a projection toRowDocumentand return the projection return typeR.<R> Rread(Class<R> type, RowDocument source) Read aRowDocumentinto the requestedaggregate type.protected <S> Sread(TypeInformation<S> type, RowDocument source) protected <S> SreadAggregate(MappingRelationalConverter.ConversionContext context, RowDocumentAccessor documentAccessor, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument.protected <S> SreadAggregate(MappingRelationalConverter.ConversionContext context, RowDocument document, TypeInformation<? extends S> typeHint) Conversion method to materialize an object from adocument.protected ObjectreadCollectionOrArray(MappingRelationalConverter.ConversionContext context, Collection<?> source, TypeInformation<?> targetType) Reads the givenCollectioninto 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.voidsetApplicationContext(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 newMappingRelationalConvertergiven the newRelationalMappingContext.- Parameters:
context- must not be null.
-
MappingRelationalConverter
Creates a newMappingRelationalConvertergiven the newRelationalMappingContextandCustomConversions.- Parameters:
context- must not be null.conversions- must not be null.
-
-
Method Details
-
setApplicationContext
- Specified by:
setApplicationContextin interfaceApplicationContextAware- Throws:
BeansException
-
getEnvironment
- Specified by:
getEnvironmentin 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:RelationalConverterReturn aPersistentPropertyAccessorto access property values of theinstance.- Specified by:
getPropertyAccessorin 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:RelationalConverterIntrospect the givenresult typein the context of theentity typewhether the returned type is a projection and what property paths are participating in the projection.- Specified by:
introspectProjectionin 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:RelationalConverterApply a projection toRowDocumentand return the projection return typeR.Non-projectingdescriptors fall back toregular object materialization.- Specified by:
projectin 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 aRowDocumentinto the requestedaggregate type.- Specified by:
readin 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- theTypeInformationto 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- theTypeInformationto 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 givenRowDocumentinto aMap. will recursively resolve nestedMaps as well. Can be overridden by subclasses.- Parameters:
context- must not be nullsource- must not be nulltargetType- theMapTypeInformationto 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 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 thisRowDocument.- Returns:
- the converted
Collectionor array, will never be null.
-
createInstance
public <T> T createInstance(PersistentEntity<T, RelationalPersistentProperty> entity, Function<Parameter<?, RelationalPersistentProperty>, Object> parameterValueProvider) Description copied from interface:RelationalConverterCreate a new instance ofPersistentEntitygivenParameterValueProviderto obtain constructor properties.- Specified by:
createInstancein 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:RelationalConverterRead a relational value into the desireddestination type.- Specified by:
readValuein interfaceRelationalConverter- Parameters:
value- a value as it is returned by the driver accessing the persistence store. May benull.type-TypeInformationinto 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, appliesEnumhandling or returns the value as is.- Parameters:
value- to be converted. May benull..type-TypeInformationinto 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:RelationalConverterWrite a property value into a relational type that can be stored natively.- Specified by:
writeValuein interfaceRelationalConverter- Parameters:
value- a value as it is used in the object model. May benull.type-TypeInformationinto which the value is to be converted. Must not benull.- Returns:
- The converted value. May be
null.
-