title: How to map oneToMany using ModelMapper
body: <p>I am using <code>springBoot</code>, I want to map <code>OneToMany</code> relationship from parent to child. Directly using <code>entity</code> with <code>eager</code> fetching I get recursive records, therefore, trying using <code>ModelMapper</code> for <code>DTO</code> mapping to <code>Entity</code> but I am not able to figure out how to do it. Please assume <code>getters</code> and <code>setters</code>.</p>
  
  <p><strong>Parent.java</strong></p>
  
  <pre><code>@Entity
  public class Parent {
  
      @Id
      private int parentId;
  
      private String a;
  
      @OneToMany(mappedBy = "parent")
      private Set&lt;Child&gt; child;
  </code></pre>
  
  <p><strong>Child.java</strong></p>
  
  <pre><code>@Entity
  public class Child {
  
      @Id
      private int childId;
  
      private String c;
  
      @ManyToOne
      @JoinColumn(name = "b")
      private Parent parent;
  </code></pre>
  
  <p>I have working repository and servicelayer with <code>findAll</code> method. </p>
  
  <p><strong>ParentDto.java</strong></p>
  
  <pre><code>public class ParentDto {
  
      private String a;
  
      private Set&lt;Child&gt; child;
  </code></pre>
  
  <p><strong>ParentController.java</strong></p>
  
  <pre><code>@RestController
  public class ParentController {
  
      @Autowired
      private ModelMapper modelMapper;
  
      @Autowired
      private ParentService parentService;
  
  
      @RequestMapping(method = RequestMethod.GET, value="/parents" )
      public List&lt;ParentDto&gt; getParents() {
          List&lt;Parent&gt; parents =  parentService.getAll();
          return parents.stream()
                  .map(x-&gt; modelMapper.map(x, ParentDto.class))
                  .collect(Collectors.toList());
      }
  }
  </code></pre>
  
  <p><strong>Error:</strong> While trying to fetch <code>http://localhost:8080/parents</code></p>
  
  <pre><code>.
  .
  ModelMapper mapping errors: 1) Converter org.modelmapper.internal.converter.CollectionConverter@51381583 failed to convert java.util.Set to java.util.Set. 1 error
  org.modelmapper.MappingException: ModelMapper mapping errors:
  
  1) Converter org.modelmapper.internal.converter.CollectionConverter@51381583 failed to convert java.util.Set to java.util.Set.
  
  1 error
      at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380)
      at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:80)
  .
  .
  </code></pre>