Skip to content

Constructor is accessed twice when persisting Java 16 records #1137

Closed

Description

@hajdak

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)

Activity

  1. changed the title [-]Constructor is accessed twice when persisting Java 16 rerods[/-] [+]Constructor is accessed twice when persisting Java 16 records[/+] on Jan 20, 2022
  2. mp911de commented on Jan 21, 2022

    @mp911de
    Member

    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)
  3. hajdak commented on Jan 21, 2022

    @hajdak
    Author

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

  4. mp911de commented on Jan 24, 2022

    @mp911de
    Member

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

  5. added a commit that references this issue on Mar 22, 2022
  6. added 2 commits that reference this issue on Apr 6, 2022
    6500bfe
    a4d2f1e
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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions