Skip to content

Before save callback not using transformed entity on update #689

Closed

Description

@JoseLion

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!

Activity

  1. added 2 commits that reference this issue on Nov 25, 2021
    0b10453
    e04c65a
  2. added this to the 1.3.8 (2021.0.8) milestone on Dec 7, 2021
  3. added 2 commits that reference this issue on Dec 7, 2021
    2a06030
    db82073
  4. PSilverQ commented on Dec 8, 2021

    @PSilverQ

    Does it fix in version 1.4.0?

  5. mp911de commented on Dec 8, 2021

    @mp911de
    Member

    Yes, see 87c2ac5

  6. PSilverQ commented on Dec 8, 2021

    @PSilverQ

    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

  7. mp911de commented on Dec 8, 2021

    @mp911de
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions