Before save callback not using transformed entity on update
Hi,
The title of the issue might be a bit confusing, but here's what I found in some simple steps:
- Create a
BeforeConvertCallbackfor any entity where the entity is changed somehow. - Create a
BeforeSaveCallbackfor the same entity. We'll use it only for control. - Create a new entity. The entity object passed to the
BeforeSaveCallbackhas the changes made by theBeforeConvertCallback. - Update the same entity. The entity object passed to the
BeforeSaveCallbackis missing the changes made onBeforeConvertCallback.
I found the problem in R2dbcEntityTemplate.java, so I'll be opening a PR with this small change in a few. I hope this helps 🙂
Cheers!
Does it fix in version 1.4.0?
Yes, see https://github.com/spring-projects/spring-data-r2dbc/commit/87c2ac56f5b60c45f6773c6495f8d19c910ef86e
I encounter the problem when I use spring-data-r2dbc ( version 1.4.0), Is there something wrong with my usage?
Here is my pom depencency(I override the version of r2dbc in spring-boot-starter-data-r2dbc by dependencyManagement ):
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
</dependencyManagement>
....
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
....
And Here is my custom EntityCallback,I use kotlin and kotlin coroutine(mono { } ) to bridge Reactive code.
Everything is Fine Except the transformed entity returned by onBeforeSave doesn't write to the Database, r2dbc use the old one.
@Component
class EnvironmentEntityCallback : ReactiveAfterConvertCallback<EnvironmentDto?> , ReactiveBeforeSaveCallback<EnvironmentDto?> {
override fun onAfterConvert(entity: EnvironmentDto, p1: Document, p2: String ):Publisher<EnvironmentDto?> = mono {
entity.nestedConfig = toNestedConfig(entity.configs)
entity
}
override fun onBeforeSave(entity: EnvironmentDto, p1: Document, p2: String ):Publisher<EnvironmentDto?> = mono {
entity.configs = JSONUtil.parseObj(entity.nestedConfig)
entity
}
}
And I add a break point in R2dbcEntityTemplate.java , near the fix code entityToUse = entity;And debug.However it's not executed, And I add To other method looks like will executing when save , it's still not executed,I'm puzzled.
Thanks for reading , want help! O(≧口≦)O
I see now what causes the confusion. We fixed the issue yesterday so it cannot be shipped in a version that was released two weeks ago.