We can now start adding our annotations to the SimpleEntity class. In this example, we want to map the name and description fields to the name and description fields of the SimpleEntityDisplay class, but not the privateData field.
To achieve this, we will use the @MappedField annotation on both of these fields. Additionally, we will define @DefaultMappingTarget on the SimpleEntity class, which will indicate that all fields annotated with @MappedField that do not specify a target should be mapped to the SimpleEntityDisplay class.
@DefaultMappingTarget(SimpleEntityDisplay.class)publicclassSimpleEntity { @MappedFieldprivateString name; @MappedFieldprivateString description;privateString privateData;// Getters and Setters...}
Default Mapping Target
@DefaultMappingTarget on a class, indicates that all fields annotated with @MappedField that do not specify a target should be mapped to this class by default.
Mapping Target
The mapping target comes into play when you want to map a single source to multiple destinations. The target attribute is used to indicate to which class the field should be mapped. If no target is specified the target will be determined by the @DefaultMappingTarget on the class.
In the above code name is mapped once to the SimpleEntityDisplay class using the default mapping target, and once to the SimpleEntityExport class using the target attribute.
publicclassSimpleEntityDisplay {privateString fullName ="";// Getters and Setters...}
Map From Field
The field name to map the value from.
Field Level
When mapFrom is used at the field level, it allows for mapping of nested values.
@DefaultMappingTarget(AdvancedEntityDisplay::class)dataclassAdvancedEntity(// This field will be mapped to the "firstChildName" field in the default target class@MappedField(mapFrom ="childName", mapTo ="firstChildName")val firstChild: AdvancedChildEntity,// This field will be mapped to the "secondChildName" field in the default target class@MappedField(mapFrom ="childName", mapTo ="secondChildName")val secondChild: AdvancedChildEntity)
@DefaultMappingTarget(AdvancedEntityDisplay.class)publicclassAdvancedEntity {// This field will be mapped to the "firstChildName" field in the default target class @MappedField(mapFrom ="childName", mapTo ="firstChildName")privateAdvancedChildEntity firstChild;// This field will be mapped to the "secondChildName" field in the default target class @MappedField(mapFrom ="childName", mapTo ="secondChildName")privateAdvancedChildEntity secondChild;// Getters and Setters...}
As you can see above, we use the mapFrom attribute to access the childName field in AdvancedChildEntity.
publicclassAdvancedEntityDisplay {privateString firstChildName ="";privateString secondChildName ="";// Getters and Setters...}
Type Level
The @MappedField annotation can also be used at the type level. When used at the type level, the mapFrom attribute is required to indicate the name of the field to use, if left empty an exception will be thrown.
publicclassSimpleEntityDisplay {privateString firstName ="";privateString lastName ="";// Getters and Setters...}
Transformer
Field transformers are a way to transform a field from one type to another when mapping it to a destination class. More about the ins-and-outs of transformers is available here:
To configure a transformer for a field use the transformer attribute. The transformer attribute receives the transformer's class.
publicclassSimpleEntityDisplay {privateList<String> stringList =newArrayList<>();// Getters and Setters...}
Transformers must be registered to the ShapeShift instance in order to be used. More info about registering transformers is available in the transformers page.
Auto Mapping
Auto mapping is used to reduce the amount of boiler-place code required to configure mapping between two classes. More info about auto mapping is available here:
Auto mapping can be added using the @AutoMapping annotation.
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.