title: How to flatten nested references into a simple target?
body: <p>I am using spring data jdbc with MapStruct. The POJOs are aligned with the table structure with all "internal-only" data like surrogate keys, audit information etc while the Domain object is hierarchical and contains only business relevant data.
  I have to map between a table specific POJO (flat structure) and a Domain object with nested objects. How can I best do this with MapStruct 1.2.0 ?</p>
  
  <pre><code>// Pojo for table
  public class PersonsRecord {
      @Id
      private Integer surrogateKey; // should not be mapped
  
      private String loginId;
      private String name;
      private Timestamp memberSince;
  
      // address columns
      private Integer houseNumber;
      private String addressStreet;
      private String addressCity;
      private String country;
  
      // getters and setters
  }
  
  // Domain object
  public class PersonDto {
      private String loginId;
      private String name;
      private Timestamp memberSince;
  
      private Address address;
  }
  
  @Mapper(uses = MapperUtils.class)
  public interface PersonMapper {
  
      PersonDto toPersonDto(PersonRecord person);
  
      Address toAddress(PersonRecord person);
  }
  </code></pre>
  
  <p>The generated code for the <code>toPersonDto</code> ignores the <code>Address</code> and does not call the <code>toAddress</code> method like <code>MapStruct</code> reference mentions. May be because both methods takes the same input type? The generated code for <code>toAddress</code> looks good. Just that it is not automatically wired by <code>MapStruct</code> generation.</p>
  
  <p>The reverse mapping generation is similar. No automatic call.</p>
  
  <p>Is there a way to make this automatic (without custom methods, or decorators etc) ? I have similar pattern in many places.</p>
  
  <p>Note : The <code>MapperUtils</code> is used for instantiating DTOs with immutable fields.</p>
  
  <p><strong>Update:</strong> I used the answer by Filip for mapping from <code>PersonRecord</code> to <code>PersonDto</code> including the <code>Person.Address</code>. For the reverse mapping I did the following:</p>
  
  <pre><code>@Mapper(uses = MapperUtils.class)
  public abstract class PersonMapper {
      @Mapping(target = "address", source = "person")
      public abstract PersonDto toPersonDto(PersonRecord person);
  
      protected abstract Address toAddress(PersonRecord person);
  
      public abstract PersonRecord toRecord(PersonDto dto);
  
      public abstract void updateAddress(PersonDto dto, @MappingTarget PersonRecord person);
  
      @AfterMapping
      protected void updateAddress(PersonDto dto, @MappingTarget PersonRecord record) {
          updateAddress(dto, record);
      }
  }
  </code></pre>
  
  <p>That worked without issues. Thanks Filip for providing the direction.</p>