title: Deep Copy in JPA
body: <p>I would like to make a deep copy of an entity in JPA.  I found an interesting discussion here:
  <a href="http://forums.java.net/jive/thread.jspa?messageID=253092&amp;tstart=0" rel="noreferrer">http://forums.java.net/jive/thread.jspa?messageID=253092&amp;tstart=0</a></p>
  
  <p>It sounded like the proposed solution was to set all @Id's to zero.  Here's my basic code:</p>
  
  <pre><code>
  //Start a JPA session.
  EntityManager em= emf.createEntityManager();
  em.getTransaction().begin();
  
  //Get the object I want to copy.
  MyClass myObject=em.find(MyClass.class,id);
  
  //Use reflection to find @Id's and set them to zero for all @OneToMany and @OneToOne relations.
  //TODO:  write the ugly recursive code to do this.
  
  //Hoping this will create a deep copy.
  em.merge(myObject);
  
  //Close the session.
  em.getTransaction().commit();
  em.close();
  </code></pre>
  
  <p>Is this a good strategy?  Might anyone have this TODO code already written that they can share???</p>
  
  <p>Thanks!</p>