播放2.5 Ebean和JPA:NoSuchMethodError

时间:2016-07-14 11:50:34

标签: java hibernate jpa ebean playframework-2.5

我将项目迁移到Play 2.5时遇到的另一个问题。其中一个项目同时使用Ebean和JPA。这是build.sbt:

name := "Project"

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

scalaVersion := "2.11.8"

routesGenerator := StaticRoutesGenerator

libraryDependencies ++= Seq(
  javaJdbc,
  javaJpa,
  "org.hibernate" % "hibernate-entitymanager" % "4.2.8.Final",
  cache,
  javaWs
)

Play 2.2.1中的一切都很好。但是,现在我在尝试启动应用程序时遇到以下错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ProvisionException: Unable to provision, see the following errors:

    1) Error injecting constructor, java.lang.NoSuchMethodError: javax.persistence.OneToOne.orphanRemoval()Z
      at play.db.jpa.DefaultJPAApi$JPAApiProvider.<init>(DefaultJPAApi.java:39)
      at play.db.jpa.DefaultJPAApi$JPAApiProvider.class(DefaultJPAApi.java:34)
      while locating play.db.jpa.DefaultJPAApi$JPAApiProvider
      while locating play.db.jpa.JPAApi
        for parameter 0 at play.db.jpa.TransactionalAction.<init>(TransactionalAction.java:20)
      while locating play.db.jpa.TransactionalAction

    1 error]]
        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:346)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
        at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40)
        at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248)
        at scala.concurrent.Promise$class.complete(Promise.scala:55)
        at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153)
        at scala.concurrent.Promise$class.failure(Promise.scala:104)
        at scala.concurrent.impl.Promise$DefaultPromise.failure(Promise.scala:153)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:257)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
        at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:91)
        at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91)
        at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91)
        at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
        at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:90)
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:405)
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
    Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:

我认为根本原因是ebean引入了javax.persistence,而jpa引入了org.hibernate.javax.persistence,这导致了冲突。我尝试添加build.sbt的exlude,如下所示:

javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api")

甚至这个:

javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api").exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.1-api")

但这没有帮助:激活器仍然下载2.0和2.1并将它们添加到classpath。还有什么可以在这里完成的?有没有办法正确排除依赖?

2 个答案:

答案 0 :(得分:0)

自Java Persistence 2.0以来添加了orphanRemoval方法。

因此,您必须删除javax.persistence % persistence-api % 1.0附带PlayEbean

的依赖关系

尝试在build.sbt中添加以下行以删除javax.persistence api 1.0的依赖项。

def excludeJPAPersistence(module: ModuleID): ModuleID =
  module.excludeAll(ExclusionRule("javax.persistence","persistence-api"))

libraryDependencies ~= (_.map(excludeJPAPersistence))

这里是build.sbt

name := "Project"

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

scalaVersion := "2.11.8"

routesGenerator := StaticRoutesGenerator

libraryDependencies ++= Seq(
  javaJdbc,
  javaJpa,
  "org.hibernate" % "hibernate-entitymanager" % "4.2.8.Final",
  cache,
  javaWs
)

def excludeJPAPersistence(module: ModuleID): ModuleID =
  module.excludeAll(ExclusionRule("javax.persistence","persistence-api"))

libraryDependencies ~= (_.map(excludeJPAPersistence))

答案 1 :(得分:0)

感谢您的回答。我通过在build.sbt和plugins.sbt中添加以下行来设法解决问题:

excludeDependencies += "javax.persistence" % "persistence-api"