Play 2.5 Ebean and JPA: NoSuchMethodError

*爱你&永不变心* 提交于 2019-12-11 04:54:47

问题


Yet another issue I'm experiencing while migrating my projects to Play 2.5. One of the projects uses both Ebean and JPA. Here is 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
)

Everything was OK in Play 2.2.1. However, now I'm getting the following error when trying to launch the application:

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:

I believe the root cause is that ebean brings in javax.persistence, while jpa brings in org.hibernate.javax.persistence, which leads to conflict. I tried adding the exlude to build.sbt, like this:

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

Or even this:

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

But that did not help: activator still downloads both 2.0 and 2.1 and adds them to classpath. What else can be done here? Is there any way to correctly exclude dependencies?


回答1:


The orphanRemoval method is added since Java Persistence 2.0.

So, you have to remove dependency of javax.persistence % persistence-api % 1.0 comes with PlayEbean

Try add following lines in build.sbt to remove dependency of javax.persistence api 1.0.

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

libraryDependencies ~= (_.map(excludeJPAPersistence))

Here's 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))



回答2:


Thanks for your answer. I've managed to solve the issue by adding the following line to both build.sbt and plugins.sbt:

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


来源:https://stackoverflow.com/questions/38373444/play-2-5-ebean-and-jpa-nosuchmethoderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!