Playframework Hibernate JPA无法连接到数据库

时间:2016-06-22 10:31:15

标签: mysql hibernate jpa playframework

我试图配置Hibernate JPA来访问数据库。如果不使用Hibernate JPA,它可以很好地访问数据库,但是当使用Hibernate JPA时它不会。

我遵循本教程:

https://www.playframework.com/documentation/2.5.x/JavaJPA

这些是我的步骤:

build.sbt

//Hibernate JPA 
libraryDependencies ++= Seq(   javaJpa,   "org.hibernate" % "hibernate-entitymanager" % "5.1.0.Final" // replace by your jpa implementation ) 

//JPA 
PlayKeys.externalizeResources := false

//Driver for mysql 
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.36"

CONF / application.conf

    db {
        # You can declare as many datasources as you want.
        # By convention, the default datasource is named `default`

        default.driver = "com.mysql.jdbc.Driver"
        default.url = "jdbc:mysql://localhost:3306/sakila"
        default.username = root
        default.password = "root"

        default.jndiName=DefaultDS
        jpa.default=defaultPersistenceUnit

        default.logSql=true
    }

CONF / META_INF / persistence.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="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        </properties>
      </persistence-unit>
    </persistence>

错误讯息:

! @70fdppp3i - Internal server error, for (GET) [/count] ->

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [jpa]]
        at play.api.Configuration$.configError(Configuration.scala:154)
        at play.api.Configuration.reportError(Configuration.scala:806)
        at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:48)
        at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42)
        at scala.collection.immutable.List.foreach(List.scala:381)
        at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
        at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:72)
        at play.api.db.DBApiProvider.get(DBModule.scala:62)
        at play.api.db.DBApiProvider.get(DBModule.scala:58)
        at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
Caused by: play.api.Configuration$$anon$1: Configuration error[either dataSource or dataSourceClassName is required]
        at play.api.Configuration$.configError(Configuration.scala:154)
        at play.api.PlayConfig.reportError(Configuration.scala:996)
        at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:70)
        at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
        at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:123)
        at play.api.db.DefaultDatabase.dataSource(Databases.scala:121)
        at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)
        at play.api.db.DefaultDatabase.getConnection(Databases.scala:138)
        at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:44)
        at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42)
Caused by: java.lang.IllegalArgumentException: either dataSource or dataSourceClassName is required
        at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:785)
        at play.api.db.HikariCPConfig.toHikariConfig(HikariCPModule.scala:141)
        at play.api.db.HikariCPConnectionPool$$anonfun$1.apply(HikariCPModule.scala:57)
        at play.api.db.HikariCPConnectionPool$$anonfun$1.apply(HikariCPModule.scala:54)
        at scala.util.Try$.apply(Try.scala:192)
        at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:54)
        at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
        at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:123)
        at play.api.db.DefaultDatabase.dataSource(Databases.scala:121)
        at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)

屏幕上显示错误消息:

enter image description here

如何解决?

我正在使用Play framework 2.5

1 个答案:

答案 0 :(得分:2)

我认为您的jpa.default属性位置错误。试试这样:

db {
    # You can declare as many datasources as you want.
    # By convention, the default datasource is named `default`

    default.driver = "com.mysql.jdbc.Driver"
    default.url = "jdbc:mysql://localhost:3306/sakila"
    default.username = root
    default.password = "root"

    default.jndiName=DefaultDS

    default.logSql=true
}

jpa.default=defaultPersistenceUnit