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=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>
我使用播放2.4
替換'prototype'用'default' –
@AliDehghani它沒有工作,但我嘗試了不同的方式(我已經更新了我的問題)。現在它找不到JNDI數據源端點 – Switch
您是否添加了「persistence.xml」? –