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

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

Comments

@spring-projects-issues
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 org.springframework.data.jdbc.core.JdbcAggregateTemplateIntegrationTests#saveAndUpdateAggregateWithImmutableVersion. This test is green on my machine.

I extended the test: the return value of template.save(reloadedAggregate) 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 = template.save(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")
      .isEqualTo(1L);

  AggregateWithImmutableVersion saved = template.save(reloadedAggregate);

  AggregateWithImmutableVersion updatedAggregate = template.findById(id, aggregate.getClass());
  assertThat(saved.version)
      .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.

 


Attachments:

Referenced from: commits d77e9cd, 2f9d4b2

@spring-projects-issues
Copy link
Author

@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

@spring-projects-issues
Copy link
Author

@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
Labels
type: bug
Projects
None yet
Development

No branches or pull requests

2 participants