PlayFramework 2.5.12 JPAApi注入错误

时间:2017-03-09 14:04:52

标签: java hibernate jpa inject playframework-2.5

当我尝试注射时,我看到一个错误。 我从official site获取的集成示例。 而不是"hibernate-entitymanager"我使用"hibernate-core"。但是,"hibernate-entitymanager"错误相同。

版本

hibernate-core - 5.2.8

play - 2.5.12

1) Error injecting constructor, java.lang.NoSuchMethodError: org.hibernate.cfg.AnnotationBinder.bindDefaults(Lorg/hibernate/boot/spi/MetadataBuildingContext;)V
  at play.db.jpa.DefaultJPAApi$JPAApiProvider.<init>(DefaultJPAApi.java:54)
  at play.db.jpa.DefaultJPAApi$JPAApiProvider.class(DefaultJPAApi.java:35)
  while locating play.db.jpa.DefaultJPAApi$JPAApiProvider
  while locating play.db.jpa.JPAApi
    for field at controllers.api.UsersApi.jpaApi(UsersApi.java:20)
  at controllers.api.UsersApi.class(UsersApi.java:20)
  while locating controllers.api.UsersApi
    for field at controllers.api.ApiFe.usersApi(ApiFe.java:34)
  while locating controllers.api.ApiFe
    for parameter 27 at router.Routes.<init>(Routes.scala:136)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application
    for parameter 0 at play.DefaultApplication.<init>(DefaultApplication.java:30)
  at play.DefaultApplication.class(DefaultApplication.java:30)
  while locating play.DefaultApplication
  while locating play.Application

1 error]
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:180)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:131)
    at scala.Option.map(Option.scala:146)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:131)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:129)

application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://dev-box-2:3306/localbase?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false"
db.default.user=test
db.default.password=test
db.default.bonecp.logStatements=true
db.default.bonecp.partitionCount=10
db.default.bonecp.maxConnectionsPerPartition=10
db.default.bonecp.minConnectionsPerPartition=1
db.default.bonecp.connectionTimeout=3 seconds

db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" >
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        </properties>
    </persistence-unit>

</persistence>

build.sbt

libraryDependencies ++= Seq(
      javaJdbc,
      javaJpa,
      cache,
      javaWs,
      "org.hibernate" % "hibernate-core" % "5.2.8.Final",
      ...

@Singleton
public class UsersApi implements Mappings {

    @Inject
    JPAApi jpaApi;

    ...
}

1 个答案:

答案 0 :(得分:0)

我最近有一个与"org.hibernate" % "hibernate-entitymanager" % "5.2.8.Final"

一起使用的项目

根据@ niel-stockton的评论,我在DefaultJPAApi类中没有看到任何直接使用hibernate的内容

package play.db.jpa;

import play.db.DBApi;
import play.inject.ApplicationLifecycle;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.persistence.*;

/**
 * Default implementation of the JPA API.
 */
public class DefaultJPAApi implements JPAApi {

以下是我目前的配置。我使用Oracle作为DB

<强> build.sbt

routesGenerator := InjectedRoutesGenerator

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "org.webjars" % "bootstrap" % "3.0.3",
  javaJdbc,
  javaJpa,
  cache,
  json,
  javaWs,
  "com.typesafe.play.modules" %% "play-modules-redis" % "2.5.0" exclude("redis.clients" ,"jedis"),
  "redis.clients" % "jedis" % "2.9.0",
  "org.hibernate" % "hibernate-entitymanager" % "5.2.8.Final",
  "com.google.guava" % "guava" % "21.0",
  "org.opensaml" % "opensaml" % "2.5.3"
)

我的application.conf包含我local-db.conf

中oracle匹配的设置

<强> application.conf

play.db.default = "oracle"

Playframework默认使用HikariCP,因此以下配置适用于HikariCP

我的 local-db.conf

evolutionplugin = disabled

jpa {
  default = devPersistenceUnit
}

db {
  oracle {
    # You can expose this datasource via JNDI if needed (Useful for JPA)
    jndiName = LocalDevDS
    # Set a connection's default autocommit setting
    autocommit = true
    hikaricp {
      dataSourceClassName = oracle.jdbc.pool.OracleDataSource

      dataSource {
        serverName = ${DB_HOST}
        user = ${DB_USER}
        password = ${DB_PASS}
        databaseName = ${DB_NAME}
        portNumber = ${DB_PORT}
        networkProtocol = tcp
        driverType = thin
      }
      maximumPoolSize = 8
      registerMbeans = true
      # 15 minutes
      maxLifetime = 450000
      # 5 minutes
      idleTimeout = 300000
      connectionTimeout = 60000
    }
  }
}

<强>的local.conf

include "application.conf"
include "local-db.conf"

<强> plugins.sbt

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.12")

addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.1.1")

** persistence.xml **

<persistence-unit name="devPersistenceUnit" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  <non-jta-data-source>LocalDevDS</non-jta-data-source>
  <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.show_sql" value="true"></property>
    <property name="hibernate.format_sql" value="true"></property>
  </properties>
</persistence-unit>

如果您想使用BoneCP,则需要进行一些配置更改 根据{{​​3}}

play.db.pool=bonecp
play.db.prototype.bonecp.maxConnectionsPerPartition = 50

此处prototype是池配置的默认名称。您可以将其称为任何内容,我在配置中将其称为oracle

希望它能解决您的问题。