2017-03-09 63 views
0

當我嘗試進行注入時,發現錯誤。 我從official site取得的集成示例。取而代之的"hibernate-entitymanager 「我用"hibernate-core"然而,隨着"hibernate-entitymanager"同樣的錯誤PlayFramework 2.5.12 JPAApi注入錯誤

版本

休眠核心 - 5.2.8

戲 - 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; 

    ... 
} 
+0

所以玩遊戲是調用一些Hibernate方法,它不會再出現在你正在使用的Hibernate版本中。因此,問他們爲什麼在涉嫌使用JPA API時調用專有方法(您不需要做這樣的事情) –

回答

0

我最近的一個項目,與"org.hibernate" % "hibernate-entitymanager" % "5.2.8.Final"

作品每@尼爾 - 斯托克頓的評論,我沒有看到在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包含針對Oracle匹配在我local-db.conf

application.conf設置

​​

Playframework默認情況下使用HikariCP,所以在configura下面重刑適用於HikariCP

本地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") 

**持久性。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有一些配置的變化需要 按https://www.playframework.com/documentation/2.5.x/SettingsJDBC

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

這裏prototype是考慮到的池配置的默認名稱。你可以稱它爲任何東西,我稱之爲oracle在我的配置中

希望它能解決你的問題。