试图删除Entit yManager,但没有设置

时间:2016-09-14 08:40:20

标签: java hibernate jpa playframework

我在Play Framework 2.5(Java)中遇到运行时错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.IllegalStateException: Tried to remove the Entit yManager, but none was set.]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.IllegalStateException: Tried to remove the EntityManager, but none was set.
        at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
        at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
        at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
        at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
        at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
        at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
        at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
        at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: java.lang.IllegalStateException: Tried to remove the EntityManager, but none was set.
        at play.db.jpa.JPAEntityManagerContext.pop(JPAEntityManagerContext.java:74)
        at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:155)
        at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:195)
        at play.db.jpa.TransactionalAction.call(TransactionalAction.java:25)
        at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
        at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
        at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:56)
        at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)

以下是代码:

@Transactional
public Result logincheck(){

    Form<User> loginForm = Form.form(User.class).bindFromRequest();

    User user = loginForm.get();

    User searchUser = UserDao.findUser(user);

    if (searchUser != null){
        return ok(homepage.render());
    }

    return ok(login.render(loginForm));
}

在课程UserDao中:

public static User findUser(User user){

    EntityManager em = jpaApi.em();

    TypedQuery<User> query = JPA.em().createQuery("select u.* from [RL].[dbo].[userdetails] u where u.userid = :username and u.password = :password", User.class);

    query.setParameter("username", user.userid);
    query.setParameter("password", user.password);

    try{
        return (User) query.getSingleResult();


    } catch(NoResultException e){
        return null;

    }


}

2 个答案:

答案 0 :(得分:3)

我们在使用play2.5时发现了这个问题。我们通过添加来修复它 将persistence.xml文件放入项目中,如下所示:Play 2.5 JavaJPA creating a persistence unit

并在application.conf中添加以下内容:

jpa.default=defaultPersistenceUnit

答案 1 :(得分:2)

您在同一方法中使用JPA.em()和jpaApi。实际上这应该是同一个实体经理。从play 2.5开始,正确的方法是jpaApi,不推荐使用JPA.em()。你应该注意注射jpaApi。

代码可能是这样的:

public static User findUser(User user) {
    JPAApi jpaApi = Play.current().injector().instanceOf(JPAApi.class);
    EntityManager em = jpaApi.em();
    TypedQuery<User> query = em.createQuery("select u.* from [RL].[dbo].[userdetails] u where u.userid = :username and u.password = :password", User.class);
    ...
}

您可以在Play

中阅读更多关于依赖注入的here