gpt4 book ai didi

java - Play Framework 无法读取数据库配置并注入(inject) JPAApi

转载 作者:行者123 更新时间:2023-12-02 03:31:41 25 4
gpt4 key购买 nike

我正在尝试使用 play 2.5,但失败了。

我的 Controller :

public class HomeController extends Controller {

private JPAApi db;

@Inject
public HomeController(JPAApi api) {
this.db = api;
}

public Result index(String name) {
return ok(views.html.index.render("hello", name, Collections.singletonList("world")));
}
}

我的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="readwritePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>ReadWriteDS</non-jta-data-source>

<properties>
<property name="hibernate.connection.isolation" value="1" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.cache.use_query_cache" value="false" />
</properties>
</persistence-unit>
</persistence>

我的conf\application.conf

jpa.default=readwritePU

db {
#pool = "hikaricp"

default.driver = org.postgresql.Driver
default.url = "jdbc:postgresql://127.0.0.1:5432/mydbname"
default.username = "postgres"
default.password = "123456"
#default = "default"

default.jndiName=ReadWriteDS
}

上面的 application.conf 中未启用和启用池配置 key 的错误:

    No configuration setting found for key 'pool'
Error in custom provider, com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'pool'
while locating play.api.db.DBApiProvider
while locating play.api.db.DBApi
for field at play.api.db.NamedDatabaseProvider.dbApi(DBModule.scala:80)
while locating play.api.db.NamedDatabaseProvider
at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
at play.api.db.DBModule$$anonfun$namedDatabaseBindings$1.apply(DBModule.scala:34):
Binding(interface play.api.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=default)) to ProviderTarget(play.api.db.NamedDatabaseProvider@7af4d3bc)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'pool'

pool has type STRING rather than OBJECT
Error in custom provider, com.typesafe.config.ConfigException$WrongType: application.conf @ file:/E:/workspace2/coup/target/scala-2.11/classes/application.conf: 344: pool has type STRING rather than OBJECT
while locating play.api.db.DBApiProvider
while locating play.api.db.DBApi
for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:27)
at play.db.DefaultDBApi.class(DefaultDBApi.java:27)
while locating play.db.DefaultDBApi
while locating play.db.DBApi
for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
while locating play.db.DBModule$NamedDatabaseProvider
at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
at play.db.DBModule.bindings(DBModule.java:40):
Binding(interface play.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=default)) to ProviderTarget(play.db.DBModule$NamedDatabaseProvider@3ec40700)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
Caused by: com.typesafe.config.ConfigException$WrongType: application.conf @ file:/E:/workspace2/coup/target/scala-2.11/classes/application.conf: 344: pool has type STRING rather than OBJECT

我尝试了很多不同的选项和组合,但我已经没有主意了。谷歌甚至不知道这些错误。游戏停止运行,我必须重新启动它。

最佳答案

尝试

jpa.default=readwritePU

db {
default {
hikaricp {
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource {
serverName = localhost
databaseName = "mydbname"
user = "postgres"
password = "123456"
}
}
jndiName=ReadWriteDS
}
}

关于java - Play Framework 无法读取数据库配置并注入(inject) JPAApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38010986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com