36
36
import org .springframework .data .jdbc .core .conversion .JdbcEntityDeleteWriter ;
37
37
import org .springframework .data .jdbc .core .conversion .JdbcEntityWriter ;
38
38
import org .springframework .data .jdbc .mapping .event .AfterDelete ;
39
- import org .springframework .data .jdbc .mapping .event .AfterInsert ;
40
- import org .springframework .data .jdbc .mapping .event .AfterUpdate ;
39
+ import org .springframework .data .jdbc .mapping .event .AfterSave ;
41
40
import org .springframework .data .jdbc .mapping .event .BeforeDelete ;
42
- import org .springframework .data .jdbc .mapping .event .BeforeInsert ;
43
- import org .springframework .data .jdbc .mapping .event .BeforeUpdate ;
41
+ import org .springframework .data .jdbc .mapping .event .BeforeSave ;
44
42
import org .springframework .data .jdbc .mapping .event .Identifier ;
45
43
import org .springframework .data .jdbc .mapping .event .Identifier .Specified ;
46
44
import org .springframework .data .jdbc .mapping .model .BasicJdbcPersistentEntityInformation ;
@@ -102,14 +100,29 @@ private static GenericConversionService getDefaultConversionService() {
102
100
103
101
@ Override
104
102
public <T > void save (T instance , Class <T > domainType ) {
105
- createChange (instance ).executeWith (interpreter );
103
+
104
+ JdbcPersistentEntityInformation <T , ?> entityInformation = context .getRequiredPersistentEntityInformation (domainType );
105
+
106
+ AggregateChange change = createChange (instance );
107
+
108
+ publisher .publishEvent (new BeforeSave ( //
109
+ Identifier .ofNullable (entityInformation .getId (instance )), //
110
+ instance , //
111
+ change //
112
+ ));
113
+
114
+ change .executeWith (interpreter );
115
+
116
+ publisher .publishEvent (new AfterSave ( //
117
+ Identifier .of (entityInformation .getId (instance )), //
118
+ instance , //
119
+ change //
120
+ ));
106
121
}
107
122
108
123
@ Override
109
124
public <T > void insert (T instance , Class <T > domainType , Map <String , Object > additionalParameters ) {
110
125
111
- publisher .publishEvent (new BeforeInsert (instance ));
112
-
113
126
KeyHolder holder = new GeneratedKeyHolder ();
114
127
JdbcPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (domainType );
115
128
JdbcPersistentEntityInformation <T , ?> entityInformation = context
@@ -132,23 +145,17 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
132
145
throw new IllegalStateException (String .format (ENTITY_NEW_AFTER_INSERT , persistentEntity ));
133
146
}
134
147
135
- publisher .publishEvent (new AfterInsert (Identifier .of (entityInformation .getRequiredId (instance )), instance ));
136
-
137
148
}
138
149
139
150
@ Override
140
151
public <S > void update (S instance , Class <S > domainType ) {
141
152
142
153
JdbcPersistentEntity <S > persistentEntity = getRequiredPersistentEntity (domainType );
143
- JdbcPersistentEntityInformation <S , ?> entityInformation = context
144
- .getRequiredPersistentEntityInformation (domainType );
145
154
146
- Specified specifiedId = Identifier .of (entityInformation .getRequiredId (instance ));
147
- publisher .publishEvent (new BeforeUpdate (specifiedId , instance ));
148
155
operations .update (sql (domainType ).getUpdate (), getPropertyMap (instance , persistentEntity ));
149
- publisher .publishEvent (new AfterUpdate (specifiedId , instance ));
150
156
}
151
157
158
+ @ SuppressWarnings ("ConstantConditions" )
152
159
@ Override
153
160
public long count (Class <?> domainType ) {
154
161
return operations .getJdbcOperations ().queryForObject (sql (domainType ).getCount (), Long .class );
@@ -211,9 +218,21 @@ public void deleteAll(Class<?> domainType) {
211
218
change .executeWith (interpreter );
212
219
}
213
220
214
- void doDelete (Object rootId , PropertyPath propertyPath ) {
221
+ private void deleteTree (Object id , Object entity , Class <?> domainType ) {
215
222
216
- JdbcPersistentEntity <?> entityToDelete = context .getRequiredPersistentEntity (propertyPath .getTypeInformation ());
223
+ AggregateChange change = createDeletingChange (id , entity , domainType );
224
+
225
+ Specified specifiedId = Identifier .of (id );
226
+ Optional <Object > optionalEntity = Optional .ofNullable (entity );
227
+ publisher .publishEvent (new BeforeDelete (specifiedId , optionalEntity , change ));
228
+
229
+ change .executeWith (interpreter );
230
+
231
+ publisher .publishEvent (new AfterDelete (specifiedId , optionalEntity , change ));
232
+
233
+ }
234
+
235
+ void doDelete (Object rootId , PropertyPath propertyPath ) {
217
236
218
237
JdbcPersistentEntity <?> rootEntity = context .getRequiredPersistentEntity (propertyPath .getOwningType ());
219
238
@@ -228,23 +247,12 @@ void doDelete(Object rootId, PropertyPath propertyPath) {
228
247
229
248
}
230
249
231
- void doDelete (Specified specifiedId , Optional <Object > optionalEntity , Class <?> domainType ) {
232
-
233
- publisher .publishEvent (new BeforeDelete (specifiedId , optionalEntity ));
250
+ void doDelete (Object id , Optional <Object > optionalEntity , Class <?> domainType ) {
234
251
235
252
String deleteByIdSql = sql (domainType ).getDeleteById ();
236
- MapSqlParameterSource parameter = createIdParameterSource (specifiedId . getValue () , domainType );
253
+ MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
237
254
238
255
operations .update (deleteByIdSql , parameter );
239
-
240
- publisher .publishEvent (new AfterDelete (specifiedId , optionalEntity ));
241
- }
242
-
243
- private void deleteTree (Object id , Object entity , Class <?> domainType ) {
244
-
245
- AggregateChange change = createDeletingChange (id , entity , domainType );
246
-
247
- change .executeWith (interpreter );
248
256
}
249
257
250
258
private <T > AggregateChange createChange (T instance ) {
0 commit comments