2016-02-05 37 views
0

我試圖在Play應用程序中使用JPA配置HikariCP。但我不確定這是否可能。我嘗試了reference.conf中的不同配置參數,它現在連接到數據庫,但Hibernate/JPA無法初始化實體管理器。如何在Play應用程序中使用JPA配置HikariCP

[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=defaultPersistenceUnitapplication.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> 

我使用播放2.4

+0

替換'prototype'用'default' –

+0

@AliDehghani它沒有工作,但我嘗試了不同的方式(我已經更新了我的問題)。現在它找不到JNDI數據源端點 – Switch

+0

您是否添加了「persistence.xml」? –

回答

1

這就是我如何使用JPA的Play 2.4:

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> 
相關問題