[英]R2DBC and Spring Data integration issues
我有 spring 引导项目(版本2.4.6
),带有 spring 数据依赖项( spring-boot-starter-data-jpa
)和 postgreSQL 驱动程序。
在项目中,我们使用 Hibernate 和数据存储库,通过以下方式配置:
@EnableSpringDataCommons(basePackages = ["..."]) // path to folder with repositories
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableTransactionManagement
@Configuration
class PersistenceConfig
而且我还想添加反应性 R2DBC。 我的计划是在一个特定的地方使用它,我们与其他系统集成,这种通信通过反应数据流发生。 根据它,我需要重新更新数据库中的一些 state。 这就是我添加下一个依赖项的原因:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.4.6</version>
</dependency>
dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
还有,下一个属性配置:
spring:
name: configuration
url: jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DBNAME}
username: ${POSTGRES_USERNAME}
password: ${POSTGRES_PASSWORD}
driverClassName: org.postgresql.Driver
hikari:
maximum-pool-size: 2
jpa:
database: POSTGRESQL
database-platform: org.hibernate.dialect.PostgreSQLDialect
generate-ddl: false
open-in-view: false
properties:
javax:
persistence:
validation:
mode: auto
hibernate:
temp:
use_jdbc_metadata_defaults: false
jdbc:
time_zone: UTC
lob.non_contextual_creation: true
hibernate:
ddl-auto: none
r2dbc:
url: r2dbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DBNAME}
username: ${POSTGRES_USERNAME}
password: ${POSTGRES_PASSWORD}
liquibase.change-log: classpath:/db/changelog-master.xml
最后,我有这样的数据层逻辑服务:
@Service
class MyDataReactiveService(
val operator: TransactionalOperator,
val template: R2dbcEntityTemplate
) {
fun updateObjectStatus(state: String, objectId: UUID): Mono<Int> =
template
.update(ObjectEntity::class.java)
.matching(query(Criteria.where("id").`is`(objectId)))
.apply(update("state", state))
.`as` { operator.transactional(it) }
}
其中ObjectEntity
- 常规 spring 数据实体。 但是,不幸的是,我在应用程序启动时遇到下一个错误(内部测试):
Field objectRepository in com.slandshow.TestManager required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
TestManager
- 带有注入 bean 的测试包装器:
@Service
class TestManager {
@Autowired
lateinit var objectRepository: ObjectRepository
...
}
和ObjectRepository
:
interface ObjectRepository : JpaRepository<ObjectEntity> {
...
}
据我了解,此类问题与 r2dbc 和 spring 数据配置错误有关。 但是我该如何解决呢?
由于您没有发布ObjectRepository
的代码,因此很难说出了什么问题。 但是,我不建议在同一项目的同一数据库中使用JPA
和R2DBC
。这很麻烦,而且这可能不会给您带来任何优势。 相反,我建议使用WebClient
进行HTTP
调用,并使用Kotlin
Coroutine
在专用线程中触发查询(因为您已经在使用Kotlin
)。 在我看来,这将是更好的方法。 然而,所有这一切都取决于您的应用程序,即您在调用后触发了多少查询等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.