Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveBeforeSaveCallback not documented properly #1448

Closed
amazingstream opened this issue Nov 10, 2023 · 6 comments
Closed

ReactiveBeforeSaveCallback not documented properly #1448

amazingstream opened this issue Nov 10, 2023 · 6 comments
Assignees
Labels
type: documentation A documentation update

Comments

@amazingstream
Copy link

ReactiveBeforeSaveCallback is not persisting the transformed object to the database but the save method returns the transformed object.

I am using version 3.4.16. I implemented ReactiveBeforeSaveCallback like this...

import org.springframework.data.cassandra.core.mapping.event.ReactiveBeforeSaveCallback;
import reactor.core.publisher.Mono;

public class CustomReactiveBeforeSaveCallback implements ReactiveBeforeSaveCallback {

@Override
public Mono<MyType> onBeforeSave(MyType entity, String tableName) { 
    entity.setColumnValue("CHANGED VALUE...");
    return Mono.just(entity);
}

}

MyType n = new MyType();
n.setColumnValue("ORIGINAL");

I am calling the save method like this.

Mono saved = reactiveEntityRepository.save(n);
saved.subscribe(s ->
System.out.println(s.columnValue); //returns CHANGED VALUE but the database has ORIGINAL

);

saved.columnValue has "CHANGED VALUE". But the database still has "ORIGINAL". I was expecting the database to persist the transformed value. I can see that the before save method is being invoked but the updated value is not being stored to the database.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 10, 2023
@mp911de
Copy link
Member

mp911de commented Nov 10, 2023

BeforeSaveCallback is called after transforming the object into a Statement (that's also why you get the Statement object). Before save is useful if you want to save a certain state to the database but you want to render a different object state after the save.

What you're looking for is ReactiveBeforeConvertCallback.

Looking at the documentation, the docs of events and callbacks could be a bit more descriptive and so I'm converting this ticket into a documentation ticket.

@mp911de mp911de removed the status: waiting-for-triage An issue we've not yet triaged label Nov 10, 2023
@mp911de mp911de changed the title ReactiveBeforeSaveCallback is not persisting the transformed object to the database but the save method returns the transformed object. ReactiveBeforeSaveCallback not documented properly Nov 10, 2023
@mp911de mp911de added the type: documentation A documentation update label Nov 10, 2023
@mp911de mp911de self-assigned this Nov 10, 2023
@mp911de mp911de added this to the 4.0.12 (2022.0.12) milestone Nov 10, 2023
@amazingstream
Copy link
Author

Thanks for the quick response. Changing ReactiveBeforeSaveCallback to ReactiveBeforeConvertCallback worked for me.

@amazingstream
Copy link
Author

why is ReactiveAfterConvertCallback or afterLoadCallback not available? I am able to transform the entity before persisting using ReactiveBeforeConvertCallback but I have to transform again after reading from the database.

Should I be using a different interface?

@mp911de
Copy link
Member

mp911de commented Nov 10, 2023

ReactiveAfterConvertCallback isn't available as the entity row mapper doesn't expose a reactive mapping interface. Also, bulk-loading entities and creating Mono's creates a lot of GC pressure so we initially didn't want to expose a reactive AfterLoad/AfterConvert callback.

@amazingstream
Copy link
Author

amazingstream commented Nov 10, 2023

I see. Initially, I implemented PropertyValueConverter and added @ValueConverter to a property before persisting. But that is also not getting invoked.

I understand @ValueConverter is part of spring data and I am using Spring data reactive cassandra.

@table("tablename")
public class TestTableClass{

@ValueConverter(..class)

@mp911de
Copy link
Member

mp911de commented Nov 13, 2023

Thanks for bringing @ValueConverter up. It isn't supported yet and I created #1449 to keep track of the progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants