title: Map an iterable to non-iterable directly to DTO
body: <p>I have been working with MapStruct 1.1 some days now and haven't yet achieved what i need.</p>
  
  <p>What I want to do is the following:
  I want to map a JPA-Entity to a DTO to achieve nicer view binding of my properties in a Primefaces project.</p>
  
  <p>So i have my entities as follows:</p>
  
  <p><strong>Patient.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class PatientEntity implements Serializable {
  
      @Id
      private long patid;
  
      // ...
      // countless properties here
      // ...
  
      private List&lt;PersonEntity&gt; persons;    // contains a history of persons
  
      // getters and setters
  }
  </code></pre>
  
  <p><strong>Person.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class PersonEntity implements Serializable {
  
      @Id
      private long personid; // some id
  
      private String name;
  
      // other properties + getters/setters
  }
  </code></pre>
  
  <p>And i want to map those entities like this:</p>
  
  <p><strong>PatientDto.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class PatientDto {
      private long patientNumber;
  
      private Date birthday;
  
      private PersonDto latestPersonInfo;
  
      // some other properties + getters/setters
  }
  </code></pre>
  
  <p><strong>PersonDto.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class PersonDto {
  
      private String name;
  
      private String surname;
  
      // some other properties + getters/setters
  }
  </code></pre>
  
  <p>I wrote Mapper classes and an utility like it's described in the official examples on GitHub (<a href="https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-nested-bean-mappings" rel="nofollow noreferrer">https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-nested-bean-mappings</a> &amp; <a href="https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-iterable-to-non-iterable" rel="nofollow noreferrer">https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-iterable-to-non-iterable</a>) but when I want to execute my test case (simply creating entities, then call the mapper and then assertEquals() the single properties) I get a <code>NullPointerException</code> for the PersonMapper but the PatientMapper successfully does its work, except for the <code>latestPersonInfo</code> property.</p>
  
  <p>My Mapper and utils look as follows:</p>
  
  <p><strong>LatestPerson.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Qualifier
  @Target(ElementType.METHOD)
  @Retention(RetentionPolicy.SOURCE)
  public @interface LatestPerson {
  
  }
  </code></pre>
  
  <p><strong>PersonUtil.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>// Returns the one person object i need for the further properties
  // returns the latest person object
  public class PersonUtil {
  
      @LatestPerson
      public PersonEntity latest(List&lt;PersonEntity&gt; persons) {
  
          if (persons != null &amp;&amp; !persons.isEmpty()) {
              for (PersonEntity person : persons) {
                  if (person.getPbisdatum() == null) {
                      return person;
                  }
              }
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p><strong>PatientMapper.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Mapper(uses = PersonUtil.class)
  public interface PatientMapper {
  
      PatientMapper INSTANCE = Mappers.getMapper(PatientMapper.class);
  
      @Mappings({
          @Mapping(source = "patnr", target = "patientNumber"),
          // other mappings
          @Mapping(source = "persons", target = "latestPersonInfo", qualifiedBy = LatestPerson.class)
      })
      PatientDto fromPatientEntity(PatientEntity patient);
  
  }
  </code></pre>
  
  <p><strong>PersonMapper.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public interface PersonMapper {
  
      PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
  
      @Mappings({
          @Mapping(source = "panrede", target = "salutation"),
          @Mapping(source = "ptitel", target = "title"),
          @Mapping(source = "pvorname", target = "name"),
          // other mappings
      })
      PersonDto fromPersonEntity(PersonEntity person);
  
      @InheritInverseConfiguration
      PersonEntity toPersonEntity(PersonDto personDto);
  
  }
  </code></pre>
  
  <p>So, I thought that nested properties get automatically converted but it looks like that they don't in this case, I also get a compiler error that tells me that I need a mapping method for the conversion: <code>PersonDto map(List&lt;PersonEntity&gt; value)</code> but I thought, that i already provided this with the use of the utils.</p>
  
  <p>If someone has an idea please tell me or provide tips to implement a good workaround.</p>
  
  <p>Note:
  The application only compiles if I change my <em>PersonUtil</em> to return a PersonDto and use the mapper that I wrote explicitly but then the NullPointerException gets thrown because my PersonMapper can't be instantiated.</p>