我想在用Kotlin编写的Micronaut Web应用程序中使用两个数据源,一个是MySQL数据库,另一个是内存中的H2数据库,用于执行测试。
当我执行测试(代码中的
这是完整的日志信息:
完整的项目代码在此处可访问:https://github.com/octaviospain/bookie-server/tree/feature/jpa-and-hibernate,若要重现上述日志,请执行
注:根据Ivan Lopez的建议,我已更新了描述,但错误和预期结果仍然相同。
我尝试只使用h2
数据库作为默认的生产数据源,因此与其相关的配置字段应该是正确的。
通过执行./gradlew run
,mysql数据源下应用程序可以正常运行。
- 我尝试将数据源放置在不同的配置文件中,
application.yml
和application-test.yml
分别放在src/main/resources/
目录中,也仅将application-test.yml
放置在src/test/resources/
中,但都没有结果。 - 添加
@MicronautTest
注释,在那里声明test
环境和application = BookieSparaerverApplication
参数无效。 - 向
BookieServerApplication
添加@TypeHint(Genre::class, Book::class, DefaultGenreRepository::class)
注释无效。
这些是src/main/resources/application.yml
配置文件的相关字段。
datasources:
default:
url: 'jdbc:mysql://localhost:3306/bookie-server?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false'
dbCreate: create-update
driverClassName: com.mysql.cj.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: root
password: root
jpa:
default:
entity-scan:
packages:
- 'com.transgressoft.bookieserver.domain'
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
这是 src/test/resources/application-test.yml
文件:
datasources:
default:
url: ${JDBC_URL:`jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE`}
username: ${JDBC_USER:sa}
password: ${JDBC_PASSWORD:""}
driverClassName: ${JDBC_DRIVER:org.h2.Driver}
jpa:
default:
entity-scan:
packages:
- 'com.transgressoft.bookieserver.domain'
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
当我执行测试(代码中的
GenreControllerSpec
),看起来EntityManager
bean没有被创建,因此DAO类无法实例化(在我的代码中是GenreRepository
类)。这是完整的日志信息:
Internal Server Error: Failed to inject value for parameter [entityManager] of class: com.transgressoft.bookieserver.domain.$DefaultGenreRepositoryDefinition$Intercepted
Message: 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).
Path Taken: new $GenreControllerDefinition$Intercepted([GenreRepository genreRepository],BeanContext beanContext,Interceptor[] interceptors) --> new $DefaultGenreRepositoryDefinition$Intercepted([EntityManager entityManager],ConfigurationProperties applicationConfiguration,BeanContext beanContext,Interceptor[] interceptors)
io.micronaut.http.client.exceptions.HttpClientResponseException: Internal Server Error: Failed to inject value for parameter [entityManager] of class: com.transgressoft.bookieserver.domain.$DefaultGenreRepositoryDefinition$Intercepted
完整的项目代码在此处可访问:https://github.com/octaviospain/bookie-server/tree/feature/jpa-and-hibernate,若要重现上述日志,请执行
./gradlew test
并打开报告,或者开启日志运行测试。注:根据Ivan Lopez的建议,我已更新了描述,但错误和预期结果仍然相同。
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
错误。 - Sap