Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions