Skip to content

Changes to entity in BeforeSaveCallback not being applied to generated SQL #902

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

Closed
alimac87 opened this issue Jan 6, 2021 · 4 comments
Closed
Labels
status: invalid An issue that we don't feel is valid

Comments

@alimac87
Copy link

alimac87 commented Jan 6, 2021

Hi,

I'd tried to use a BeforeSaveCallback to change an immutable entity before saving to the database (specifically to set a String id attribute). However after returning a new version of the entity from a BeforeSaveCallback, the generated insert statement doesn't include the ID column:
INSERT INTO "LEGO_SET" ("MAX_AGE", "MIN_AGE", "NAME") VALUES...

I've reproduced the issue by modifying the LegoSet example to use a BeforeSaveCallback: alimac87/spring-data-examples@924e779

With these changes, running AggregateTests:exerciseSomewhatComplexEntity errors out on the first call to repository:save (I've made the ID columns NOT NULL to highlight the issue).

It may be that I'm mis-understanding and/or mis-using the BeforeSaveCallback in some way - apologies in advance if that's the case.

Some things I've tried:

  • Within the BeforeSaveCallback setting the entity ids by mutating the entity does work, however in my production code the preference is for immutable objects (which I think using BeforeSaveCallback should support, at least if I'm reading point 3 of this comment correctly?)
  • Debugging through the code I see JdbcAggregateTemplate.store does update the aggregateRoot on the MutableAggregateChange (line 335), but does not appear to update the entity contained within the DbAction.InsertRoot within the MutableAggregateChange - it appears it's the DbAction which is later used to build the INSERT statement.
  • I can avoid the issue by using JdbcAggregateTemplate.insert directly

Thanks,
Ali

@alimac87 alimac87 changed the title Changes to BeforeSaveCallback Changes to entity in BeforeSaveCallback not being applied to generated SQL Jan 6, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 6, 2021
@schauder
Copy link
Contributor

This is to be expected.

When BeforeSaveCallback gets fired the logical conversion into SQL Statements has already happened. The statements aren't actually generated but the decisions what statements to generate and values to use has been made by using references to the entities involved.

In order to set the id before an insert you should use BeforeConvertCallback.

I'll create an issue to fix the example which should use BeforeConvertEvent and only works with mutable entities as you found out the hard way.

@alimac87
Copy link
Author

Thanks for clearing that up, much appreciated.

One small related point, the documentation has the same "When It Is Published" description for both BeforeConvertCallback and BeforeSaveCallback.

Is that as expected? Or could/should the text for BeforeConvertedCallback read something like "Before the aggregate is converted into a database change"?

@schauder
Copy link
Contributor

The documentation is wrong. I'll fix that.

@alimac87
Copy link
Author

Great, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants