title: How Would One Find a Dependency-Order list of Tables to Delete for Test Data?
body: <p>I have a test suite that needs to delete any existing data in my local MySQL test instance for a large number (but not all) of the tables in the system.</p>
  
  <pre><code> delete from customer;
  </code></pre>
  
  <p>Of course, <code>customer</code> has quite a few foreign keys to it, so I also have to delete a few more tables...</p>
  
  <pre><code> delete from customer_email;
   delete from customer_phone;
  </code></pre>
  
  <p>And again, these tables have more foreign keys...</p>
  
  <pre><code> delete from customer_email_notification;
   delete from customer_phone_call;
  </code></pre>
  
  <p>etc etc ad nauseum. It's frustrating to have to mentally navigate the entire data model in order to write the test setup. </p>
  
  <p>I am using Hibernate so I was wondering if there was some way to automatically get a priority-order list of tables such that it can be passed directly to  a deletion handler. Ideally:</p>
  
  <pre><code> List&lt;String&gt; dependentTables = foreignKeyGraph.findAll("customer");
   deletionHandler.clean(dependentTables);
  </code></pre>
  
  <p>Is there a Hibernate utility out there that can do this? I was going to write something that traversed our domain's annotations and derived table names that way, but it seems like a lot of work for a fairly common problem. Thanks!</p>