Auto Mapping
Auto map fields without any boiler-plate code.
Auto mapping is used to reduce the amount of boiler-place code required to configure mapping between two classes.
We start with our two classes, our source class SimpleEntity
and our destination class SimpleEntityDisplay
.
Auto Mapping Strategy
Auto mapping supports 3 different strategies:
BY_NAME_AND_TYPE - When a field with the same name and type is available on both the source and destination classes. The field will be mapped automatically.
In our example the fields that will be mapped in this strategy are: id
, email
and telephone
.
BY_NAME - When a field with the same name is available on both the source and destination classes, not necessarily of the same type. The field will be mapped automatically using default transformers.
When using the BY_NAME strategy all fields with the same name and different types must have registered default transformers for the corresponding types. Otherwise, a runtime exception will be thrown.
In our example the fields that will be mapped in this strategy are: id
, email,
telephone
and birthDate
. Note that a default transformer between Date
and Long
must be registered or a runtime exception will be thrown.
NONE - The default strategy. Disables auto mapping completely. All mappings should be added manually.
When a field is mapped manually, either in the source or destination classes, all auto mappings of that field are disabled.
Configuring Auto Mapping
Auto mapping is available in both DSL and annotation mapping.
Annotations
Auto mapping can be added using the @AutoMapping
annotation.
Two things to note:
The
name
field is mapped manually because it has a different name in the target class.The
@AutoMapping
annotation has two attributes:target
- If no target is added then the auto mapping will be configured to any target class. It is possible to add multiple@AutoMapping
annotation for multiple target classes.strategy
- The auto mapping strategy. Default valueNONE
.
Kotlin DSL
Auto mapping can be added using the autoMap
function.
autoMap
function receives the desired auto mapping strategy. It is possible to add any manual mapping to add/change mapping behavior.
Java Builder
Similar to the Kotlin DSL, auto mapping can be added using the autoMap
function.
With the builder, it is also possible to add additional manual mappings to add/change mapping behavior.
Last updated
Was this helpful?