'R2DBC and Spring Data integration issues

I have spring boot project (version 2.4.6) with spring data dependency (spring-boot-starter-data-jpa) and postgreSQL driver.

In project we are using Hibernate and data repositories, which configured via:

@EnableSpringDataCommons(basePackages = ["..."]) // path to folder with repositories
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableTransactionManagement
@Configuration
class PersistenceConfig

And also I want to add reactive R2DBC. My plan is to use it in one specific place, where we have integration with other system, such communication happened via reactive data streaming. According on it, I need to update some state in database reactivly. That's why I'm added next dependencies:

 <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>

And also, next properties configuration:

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

And finally, I have such data-layer logic service:

@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) }
}

Where ObjectEntity - regular spring data entity. But, unfortunately, I have next error while application startup (inside tests):

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 - Test wrapper with injected beans:

@Service
class TestManager {

    @Autowired
    lateinit var objectRepository: ObjectRepository
    ...
}

And ObjectRepository:

interface ObjectRepository : JpaRepository<ObjectEntity> {
  ...
}

As far as I understand, such issue related to r2dbc and spring data misconfig. But how can I fix it?



Solution 1:[1]

Since you did not post the code of ObjectRepository it’s hard to say what is wrong. However, I do not recommend to use JPA and R2DBC in same project for the same database..it’s a hassle and furthermore this may not give you any advantage. Instead I would recommend to use WebClient to make HTTP calls and use Kotlin Coroutine to fire query in dedicated thread (since you are using Kotlin already). In my opinion this will be better approach. However all this depends on your application i.e. how many queries you are firing after calls and so forth.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nicholasnet