title: How to persist an object which has nested list of objects
body: <p>I am trying to create a task list application, in Java, in which the tasks can have dependent tasks.  So, the task class looks like this:</p>
  
  <pre><code>public class Task implements Serializable {
  
  private static final long serialVersionUID = -5372927338757312018L;
  private long id;
  private String title;
  private String description;
  private Task parent;
  private Set&lt;Task&gt; children;
  </code></pre>
  
  <p>The tasks are persisted to postgres.  Currently, there are 2 tables:</p>
  
  <p><strong>tasks table</strong>: id, title, description<br>
  <strong>parent_child table</strong>: parent_id, child_id</p>
  
  <p>To deserialize, I retrieve the task in question, construct the object, then I retrieve each of the dependent/child tasks, construct them, and add them to the original task as children.  The challenge is each child task can have its own dependent/child tasks.</p>
  
  <p>Retrieve the task (and whether it has any children):</p>
  
  <pre><code>SELECT id, title, description, start, due, totalhours, completedhours,
  
  (SELECT 
    COUNT(*)
    FROM todo.parent_child 
    WHERE todo.parent_child.parent_id = '201'
    ) AS child_count
  
  FROM todo.tasks 
  WHERE id = '201'
  GROUP BY id, title, description, start, due, totalhours, completedhours;
  </code></pre>
  
  <p>If the child_count is greater than 0, I follow up with additional queries to retrieve the dependent/child tasks.</p>
  
  <p>This is all doable as is, but is there a better approach to modeling and/or persisting the tasks?  More generally, how should objects, with nested lists of objects, be persisted and reconstructed?</p>