40
40
import org .springframework .data .mapping .context .MappingContext ;
41
41
import org .springframework .data .relational .core .conversion .DbAction ;
42
42
import org .springframework .data .relational .core .conversion .DbActionExecutionResult ;
43
- import org .springframework .data .relational .core .conversion .RelationalEntityVersionUtils ;
44
43
import org .springframework .data .relational .core .mapping .PersistentPropertyPathExtension ;
45
44
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
46
45
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
@@ -65,7 +64,6 @@ class JdbcAggregateChangeExecutionContext {
65
64
private final DataAccessStrategy accessStrategy ;
66
65
67
66
private final Map <DbAction <?>, DbActionExecutionResult > results = new LinkedHashMap <>();
68
- @ Nullable private Long version ;
69
67
70
68
JdbcAggregateChangeExecutionContext (JdbcConverter converter , DataAccessStrategy accessStrategy ) {
71
69
@@ -76,28 +74,8 @@ class JdbcAggregateChangeExecutionContext {
76
74
77
75
<T > void executeInsertRoot (DbAction .InsertRoot <T > insert ) {
78
76
79
- RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (insert .getEntityType ());
80
-
81
- Object id ;
82
- if (persistentEntity .hasVersionProperty ()) {
83
-
84
- RelationalPersistentProperty versionProperty = persistentEntity .getVersionProperty ();
85
-
86
- Assert .state (versionProperty != null , "Version property must not be null at this stage." );
87
-
88
- long initialVersion = versionProperty .getActualType ().isPrimitive () ? 1L : 0 ;
89
-
90
- T rootEntity = RelationalEntityVersionUtils .setVersionNumberOnEntity ( //
91
- insert .getEntity (), initialVersion , persistentEntity , converter );
92
-
93
- id = accessStrategy .insert (rootEntity , insert .getEntityType (), Identifier .empty (), insert .getIdValueSource ());
94
-
95
- setNewVersion (initialVersion );
96
- } else {
97
- id = accessStrategy .insert (insert .getEntity (), insert .getEntityType (), Identifier .empty (),
98
- insert .getIdValueSource ());
99
- }
100
-
77
+ Object id = accessStrategy .insert (insert .getEntity (), insert .getEntityType (), Identifier .empty (),
78
+ insert .getIdValueSource ());
101
79
add (new DbActionExecutionResult (insert , id ));
102
80
}
103
81
@@ -125,12 +103,9 @@ <T> void executeInsertBatch(DbAction.InsertBatch<T> insertBatch) {
125
103
126
104
<T > void executeUpdateRoot (DbAction .UpdateRoot <T > update ) {
127
105
128
- RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (update .getEntityType ());
129
-
130
- if (persistentEntity .hasVersionProperty ()) {
131
- updateWithVersion (update , persistentEntity );
106
+ if (update .getPreviousVersion () != null ) {
107
+ updateWithVersion (update );
132
108
} else {
133
-
134
109
updateWithoutVersion (update );
135
110
}
136
111
}
@@ -147,16 +122,10 @@ <T> void executeUpdate(DbAction.Update<T> update) {
147
122
<T > void executeDeleteRoot (DbAction .DeleteRoot <T > delete ) {
148
123
149
124
if (delete .getPreviousVersion () != null ) {
150
-
151
- RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (delete .getEntityType ());
152
- if (persistentEntity .hasVersionProperty ()) {
153
-
154
- accessStrategy .deleteWithVersion (delete .getId (), delete .getEntityType (), delete .getPreviousVersion ());
155
- return ;
156
- }
125
+ accessStrategy .deleteWithVersion (delete .getId (), delete .getEntityType (), delete .getPreviousVersion ());
126
+ } else {
127
+ accessStrategy .delete (delete .getId (), delete .getEntityType ());
157
128
}
158
-
159
- accessStrategy .delete (delete .getId (), delete .getEntityType ());
160
129
}
161
130
162
131
<T > void executeDelete (DbAction .Delete <T > delete ) {
@@ -258,36 +227,6 @@ private Object getIdFrom(DbAction.WithEntity<?> idOwningAction) {
258
227
return identifier ;
259
228
}
260
229
261
- private void setNewVersion (long version ) {
262
-
263
- Assert .isNull (this .version , "A new version was set a second time." );
264
-
265
- this .version = version ;
266
- }
267
-
268
- private long getNewVersion () {
269
-
270
- Assert .notNull (version , "A new version was requested, but none was set." );
271
-
272
- return version ;
273
- }
274
-
275
- private boolean hasNewVersion () {
276
- return version != null ;
277
- }
278
-
279
- <T > T populateRootVersionIfNecessary (T newRoot ) {
280
-
281
- if (!hasNewVersion ()) {
282
- return newRoot ;
283
- }
284
- // Does the root entity have a version attribute?
285
- RelationalPersistentEntity <T > persistentEntity = (RelationalPersistentEntity <T >) context
286
- .getRequiredPersistentEntity (newRoot .getClass ());
287
-
288
- return RelationalEntityVersionUtils .setVersionNumberOnEntity (newRoot , getNewVersion (), persistentEntity , converter );
289
- }
290
-
291
230
@ SuppressWarnings ("unchecked" )
292
231
@ Nullable
293
232
<T > T populateIdsIfNecessary () {
@@ -378,20 +317,12 @@ private <T> void updateWithoutVersion(DbAction.UpdateRoot<T> update) {
378
317
}
379
318
}
380
319
381
- private <T > void updateWithVersion (DbAction .UpdateRoot <T > update , RelationalPersistentEntity <T > persistentEntity ) {
382
-
383
- // If the root aggregate has a version property, increment it.
384
- Number previousVersion = RelationalEntityVersionUtils .getVersionNumberFromEntity (update .getEntity (),
385
- persistentEntity , converter );
320
+ private <T > void updateWithVersion (DbAction .UpdateRoot <T > update ) {
386
321
322
+ Number previousVersion = update .getPreviousVersion ();
387
323
Assert .notNull (previousVersion , "The root aggregate cannot be updated because the version property is null." );
388
324
389
- setNewVersion (previousVersion .longValue () + 1 );
390
-
391
- T rootEntity = RelationalEntityVersionUtils .setVersionNumberOnEntity (update .getEntity (), getNewVersion (),
392
- persistentEntity , converter );
393
-
394
- if (!accessStrategy .updateWithVersion (rootEntity , update .getEntityType (), previousVersion )) {
325
+ if (!accessStrategy .updateWithVersion (update .getEntity (), update .getEntityType (), previousVersion )) {
395
326
396
327
throw new OptimisticLockingFailureException (String .format (UPDATE_FAILED_OPTIMISTIC_LOCKING , update .getEntity ()));
397
328
}
0 commit comments