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?
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.
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.
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.
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.
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.
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).
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.
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.
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. 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With