从数据库获取实体后未对其进行管理

时间:2019-01-15 10:19:39

标签: java jpa playframework repository

我正在使用Play Framework,目前正在对用户进行CRUD。我创建了一个存储库,用于使用Play Framework JPAApi对数据库进行操作。相关的是这个

<U> U jpaWrapper(Function<EntityManager, U> function) {
    return jpaApi.withTransaction(function);
}

public CompletionStage<T> update(T entity) {
    return supplyAsync(() -> jpaWrapper(em -> update(em, entity)), executionContext);
}

private T update(EntityManager em, T entity) {
    em.refresh(entity);
    return entity;
}

public CompletionStage<Usuario> findById(int id) {
    return supplyAsync(
          () -> jpaWrapper(em -> findById(id, em)),
          executionContext);
}

private Usuario findById(int id, EntityManager em) {
    return (Usuario) JpaResultHelper.getSingleResultOrNull(
          em.createNamedQuery("Usuario.findById", Usuario.class)
                .setParameter("id", id)
    );
}

在我的控制器中,我正在编辑用户,方法是先在存储库中找到该用户,然后设置正确的参数。这是控制器的动作。

public CompletionStage<Result> editUser() {
    Usuario editedUser = formFactory.form(Usuario.class).bindFromRequest("id", "nombre", "email", "rol").get();

    return usuarioRepository.findById(editedUser.getId()).thenCompose( dbUser -> {
        dbUser.setNombre(editedUser.getNombre());
        dbUser.setRol(editedUser.getRol());
        dbUser.setEmail(editedUser.getEmail());

        return usuarioRepository.update(dbUser).thenApplyAsync( u ->
              redirect(routes.UserController.listUsers())
        );
    });
}

问题是,当我调用该方法时,该实体似乎无法通过以下堆栈跟踪进行管理。

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)

0 个答案:

没有答案