首页 > 解决方案 > 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)"
}

标签: javapostgresqlgradlemicronaut

解决方案


确保您正确指定了要扫描实体的包列表。因此,当您在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-dbcpmicronaut-jdbc-ucp.


还要确保您的所有存储库接口都由@Repository注释注释,实体类由@Entity注释注释。但没有它,它将无法编译。


推荐阅读