使用Mongodb配置Hibernate“尝试删除EntityManager,但没有设置”

时间:2017-02-12 19:46:44

标签: java mongodb hibernate jpa playframework-2.0

我目前正致力于使用我的java play框架服务器和JPA设置MongoDB。现在我遇到一个错误,任何用@Transactional注释的函数都会收到此错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.IllegalStateException: Tried to remove the EntityManager, but none was set.]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
        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:100)
        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:346)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
        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时才会发生此错误。

我在build.sbt中添加了正确的依赖项:

libraryDependencies += javaJpa
libraryDependencies += "org.hibernate.ogm" % "hibernate-ogm-core" % "5.0.0.Final"
libraryDependencies += "org.hibernate.ogm" % "hibernate-ogm-mongodb" % "5.0.0.Final"

我配置了conf / META-INF / persitence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.ogm.datastore.provider"
                     value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
            <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
            <property name="hibernate.ogm.datastore.port" value="27017"/>
            <property name="hibernate.ogm.datastore.database" value="[DB name here]"/>
            <property name="hibernate.ogm.datastore.safe" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

在conf / application.conf中设置我的默认持久性单元:

jpa.default=defaultPersistenceUnit

这是我的示例方法:

@Transactional()
public Result put() {
    Test tmp = play.libs.Json.fromJson(request().body().asJson(), Test.class);
    JPA.em().persist(tmp);
    return ok(play.libs.Json.toJson(tmp));
}

1 个答案:

答案 0 :(得分:0)

好吧,花了好几个星期,我觉得很愚蠢。问题是交易类型需要是资源本地的,如下所示:

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">