@@ -819,6 +819,32 @@ void saveAndUpdateAggregateWithImmutableVersion() {
819
819
.hasRootCauseInstanceOf (OptimisticLockingFailureException .class );
820
820
}
821
821
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
+
822
848
@ Test // DATAJDBC-219 Test that a delete with a version attribute works as expected.
823
849
void deleteAggregateWithVersion () {
824
850
@@ -1227,6 +1253,25 @@ static class AggregateWithImmutableVersion {
1227
1253
1228
1254
@ Id Long id ;
1229
1255
@ 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 ;
1230
1275
}
1231
1276
1232
1277
@ Data
0 commit comments