Skip to content

Commit 9eb2dca

Browse files
committed
Copy over test from #1150
1 parent 5ea4408 commit 9eb2dca

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,32 @@ void saveAndUpdateAggregateWithImmutableVersion() {
819819
.hasRootCauseInstanceOf(OptimisticLockingFailureException.class);
820820
}
821821

822+
@Test // GH-1137
823+
void testUpdateEntityWithVersionDoesNotTriggerAnewConstructorInvocation() {
824+
AggregateWithImmutableVersion aggregateWithImmutableVersion = new AggregateWithImmutableVersion(null, null);
825+
826+
final AggregateWithImmutableVersion savedRoot = template.save(aggregateWithImmutableVersion);
827+
828+
assertThat(savedRoot).isNotNull();
829+
assertThat(savedRoot.version).isEqualTo(0L);
830+
831+
assertThat(AggregateWithImmutableVersion.constructorInvocations).containsExactly(
832+
new ConstructorInvocation(null, null), // Initial invocation, done by client
833+
new ConstructorInvocation(null, savedRoot.version), // Assigning the version
834+
new ConstructorInvocation(savedRoot.id, savedRoot.version) // Assigning the id
835+
);
836+
837+
AggregateWithImmutableVersion.clearConstructorInvocationData();
838+
839+
final AggregateWithImmutableVersion updatedRoot = template.save(savedRoot);
840+
841+
assertThat(updatedRoot).isNotNull();
842+
assertThat(updatedRoot.version).isEqualTo(1L);
843+
844+
// Expect only one assignnment of the version to AggregateWithImmutableVersion
845+
assertThat(AggregateWithImmutableVersion.constructorInvocations).containsOnly(new ConstructorInvocation(savedRoot.id, updatedRoot.version));
846+
}
847+
822848
@Test // DATAJDBC-219 Test that a delete with a version attribute works as expected.
823849
void deleteAggregateWithVersion() {
824850

@@ -1227,6 +1253,25 @@ static class AggregateWithImmutableVersion {
12271253

12281254
@Id Long id;
12291255
@Version Long version;
1256+
1257+
private final static List<ConstructorInvocation> constructorInvocations = new ArrayList<>();
1258+
1259+
public static void clearConstructorInvocationData() {
1260+
constructorInvocations.clear();
1261+
}
1262+
1263+
public AggregateWithImmutableVersion(Long id, Long version) {
1264+
constructorInvocations.add(new ConstructorInvocation(id, version));
1265+
this.id = id;
1266+
this.version = version;
1267+
}
1268+
}
1269+
1270+
@Value
1271+
@EqualsAndHashCode
1272+
private static class ConstructorInvocation {
1273+
private Long id;
1274+
private Long version;
12301275
}
12311276

12321277
@Data

0 commit comments

Comments
 (0)