My sample codes are based on Spring Boot 2.4.0-M2(spring-data-r2dbc 1.2.0-M2), Java 11, Postgres.
In the spring blog and change log, it delcares Spring Data R2dbc got audting support.
I Enabled auditing like this.
@Configuration
@EnableR2dbcAuditing
class DataConfig {
@Bean
ReactiveAuditorAware<String> auditorAware() {
return () -> Mono.just("hantsy");
}
}
And entity class and repository.
interface PersonRepository extends R2dbcRepository<Person, UUID> {
}
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(value = "persons")
class Person {
@Id
@Column("id")
private UUID id;
@Column("first_name")
private String firstName;
@Column("last_name")
private String lastName;
@Column("created_at")
@CreatedDate
private LocalDateTime createdAt;
@Column("updated_at")
@LastModifiedDate
private LocalDateTime updatedAt;
@Column("version")
@Version
private Long version;
}
Schema script. (no worry about the UUID, the extension initialized in docker init scripts)
CREATE TABLE IF NOT EXISTS persons (
-- id SERIAL PRIMARY KEY,
id UUID DEFAULT uuid_generate_v4(),
first_name VARCHAR(255),
last_name VARCHAR(255),
created_at TIMESTAMP ,
updated_at TIMESTAMP,
version INTEGER,
PRIMARY KEY (id)
);
When I added the following code fragments in the ApplicationRunner
to experience the auditing, none of @createdDate
and @lastModifiedDate
are filled.
persons
.save(
Person.builder()
.firstName("hantsy")
.lastName("bai")
.build()
)
.log()
.map(person -> {
person.setFirstName("new Hantsy");
return person;
})
.flatMap(persons::save)
.log()
.then()
.thenMany(persons.findAll())
.subscribe(
data -> log.info("saved data: {}", data),
err -> log.error("err: {}", err)
);
The console prints:
2020-09-07 22:12:30.734 INFO 8800 --- [actor-tcp-nio-2] reactor.Mono.UsingWhen.1 : onNext(Person(id=8814e87c-c6d3-4dca-a095-fb5f41696b49, firstName=hantsy, lastName=bai, createdAt=null, updatedAt=null, version=0))
2020-09-07 22:12:30.779 INFO 8800 --- [actor-tcp-nio-2] reactor.Mono.UsingWhen.1 : onComplete()
2020-09-07 22:12:30.801 INFO 8800 --- [actor-tcp-nio-2] reactor.Mono.FlatMap.2 : | onNext(Person(id=8814e87c-c6d3-4dca-a095-fb5f41696b49, firstName=new Hantsy, lastName=bai, createdAt=null, updatedAt=null, version=1))
2020-09-07 22:12:30.801 INFO 8800 --- [actor-tcp-nio-2] reactor.Mono.FlatMap.2 : | onComplete()
2020-09-07 22:12:30.823 INFO 8800 --- [actor-tcp-nio-2] com.example.demo.DemoApplication : saved data: Person(id=981741c3-4468-474d-9e92-b4d396f004d9, firstName=hantsy, lastName=bai, createdAt=null, updatedAt=null, version=0)
2020-09-07 22:12:30.824 INFO 8800 --- [actor-tcp-nio-2] com.example.demo.DemoApplication : saved data: Person(id=8814e87c-c6d3-4dca-a095-fb5f41696b49, firstName=new Hantsy, lastName=bai, createdAt=null, updatedAt=null, version=1)
Updated: Created another simple example(based on H2) for r2dbc auditing to reproduce it, the testing codes work, but application is still problematic. Not sure if my original example(based on Postgres, and used Pg specific JSON, Enum, etc.) requires other configs, none of the application and the testing codes work as expected.
Updated: A related issue was reported in Github issues, see issues#451. Updated my sample to make sure the fixes is applied in the 1.2.0 snapshot20200910 version. I think the next version(1.2.0-RC1) will include the fixes.
Updated: my sample is updated to Spring Boot 2.4.0-M3, the bug is fixed.