【问题标题】:Micronaut, postgres issueMicronaut,postgres 问题
【发布时间】:2020-10-24 02:02:30
【问题描述】:

点击以下链接 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)"
}

【问题讨论】:

    标签: java postgresql gradle micronaut


    【解决方案1】:

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


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

    【讨论】:

      【解决方案2】:

      使用https://micronaut.io/launch/ 创建项目并选择所需的功能。它会自动给出所有依赖项。

      提供正确的数据库凭据。

      终于成功了!

      【讨论】:

        猜你喜欢
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        • 2020-09-14
        • 1970-01-01
        相关资源
        最近更新 更多