title: Play JPA - Error: Removing a detached instance
body: <p>I'm using Play Framework 2.4 and
  I got this error:</p>
  
  <pre><code>[IllegalArgumentException: Removing a detached instance models.Account#8]
   In: (...)\models\Account.java
      JPA.em().remove(this)
  </code></pre>
  
  <p>My code looks like this:</p>
  
  <ul>
  <li><em>ProfileController</em>: <a href="http://pastebin.com/embed_iframe.php?i=257PThm4" rel="nofollow">http://pastebin.com/embed_iframe.php?i=257PThm4</a></li>
  <li><em>Account</em>: <a href="http://pastebin.com/embed_iframe.php?i=WN1WChni" rel="nofollow">http://pastebin.com/embed_iframe.php?i=WN1WChni</a></li>
  </ul>
  
  <hr>
  
  <p>I also tried (as suggested by many other answers to similar questions):</p>
  
  <pre><code>JPA.em.remove(JPA.em().merge(this);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>JPA.em().remove(Account.findById(this.id));
  </code></pre>
  
  <p>or adding <code>@Transactional</code> to the delete() function.</p>
  
  <p>But they both result in</p>
  
  <blockquote>
    <p>RollbackException: Error while committing the transaction</p>
  </blockquote>
  
  <p>.</p>
  
  <h2>UPDATE - My solution:</h2>
  
  <p>It was a mixture of two problems:</p>
  
  <ol>
  <li>I missed a line in the console output saying there was a constraint from another table and that the Account cannot be deleted.</li>
  <li><p>The account instance returned by <code>Component.currentAccount()</code> was a detached instance and I fixed that with:</p>
  
  <p><code>Account current = JPA.em().merge(Component.currentAccount())</code></p></li>
  </ol>