spring-data-r2dbc icon indicating copy to clipboard operation
spring-data-r2dbc copied to clipboard

Before save callback not using transformed entity on update

Open JoseLion opened this issue 5 years ago • 4 comments

Hi,

The title of the issue might be a bit confusing, but here's what I found in some simple steps:

  • Create a BeforeConvertCallback for any entity where the entity is changed somehow.
  • Create a BeforeSaveCallback for the same entity. We'll use it only for control.
  • Create a new entity. The entity object passed to the BeforeSaveCallback has the changes made by the BeforeConvertCallback.
  • Update the same entity. The entity object passed to the BeforeSaveCallback is missing the changes made on BeforeConvertCallback.

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!

JoseLion avatar Nov 25 '21 00:11 JoseLion

Does it fix in version 1.4.0?

PSilverQ avatar Dec 08 '21 09:12 PSilverQ

Yes, see https://github.com/spring-projects/spring-data-r2dbc/commit/87c2ac56f5b60c45f6773c6495f8d19c910ef86e

mp911de avatar Dec 08 '21 10:12 mp911de

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

PSilverQ avatar Dec 08 '21 12:12 PSilverQ

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.

mp911de avatar Dec 08 '21 12:12 mp911de