I am using spring-boot-starter-data-r2dbc
(version 1.1.3) module in Spring Webflux application.
I want to add entity lifecycle callbacks to my persistence layer.
With Spring Data JPA it was possible with annotations like @PrePersist
, @PreUpdate
, etc.
Is there any convenient way to achieve this with Spring Data r2dbc?
Asked
Active
Viewed 1,741 times
4

fyrkov
- 2,245
- 16
- 41
2 Answers
5
Starting from the spring-data-r2dbc:1.2.0
which is a part of the new Spring Data 2020.0 release it is possible with the new "Lifecycle Entity Callback API".
Here is a short example:
import org.springframework.data.r2dbc.mapping.event.BeforeSaveCallback;
@Component
public class DefaultingEntityCallback implements BeforeSaveCallback<MyEntity> {
@Override
public Publisher<MyEntity> onBeforeSave(final MyEntity entity,
final OutboundRow row,
final SqlIdentifier table) {
// do something
return Mono.just(entity);
}
}
Here is some documentation: https://docs.spring.io/spring-data/r2dbc/docs/current/reference/html/#r2dbc.entity-callbacks

fyrkov
- 2,245
- 16
- 41
0
From the docs
Spring Data R2DBC aims at being conceptually easy. In order to achieve this it does NOT offer caching, lazy loading, write behind or many other features of ORM frameworks. This makes Spring Data R2DBC a simple, limited, opinionated object mapper.
So you'll have to either write your own such mechanisms or write persistence code that doesn't depend on them.

Kayaman
- 72,141
- 5
- 83
- 121
-
I am not sure this high-level description rejects the existence of `@PrePersist`-like feature. I have found the following PR for supporting it: https://github.com/spring-projects/spring-data-r2dbc/pull/397. Unfortunately, it has not been released yet in the main `spring-data-r2dbc` module – fyrkov Sep 30 '20 at 06:53