Skip to content

Constructor is accessed twice when persisting Java 16 records #1137

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
hajdamak opened this issue Jan 20, 2022 · 3 comments
Closed

Constructor is accessed twice when persisting Java 16 records #1137

hajdamak opened this issue Jan 20, 2022 · 3 comments
Assignees
Labels
type: bug A general bug

Comments

@hajdamak
Copy link

When new Java record is persisted using repository's save method, it's constructor is called twice with the same parameters.
One should be enough to set id and/or version.

Minimal reproducible example: hajdamak/spring-data-jdbc-record-access
Requires Maven and JDK17.
Call mvn spring-boot:run .
Relevant output:

Saving record ...
Constructor is accessed: (null, 0, test)
Constructor is accessed: (5f5b5ea0-3c8c-4ea3-88d1-b02142d2b9b3, 1, test)
Constructor is accessed: (5f5b5ea0-3c8c-4ea3-88d1-b02142d2b9b3, 1, test)
@hajdamak hajdamak changed the title Constructor is accessed twice when persisting Java 16 rerods Constructor is accessed twice when persisting Java 16 records Jan 20, 2022
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 20, 2022
@schauder schauder added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 20, 2022
@mp911de mp911de assigned mp911de and schauder and unassigned mp911de Jan 21, 2022
@mp911de mp911de added type: bug A general bug and removed type: enhancement A general enhancement labels Jan 21, 2022
@mp911de
Copy link
Member

mp911de commented Jan 21, 2022

This is a general issue in Spring Data JDBC where the version number is being set twice:

  • AggregateChangeExecutor -> execute -> executeInsertRoot -> RelationalEntityVersionUtils.setVersionNumberOnEntity
  • AggregateChangeExecutor -> executionContext.populateRootVersionIfNecessary(root)

@hajdamak
Copy link
Author

@mp911de Additional note, during this operation properties are accessed twice each, except Id which is accessed 3 times.

@mp911de
Copy link
Member

mp911de commented Jan 24, 2022

Additional property access is required to create new instances of an object.

schauder added a commit that referenced this issue Mar 22, 2022
This change extracts entity modifying behaviour into separate methods, so it doesn't appear as an unexpected side effect of the creation of aggregate changes.

Also some formatting.

Original pull request #1196
See #1137
lseeker pushed a commit to lseeker/spring-data-jdbc that referenced this issue Apr 6, 2022
…DbActions to simplify execution context.

This change incorporates one test from spring-projects#1150

Original pull request spring-projects#1196
Closes spring-projects#1137
lseeker pushed a commit to lseeker/spring-data-jdbc that referenced this issue Apr 6, 2022
This change extracts entity modifying behaviour into separate methods, so it doesn't appear as an unexpected side effect of the creation of aggregate changes.

Also some formatting.

Original pull request spring-projects#1196
See spring-projects#1137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
4 participants