如何在Play应用程序中使用JPA配置HikariCP

时间:2016-02-05 08:28:14

标签: playframework

我正在尝试在Play应用程序中使用JPA配置HikariCP。但我不确定这是否可行。我在reference.conf中尝试了不同的配置参数,它现在连接到数据库,但Hibernate / JPA无法初始化实体管理器。

[RuntimeException: No JPA entity manager defined for 'default']  

这是我的reference.conf

play {
  modules {
    enabled += "play.api.db.DBModule"
    enabled += "play.api.db.HikariCPModule"
    enabled += "play.db.jpa.JPAModule"
  }
  # Database configuration
  db {
    default = "default"
    prototype = {
      pool = "hikaricp"
      url = "jdbc:postgresql://localhost:5432/playdb"
      username = postgres
      password = "######"
      jndiName = DefaultDS
      jpaUnit = defaultPersistenceUnit

      # HikariCP configuration options
      hikaricp {
        dataSourceClassName = org.postgresql.ds.PGSimpleDataSource
        autoCommit = true
        connectionTimeout = 30 seconds
        idleTimeout = 10 minutes
        maxLifetime = 30 minutes
        connectionTestQuery = "SELECT 1"
        minimumIdle = null
        maximumPoolSize = 10
        poolName = null
        initializationFailFast = true
        isolateInternalQueries = false
        allowPoolSuspension = false
        readOnly = false
        registerMbeans = false
        catalog = null
        connectionInitSql = null
        transactionIsolation = null
        validationTimeout = 5 seconds
        leakDetectionThreshold = null
      }
    }
  }
}

当我向jpa.prototype=defaultPersistenceUnit添加application.conf时更新它会给我一个不同的问题。

[ProvisionException: Unable to provision, see the following errors: 1) Error injecting constructor, javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory at play.db.jpa.DefaultJPAApi$JPAApiProvider.<init>(DefaultJPAApi.java:35) at play.db.jpa.DefaultJPAApi$JPAApiProvider.class(DefaultJPAApi.java:30) while locating play.db.jpa.DefaultJPAApi$JPAApiProvider while locating play.db.jpa.JPAApi 1 error]

application.conf

# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#

jpa.prototype=defaultPersistenceUnit

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="false"/>   
            <property name="hibernate.ejb.interceptor"  value="configs.AuditLogInterceptor" />        
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="true" />          
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" /> 
            <property name="hibernate.generate_statistics" value="true" />
        </properties>
    </persistence-unit>

</persistence>

我正在使用Play 2.4

1 个答案:

答案 0 :(得分:1)

我如何在Play 2.4中使用JPA:

conf/application.conf

db.default.hikaricp.dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
db.default.hikaricp.dataSource.user=james
db.default.hikaricp.dataSource.password=bond
db.default.hikaricp.dataSource.databaseName=moneypenny
db.default.hikaricp.dataSource.serverName=localhost

db.default.jndiName=DefaultDS

jpa.default=defaultPersistenceUnit

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>
        <validation-mode>NONE</validation-mode>
        <properties>
            <property name="hibernate.dialect" value="jpa.PostgreSQLDialect" />
            <property name="hibernate.implicit_naming_strategy" value="org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl" />
            <!-- etc. -->
        </properties>
    </persistence-unit>

</persistence>