Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring R2DBC DatabaseClient.as(…)

In my spring-boot 2.3 application, I have a simple data method using DatabaseClient:

fun getCurrentTime(): Mono<LocalDateTime> =
    databaseClient
        .execute("SELECT NOW()")
        .asType<LocalDateTime>()
        .fetch()
        .first()
}

With spring-boot 2.4 (and spring 5.3 and spring-data-r2dbc 1.2), org.springframework.data.r2dbc.core.DatabaseClient from spring-data-r2dbc is deprecated in favor of org.springframework.r2dbc.core.DatabaseClient of spring-r2dbc - which has a different API.

Adapting that is pretty much straightforward - with the exception of the kotlin extension asType, which is not a part of the new DatabaseClientExtensions.

fun getCurrentTime(): Mono<LocalDateTime> =
    databaseClient
        .sql("SELECT NOW()")
        .map { row: Row ->
            row.get(0, LocalDateTime::class.java)!!
        }
        .one()

Are those extensions somewhere else or how can I convert using a reified type parameter?

like image 292
Rüdiger Schulz Avatar asked Nov 02 '20 14:11

Rüdiger Schulz


People also ask

What is Spring Data R2DBC?

Spring Data R2DBC applies familiar Spring abstractions and repository support for R2DBC. It makes it easier to build Spring-powered applications that use relational data access technologies in a reactive application stack. Spring Data R2DBC aims at being conceptually easy.

Is R2DBC production ready?

Currently there are a few drivers ready for production, check the R2dbc drivers page for the complete list. H2 database is frequently used in development environment, add the following dependency when using either embedded or file-based H2 database.

Can we use JPA with R2DBC?

JPA cannot deal with reactive repositories such as provided by Spring Data R2DBC. This means you will have to do more things manually when using R2DBC. There are other reactive drivers around such as for example Quarkus Reactive Postgres client (which uses Vert.

What is Spring Data JDBC?

Spring Data JDBC is an object-relational mapping framework for relational databases that aims to avoid most of the complexity of other ORM frameworks. It does that by avoiding features like lazy loading, managed lifecycles of entity objects and caching.

What is spring data r2dbc?

R2DBC stands for Reactive Relational Database Connectivity, an incubator to integrate relational databases using a reactive driver. Spring Data R2DBC applies familiar Spring abstractions and repository support for R2DBC.

What is databaseclient in spring data?

DatabaseClient started its journey in the experimental Spring Data R2DBC project, trying out various approaches. One of them evaluated how close a textual SQL API and an object-mapping API could be brought together. DatabaseClient in Spring Data exposed various API methods such as select ().from ("table").as (targetType).

Is there an as (class) API after migration to spring r2dbc?

There's no as (Class) API after the migration to Spring R2DBC. DatabaseClient started its journey in the experimental Spring Data R2DBC project, trying out various approaches. One of them evaluated how close a textual SQL API and an object-mapping API could be brought together.

What is r2dbc?

R2DBC is the abbreviation for Reactive Relational Database Connectivity, an incubator to integrate relational databases using a reactive driver. Spring Data R2DBC aims at being conceptually easy.


1 Answers

TL;DR

There's no as(Class) API after the migration to Spring R2DBC.

A bit of background

DatabaseClient started its journey in the experimental Spring Data R2DBC project, trying out various approaches. One of them evaluated how close a textual SQL API and an object-mapping API could be brought together. DatabaseClient in Spring Data exposed various API methods such as select().from("table").as(targetType).

It turned out that this functionality is useful but draws certain limitations because the more an API goes into an entity or even aggregate-oriented direction, the more complexity the actual API becomes and at some point, boundaries between simple object mapping and entities (for example, entity lifecycle callbacks) blur.

We decided to introduce R2dbcEntityTemplate as an abstraction for all entity-bound operations to support most common use-cases. Looking at the fluent API that was previously in place, there's still a gap for all use-cases that require ad-hoc SQL queries, aggregations, function calls, etc.

In the meantime, the project proved useful, and we've identified core support classes that could be migrated into Spring Framework 5.3, so Spring Data R2DBC 1.2 could be based on top of Spring R2DBC.

We weren't able to come up with a proper approach while migrating the code. To be fair, DatabaseClient offers almost the same level of abstraction (except for stored procedures) as NamedParameterJdbcTemplate. Spring JDBC ships clearly with several RowMapper implementations such as SingleColumnRowMapper or DataClassRowMapper that would be useful for Spring R2DBC, too.

Final thoughts

From a user-perspective, as(…) sees a lot of demand and we should investigate, how this functionality (or a variant of it) could be surfaced.

like image 64
mp911de Avatar answered Nov 12 '22 03:11

mp911de