title: Entity not managed after getting it from database
body: <p>I am using Play Framework and currently doing a CRUD of users. I have created a repository for doing operations on the database using the <a href="https://www.playframework.com/documentation/2.6.x/api/java/play/db/jpa/JPAApi.html" rel="nofollow noreferrer">Play Framework JPAApi</a>. The relevant ones are this</p>
  
  <pre><code>&lt;U&gt; U jpaWrapper(Function&lt;EntityManager, U&gt; function) {
      return jpaApi.withTransaction(function);
  }
  
  public CompletionStage&lt;T&gt; update(T entity) {
      return supplyAsync(() -&gt; jpaWrapper(em -&gt; update(em, entity)), executionContext);
  }
  
  private T update(EntityManager em, T entity) {
      em.refresh(entity);
      return entity;
  }
  
  public CompletionStage&lt;Usuario&gt; findById(int id) {
      return supplyAsync(
            () -&gt; jpaWrapper(em -&gt; findById(id, em)),
            executionContext);
  }
  
  private Usuario findById(int id, EntityManager em) {
      return (Usuario) JpaResultHelper.getSingleResultOrNull(
            em.createNamedQuery("Usuario.findById", Usuario.class)
                  .setParameter("id", id)
      );
  }
  </code></pre>
  
  <p>In my controller I am editing the user by first finding it on the repository and after that setting the correct parameters. This is the controller action.</p>
  
  <pre><code>public CompletionStage&lt;Result&gt; editUser() {
      Usuario editedUser = formFactory.form(Usuario.class).bindFromRequest("id", "nombre", "email", "rol").get();
  
      return usuarioRepository.findById(editedUser.getId()).thenCompose( dbUser -&gt; {
          dbUser.setNombre(editedUser.getNombre());
          dbUser.setRol(editedUser.getRol());
          dbUser.setEmail(editedUser.getEmail());
  
          return usuarioRepository.update(dbUser).thenApplyAsync( u -&gt;
                redirect(routes.UserController.listUsers())
          );
      });
  }
  </code></pre>
  
  <p>The problem is that when I call the method the entity appears to be not managed with the following stack trace.</p>
  
  <pre><code>play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.IllegalArgumentException: Entity not managed]]
      at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251)
      at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178)
      at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:382)
      at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:380)
      at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:413)
      at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
      at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
      at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
      at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
      at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
  Caused by: java.util.concurrent.CompletionException: java.lang.IllegalArgumentException: Entity not managed
      at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
      at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
      at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
      at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.lang.IllegalArgumentException: Entity not managed
      at org.hibernate.internal.SessionImpl.fireRefresh(SessionImpl.java:1320)
      at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:1277)
      at models.management.AbstractRepository.update(AbstractRepository.java:54)
      at models.management.AbstractRepository.lambda$null$4(AbstractRepository.java:31)
      at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:142)
      at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:100)
      at models.management.AbstractRepository.jpaWrapper(AbstractRepository.java:40)
      at models.management.AbstractRepository.lambda$update$5(AbstractRepository.java:31)
      at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
      at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
  </code></pre>