Objects definition:
public class EntityA {
private String id;
private String anotherField;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAnotherField() {
return anotherField;
}
}
public class ABC {
private final ObjectXYZ identification;
private final String anotherField;
public ABC(ObjectXYZ identification, String anotherField) {
this.identification = identification;
this.anotherField = anotherField;
}
public String getAnotherField() {
return anotherField;
}
public ObjectXYZ getIdentification() {
return identification;
}
}
public class ObjectXYZ
{
private String identification;
public ObjectXYZ(String identification) {
this.identification = identification;
}
public String getIdentification() {
return identification;
}
}
Mapper:
Mapstruct automatically determines the methods or constructors for mapping, you need just specify the right target\source properties.
@Mapper
public interface ObjectMapper
{
@Mapping(target = "identification", source = "entity.id")
@Mapping(target = "anotherField", source = "entity.anotherField")
ABC toABC(EntityA entity);
@Mapping(target = "identification", source = "identification")
ObjectXYZ toObjectXYZ(String identification);
}
Generated code:
public class ObjectMapperImpl implements ObjectMapper {
@Override
public ABC toABC(EntityA entity) {
if ( entity == null ) {
return null;
}
ObjectXYZ identification = null;
String anotherField = null;
identification = toObjectXYZ( entity.getId() );
anotherField = entity.getAnotherField();
ABC aBC = new ABC( identification, anotherField );
return aBC;
}
@Override
public ObjectXYZ toObjectXYZ(String identification) {
if ( identification == null ) {
return null;
}
String identification1 = null;
identification1 = identification;
ObjectXYZ objectXYZ = new ObjectXYZ( identification1 );
return objectXYZ;
}
}