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

Immutable entity class vs optimistic locking: version-field not incremented by save() [DATAJDBC-481] #707

spring-projects-issues opened this issue Jan 29, 2020 · 2 comments
type: bug


Copy link

@spring-projects-issues spring-projects-issues commented Jan 29, 2020

Clemens Hahn opened DATAJDBC-481 and commented

I think there is a problem with @Version-Fields in immutable entities.

I found the test This test is green on my machine.

I extended the test: the return value of does not hold the incremented version number (problem occurs only at the second call!).

Please have a look at the following adjusted test:

@Test // DATAJDBC-219 Test that immutable version attribute works as expected.
public void saveAndUpdateAggregateWithImmutableVersion() {

  AggregateWithImmutableVersion aggregate = new AggregateWithImmutableVersion(null, null);
  aggregate =;
  assertThat(aggregate.version).isEqualTo(1L); // works

  Long id = aggregate.getId();

  AggregateWithImmutableVersion reloadedAggregate = template.findById(id, aggregate.getClass());
  assertThat(reloadedAggregate.getVersion()).describedAs("version field should initially have the value 1")

  AggregateWithImmutableVersion saved =;

  AggregateWithImmutableVersion updatedAggregate = template.findById(id, aggregate.getClass());
      .describedAs("returned by save(): "+ saved + " vs. returned by findById(): " + updatedAggregate)
      .isEqualTo(updatedAggregate.version); // fails
  // [..]

This adjusted test fails with

org.junit.ComparisonFailure: [returned by save(): JdbcAggregateTemplateIntegrationTests.AggregateWithImmutableVersion(id=1, version=1) vs. returned by findById(): JdbcAggregateTemplateIntegrationTests.AggregateWithImmutableVersion(id=1, version=2)] 
 Expected :2L
 Actual :1L

I hope I did not make a mistake and that the current bahavior is not the expected one.

I attached a Patch-File with this adjusted test.



Referenced from: commits d77e9cd, 2f9d4b2

Copy link

@spring-projects-issues spring-projects-issues commented Jan 30, 2020

Jens Schauder commented

Thanks for finding this and providing the reproducer. It made fixing this a breeze

Copy link

@spring-projects-issues spring-projects-issues commented Jan 30, 2020

Clemens Hahn commented

I'm more than happy to support you a little bit by reporting these issues. Thanks for the rapid fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
type: bug
None yet

No branches or pull requests

2 participants