使 jpa 和协程存储库/r2dbc 共同存在于 Spring Boot 应用程序中

问题描述 投票:0回答:2

我正在开发 kotlin/spring boot 应用程序,我的主要目标是使其具有反应性。目前,该应用程序使用普通的

JpaRepository
进行数据访问,我想做的是慢慢地修改存储库以使用
CoroutineCrudRepository
,一一进行。令人惊讶的是,在春天这并不是一件容易的事:

  • 简单地扩展
    CoroutineCrudRepository
    而不是
    JpaRepository
    会导致
    org.springframework.dao.InvalidDataAccessApiUsageException: Reactive Repositories are not supported by JPA. Offending repository is MyReactiveRepository!
  • 所以根据https://docs.spring.io/spring-data/r2dbc/docs/current/reference/html/看来我需要添加
    implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
    implementation("io.r2dbc:r2dbc-postgresql:0.8.13.RELEASE")
    并进行一些更改项目使 jpa 和协程存储库能够在同一个应用程序中共存。

有没有任何指南谈论我的具体场景?非常感谢任何帮助!

更新:

继续努力让这一切顺利进行:

  • 添加了
    implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
    implementation("io.r2dbc:r2dbc-postgresql:0.8.13.RELEASE")
    以便可以使用
    CoroutineCrudRepository
  • 添加了
    @EnableJpaRepositories(basePackages = ...)
    @EnableR2dbcRepositories(basePackages = ...)
    划分不同类型的存储库。

所有这些导致我遇到以下问题:

Exception encountered during context initialization - cancelling refresh attempt:  
org.springframework.beans.factory.UnsatisfiedDependencyException: ...

Error creating bean with name 'someJpaRepository' defined in obfuscated.package.SomeJpaRepository defined in 
  
@EnableJpaRepositories declared on MyApplication: Cannot create inner bean '(inner bean)#6dbdbb69' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';  
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#6dbdbb69': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;  
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

暂时不知道如何解决这个问题,因为我不清楚为什么 jpaEntityManagerFactory 不再自动配置。

spring-boot spring-data-jpa spring-data kotlin-coroutines spring-data-r2dbc
2个回答

0
投票

您可以尝试在同一个

@Import(DataSourceAutoConfiguration.class)
类中添加
@Configuration
来重新启用
entityManagerFactory

来源:https://github.com/spring-projects/spring-boot/issues/28025#issuecomment-920006811

© www.soinside.com 2019 - 2024. All rights reserved.