title: Copying relational data with non-trivial relationships
body: <p>Say we have these tables:</p>
  
  <pre class="lang-none prettyprint-override"><code>                  +-------------------------+
                    | Orphanage               |
                    |-------------------------|
                    | ID                      |
                    |                         |
                    +-------------------------+
  
  
        +-------------------------+    +-------------------------+
        | Room                    |    | Orphan                  |
        |-------------------------|    |-------------------------|
        | ID                      |    | ID                      |
        | Orphanage ID            |    | Orphanage ID            |
        |                         |    |                         |
        +-------------------------+    +-------------------------+
  
  
                       +-------------------------+
                       | Room Movement           |
                       |-------------------------|
                       | ID                      |
                       | Orphan ID               |
                       | From Room ID            |
                       | To Room ID              |
                       |                         |
                       +-------------------------+
  </code></pre>
  
  <p><code>Room Movement</code> gets a new entry when an Orphan is moved from one room to another.</p>
  
  <p>Now say I wanted to copy an entire <code>Orphanage</code>, complete with all the related entities.  <code>Room</code> and <code>Orphan</code> are simple enough to copy - save the new Orphanage to create the ID, and pass it to clones of the Room and Orphan - but entries in <code>Room Movement</code> are slightly more complicated.  I would have to maintain a map of old to new IDs for both Orphans and Rooms and do a lookup for each insert.  This is simple in this contrived example, but in reality we have a lot of these kinds of relationships under <code>Orphanage</code>, and nested levels of these FK relationships.</p>
  
  <p>Two questions: is this something the ORM should handle?  This is a problem not unique to our application, and it seems like with properly configured foreign keys and some sort of "clone strategy" system the ORM should do the heavy lifting and do things in the right order.</p>
  
  <p>If not, is there somewhere where I can read up on this problem and see how other people solved it without creating a big mess of code? </p>