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.
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 value NONE.
Kotlin DSL
Auto mapping can be added using the autoMap function.
val mapper =mapper<SimpleEntity, SimpleEntityDisplay> {autoMap(AutoMappingStrategy.BY_NAME) SimpleEntity::name mappedTo SimpleEntityDisplay::fullName}
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.