title: Parent/child model for DTO
body: <p>I am not sure if this situation would more be related to <em>generics</em> than <em>DTOs</em>, but here it goes:</p>
  
  <p>I have a DTO that represents a <strong>Person</strong>. A <strong>Person</strong> can have as <em>children</em> other <strong>Person</strong>(s) or just <strong>ResourceLink</strong> to those <strong>Person</strong>(s). This means that the child can be of either of the 2 types: <em>Person</em> (the DTO) or the <em>ResourceLink</em>. What type it would be, is determined through a <em>queryParam</em> and consequently logically through the flow follwed. I want to represent them using just <strong><em>ONE</em></strong> collection object and am not aware of the best way to do so.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>public class PersonDTO&lt;T&gt; {
  
      @XmlElementWrapper(name = "children")
      @XmlElement(name = "child")
      List&lt;T&gt; children;
      // other stuff
  
  }
  </code></pre>
  
  <p>With this approach, I need to define the translated type based on an <code>if...else</code> condition.</p>
  
  <p>Earlier I had 2 different collections, one of which remained <strong>NULL</strong>. I also thought of extracting the <em>relationship</em> thing out in a new DTO as ChildrenDTO (not sure if that's a great idea)</p>
  
  <p>I would like to know if there is a best practice for this situation, otherwise, if it is possible to declare a <code>PersonDTO&lt;PersonDTO&gt;</code> or <code>PersonDTO&lt;ResourceLink&gt;</code> depending on a condition.</p>
  
  <p>Thanks in advance!</p>