R2DBC (Reactive Relational Database Connectivity) is a separate specification for working with relational databases using reactive types. It provides a nonblocking alternative to JDBC and is built on Reactive Streams rather than being part of Java SE. It can be used with databases including MySQL, PostgreSQL, H2, and Oracle. section 13.1
Spring Data R2DBC adds automatic repository support. To use it in a Spring Boot application, add the Spring Data R2DBC starter, a relational database, and that database’s R2DBC driver. For an in-memory H2 setup, both the H2 library and its R2DBC driver are needed. section 13.1
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
This Maven dependency adds the Spring Data R2DBC starter; it does not replace the database and driver dependencies. section 13.1
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>runtime</scope>
</dependency>
These runtime dependencies supply the H2 database and its matching R2DBC driver. section 13.1
A practical limit appears when an order contains tacos: Spring Data R2DBC does not directly persist relationships in domain classes. The order can hold a taco collection for application use, but marking it `@Transient` prevents R2DBC from trying to persist that collection. Taco IDs are stored separately. section 13.1.4 section 13.4
Listing 13.8 Adding a Taco collection to TacoOrder
@Data
public class TacoOrder {
...
@Transient
private transient List<Taco> tacos = new ArrayList<>();
public void addTaco(Taco taco) {
tacos.add(taco);
if (taco.getId() != null) {
tacoIds.add(taco.getId());
}
}
}
The order keeps tacos in a transient collection while `addTaco()` also records each saved taco’s ID. section 13.1.4
Load an order with its tacos
A service can fetch an order through its repository, look up tacos using the stored IDs, and add them to the order before returning a `Mono<TacoOrder>`. `flatMap()` keeps the result as a `Mono<TacoOrder>` when the lookup itself produces a reactive result, rather than nesting one `Mono` inside another. section 13.1.4
The takeaway: R2DBC enables nonblocking relational persistence, while Spring Data R2DBC supplies repositories. Plan to handle domain relationships explicitly rather than expecting them to be persisted automatically. section 13.1 section 13.4
