title: Failed to instantiate instance of destination [subclassDTO]. Ensure that [subclassDTO] has a non-private no-argument constructor
body: <p>Im working on a Springboot App exposing some endpoints that do not return the original Entities but they return their DTO. To map all the entities i am working using "org.modelmapper" version: 0.7.4 and project lombok to avoid implement getters, setters and some another utilities.</p>
  
  <p>Everything worked perfectly until I had to map a list of subclasses on a entity. The source entity is like this (i removed some properties and hibernate annotations because they are not necessary in the example):</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.pfi.repository.entity.sport;
  
  import com.pfi.repository.entity.address.Address;
  import lombok.*;
  
  import java.util.List;
  
  @Getter
  @Setter
  @EqualsAndHashCode
  @AllArgsConstructor
  @NoArgsConstructor
  public class SportPlace {
  
      private Long id;
      private List&lt;AbstractSportField&gt; sportFields;
  }
  </code></pre>
  
  <p>The destination DTO is:</p>
  
  <pre class="lang-java prettyprint-override"><code>package dto.sport;
  
  import lombok.*;
  
  import java.util.List;
  
  @Getter
  @NoArgsConstructor
  @Setter
  public class SportPlaceDTO {
  
      private Long id;
      private List&lt;AbstractSportFieldDTO&gt; sportFields;
  }
  </code></pre>
  
  <p>Both AbstractSportFieldDTO and AbstractSportField are abstract classes that with two possible subclasses:</p>
  
  <pre class="lang-java prettyprint-override"><code>package dto.sport;
  
  import dto.reserve.AbstractReserveDTO;
  
  import java.util.List;
  
  public abstract class AbstractSportFieldDTO {
  
      protected Long id;
      protected List&lt;AbstractReserveDTO&gt; reserves;
      public AbstractSportFieldDTO() {
      }
  
      public AbstractSportFieldDTO(Long id, List&lt;AbstractReserveDTO&gt; reserves) {
          this.id = id;
          this.reserves = reserves;
      }
  
      public Long getId() {
          return id;
      }
  
      public AbstractSportFieldDTO setId(Long id) {
          this.id = id;
          return this;
      }
  
  
      public List&lt;AbstractReserveDTO&gt; getReserves() {
          return reserves;
      }
  
      public AbstractSportFieldDTO setReserves(List&lt;AbstractReserveDTO&gt; reserves) {
          this.reserves = reserves;
          return this;
      }
  }
  </code></pre>
  
  <p>The first DTO subclass is:</p>
  
  <pre class="lang-java prettyprint-override"><code>package dto.sport;
  
  
  import dto.reserve.AbstractReserveDTO;
  
  import java.util.List;
  
  public class ComboSportFieldDTO extends AbstractSportFieldDTO {
  
      private List&lt;SportFieldDTO&gt; sportFields;
  
      public ComboSportFieldDTO(List&lt;SportFieldDTO&gt; sportFields) {
          this.sportFields = sportFields;
      }
  
      public ComboSportFieldDTO(Long id, List&lt;AbstractReserveDTO&gt; reserves, List&lt;SportFieldDTO&gt; sportFields) {
          super(id, reserves);
          this.sportFields = sportFields;
      }
  
      public ComboSportFieldDTO() {
          super();
      }
  
      public List&lt;SportFieldDTO&gt; getSportFields() {
          return sportFields;
      }
  
      public void setSportFields(List&lt;SportFieldDTO&gt; sportFields) {
          this.sportFields = sportFields;
      }
  }
  </code></pre>
  
  <p>The second DTO subclass is:</p>
  
  <pre class="lang-java prettyprint-override"><code>package dto.sport;
  
  import dto.reserve.AbstractReserveDTO;
  
  import java.util.List;
  
  public class SportFieldDTO extends AbstractSportFieldDTO {
  
      private Boolean joineable;
  
      public SportFieldDTO(Long id, List&lt;AbstractReserveDTO&gt; reserves, Boolean joineable) {
          super(id, reserves);
          this.joineable = joineable;
      }
  
      public SportFieldDTO() {
          super();
      }
  
      public Boolean getJoineable() {
          return joineable;
      }
  
      public void setJoineable(Boolean joineable) {
          this.joineable = joineable;
      }
  }
  
  </code></pre>
  
  <p>Then the Entities are:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.pfi.repository.entity.sport;
  
  import com.pfi.repository.entity.reserve.AbstractReserve;
  import java.util.List;
  
  public abstract class AbstractSportField {
  
      protected Long id;
      protected List&lt;AbstractReserve&gt; reserves;
  
      public AbstractSportField(Long id, List&lt;AbstractReserve&gt; reserves) {
          this.id = id;
          this.reserves = reserves;
      }
  
      public AbstractSportField() {
  
      }
  
      public Long getId() {
          return id;
      }
  
      public AbstractSportField setId(Long id) {
          this.id = id;
          return this;
      }
  
      public List&lt;AbstractReserve&gt; getReserves() {
          return reserves;
      }
  
      public AbstractSportField setReserves(List&lt;AbstractReserve&gt; reserves) {
          this.reserves = reserves;
          return this;
      }
  
  }
  
  </code></pre>
  
  <p>The first subclass is:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.pfi.repository.entity.sport;
  
  import com.pfi.repository.entity.reserve.AbstractReserve;
  
  import java.util.List;
  
  public class ComboSportField extends AbstractSportField {
  
      private List&lt;SportField&gt; sportFields;
  
      public ComboSportField(Long id, List&lt;AbstractReserve&gt; reserves, List&lt;SportField&gt; sportFields) {
          super(id, reserves);
          this.sportFields = sportFields;
      }
  
      public ComboSportField(){
          super();
      }
  
      public List&lt;SportField&gt; getSportFields() {
          return sportFields;
      }
  
      public ComboSportField setSportFields(List&lt;SportField&gt; sportFields) {
          this.sportFields = sportFields;
          return this;
      }
  }
  
  </code></pre>
  
  <p>And the last subclass is:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.pfi.repository.entity.sport;
  
  import com.pfi.repository.entity.reserve.AbstractReserve;
  
  import javax.persistence.Entity;
  import java.util.List;
  
  public class SportField extends AbstractSportField {
  
      private Boolean joineable;
  
      public SportField(Long id, List&lt;AbstractReserve&gt; reserves, Boolean joineable) {
          super(id, reserves);
          this.joineable = joineable;
      }
  
      public SportField(){
          super();
      }
  
      public Boolean getJoineable() {
          return joineable;
      }
  
      public SportField setJoineable(Boolean joineable) {
          this.joineable = joineable;
          return this;
      }
  }
  
  </code></pre>
  
  <p>When i try to map an instance of SportPlace to SportPlaceDTO like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>modelMapper.map(sportPlace, SportPlaceDTO.class)
  </code></pre>
  
  <p>That de sportPlace contains the given 6 Sportfields that are instances of subclasses. The model mapper throws (on the 6 elements of the list):</p>
  
  <p>Failed to instantiate instance of destination dto.sport.AbstractSportFieldDTO. Ensure that dto.sport.AbstractSportFieldDTO has a non-private no-argument constructor.</p>
  
  <p>Reading about how can i map subclasses on modelmapper i found setting TypeMap, that can set a specific mapping on a class like this:</p>
  
  <p>modelMapper.createTypeMap(ComboSportField.class, ComboSportFieldDTO.class);</p>
  
  <p>For that reason i created a builder to config the model mapper like this </p>
  
  <pre class="lang-java prettyprint-override"><code>package com.pfi.repository.builder;
  
  import com.pfi.repository.entity.*;;
  import dto.reserve.*;
  import dto.sport.*;
  import org.modelmapper.ModelMapper;
  import org.modelmapper.convention.MatchingStrategies;
  import org.springframework.stereotype.Service;
  
  @Service
  public class MapperBuilder {
  
      public ModelMapper buildModelMapper() {
          ModelMapper modelMapper = new ModelMapper();
          modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
          mapEntitiesToDTO(modelMapper);
          mapDTOToEntities(modelMapper);
          return modelMapper;
      }
  
      private void mapEntitiesToDTO(final ModelMapper modelMapper){
          modelMapper.createTypeMap(ComboSportField.class, ComboSportFieldDTO.class);
          modelMapper.createTypeMap(SportField.class, SportFieldDTO.class);
          modelMapper.createTypeMap(Reserve.class, ReserveDTO.class);
          modelMapper.createTypeMap(AppReserve.class, AppReserveDTO.class);
  
      }
      private void mapDTOToEntities(final ModelMapper modelMapper){
          modelMapper.createTypeMap(ComboSportFieldDTO.class, ComboSportField.class);
          modelMapper.createTypeMap(SportFieldDTO.class, SportField.class);
          modelMapper.createTypeMap(ReserveDTO.class, Reserve.class);
          modelMapper.createTypeMap(AppReserveDTO.class, AppReserve.class);
      }
  }
  
  </code></pre>
  
  <p>And i use the builder when i create the controller like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Autowired
      protected MapperBuilder mapperBuilder;
  
      BaseController(){
          this.getLogger();
      }
  
      @PostConstruct
      public void buildMapper(){
          modelMapper = mapperBuilder.buildModelMapper();
      }
  </code></pre>
  
  <p>I don't know what else I can do to be able map the subclasses.</p>
  
  <p>Does anyone know how to solve this issue?</p>
  
  <p>Thanks!</p>