java - Micronaut,postgres 问题
问题描述
按照以下链接
https://guides.micronaut.io/micronaut-data-access-jpa-hibernate/guide/index.html
我尝试连接 postgresql,在运行测试时,所有测试都通过了,但是当尝试调用端点http://localhost:8080/genres/1
它给出了以下错误:
{
"message": "Internal Server Error: Failed to inject value for parameter [entityManager] of class: example.micronaut.$GenreRepositoryImplDefinition$Intercepted\n\nMessage: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check)
and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).\nPath Taken:
new $GenreControllerDefinition$Intercepted([GenreRepository genreRepository],BeanContext beanContext,Qualifier qualifier,Interceptor[] interceptors) -->
new $GenreRepositoryImplDefinition$Intercepted([EntityManager entityManager],ApplicationConfiguration applicationConfiguration,BeanContext beanContext,Qualifier qualifier,Interceptor[] interceptors)"
}
解决方案
确保您正确指定了要扫描实体的包列表。因此,当您在my.project.entity
包中有实体类时,您必须将此部分添加到application.yaml
配置中:
jpa:
default:
entity-scan:
packages: 'my.project.entity'
当然,在application.yaml文件中需要正确的数据源配置,请确保您将数据源命名为default:
datasources:
default:
url: jdbc:postgresql://localhost:5432/micronaut
driverClassName: org.postgresql.Driver
username: postgres
password: postgres
schema-generate: CREATE_DROP
另一个原因可能是缺少对 JDBC 配置的依赖,它为您的项目预先配置了一个简单的 JDBC 连接。因此,请确保将其添加到build.gradle
文件中。您可以使用micronaut-jdbc-hikari
例如:
dependencies {
runtime("io.micronaut.sql:micronaut-jdbc-hikari")
...
}
或者您可以使用micronaut-jdbc-tomcat
,micronaut-jdbc-dbcp
或micronaut-jdbc-ucp
.
还要确保您的所有存储库接口都由@Repository
注释注释,实体类由@Entity
注释注释。但没有它,它将无法编译。
推荐阅读
- graphql - GQL 架构不返回嵌套对象
- neo4j - 我可以在 neo4j 的同一个密码查询中使用“USING SCAN ...”和“USING INDEX ...”吗?
- reactjs - 无法访问 Gatsby 页面查询中的对象数据
- angular - 在角度 6 中添加加载更多按钮
- wpf - 如何将 UI 控件的双精度数据值格式化为数字,例如 1234 到 1.23K?
- python - Keras 图像数据增强
- ios - 如何使用一个不是 tableview 成员的开关禁用我的 tableview 单元格的所有文本字段?
- r - 在 R 的当前目录中创建嵌套文件夹?
- docker - 如果 docker 中的网络没有被移除会发生什么?
- php - PHP & MySqli - 即使文本框中有其他字符,如何搜索和处理“LIKE”?