|
12 | 12 | import org.hibernate.LockMode;
|
13 | 13 | import org.hibernate.NonUniqueObjectException;
|
14 | 14 | import org.hibernate.action.internal.AbstractEntityInsertAction;
|
| 15 | +import org.hibernate.action.internal.EntityIdentityInsertAction; |
15 | 16 | import org.hibernate.engine.internal.CascadePoint;
|
16 | 17 | import org.hibernate.engine.internal.Versioning;
|
17 | 18 | import org.hibernate.engine.spi.EntityEntry;
|
@@ -131,7 +132,6 @@ protected CompletionStage<Void> reactiveSaveWithGeneratedId(
|
131 | 132 | .thenCompose( generatedId -> performSaveWithId( entity, context, source, persister, generator, generatedId ) );
|
132 | 133 | }
|
133 | 134 |
|
134 |
| - // FIXME: I think this should be a reactive type |
135 | 135 | final Object generatedId = ( (BeforeExecutionGenerator) generator ).generate( source, entity, null, INSERT );
|
136 | 136 | return performSaveWithId( entity, context, source, persister, generator, generatedId );
|
137 | 137 | }
|
@@ -282,68 +282,69 @@ protected CompletionStage<Void> reactivePerformSaveOrReplicate(
|
282 | 282 | );
|
283 | 283 |
|
284 | 284 | return cascadeBeforeSave( source, persister, entity, context )
|
285 |
| - .thenCompose( v -> { |
286 |
| - // We have to do this after cascadeBeforeSave completes, |
287 |
| - // since it could result in generation of parent ids, |
288 |
| - // which we will need as foreign keys in the insert |
289 |
| - |
290 |
| - Object[] values = persister.getPropertyValuesToInsert( entity, getMergeMap( context ), source ); |
291 |
| - Type[] types = persister.getPropertyTypes(); |
292 |
| - |
293 |
| - boolean substitute = substituteValuesIfNecessary( entity, id, values, persister, source ); |
294 |
| - |
295 |
| - if ( persister.hasCollections() ) { |
296 |
| - boolean substituteBecauseOfCollections = visitCollectionsBeforeSave( entity, id, values, types, source ); |
297 |
| - substitute = substitute || substituteBecauseOfCollections; |
298 |
| - } |
299 |
| - |
300 |
| - if ( substitute ) { |
301 |
| - persister.setValues( entity, values ); |
302 |
| - } |
303 |
| - |
304 |
| - TypeHelper.deepCopy( |
305 |
| - values, |
306 |
| - types, |
307 |
| - persister.getPropertyUpdateability(), |
308 |
| - values, |
309 |
| - source |
310 |
| - ); |
311 |
| - |
312 |
| - CompletionStage<AbstractEntityInsertAction> insert = addInsertAction( |
313 |
| - values, id, entity, persister, useIdentityColumn, source, shouldDelayIdentityInserts |
314 |
| - ); |
315 |
| - |
316 |
| - EntityEntry newEntry = persistenceContext.getEntry( entity ); |
317 |
| - |
318 |
| - if ( newEntry != original ) { |
319 |
| - EntityEntryExtraState extraState = newEntry.getExtraState( EntityEntryExtraState.class ); |
320 |
| - if ( extraState == null ) { |
321 |
| - newEntry.addExtraState( original.getExtraState( EntityEntryExtraState.class ) ); |
| 285 | + .thenCompose( v -> addInsertAction( |
| 286 | + // We have to do this after cascadeBeforeSave completes, |
| 287 | + // since it could result in generation of parent ids, |
| 288 | + // which we will need as foreign keys in the insert |
| 289 | + cloneAndSubstituteValues( entity, persister, context, source, id ), |
| 290 | + id, |
| 291 | + entity, |
| 292 | + persister, |
| 293 | + useIdentityColumn, |
| 294 | + source, |
| 295 | + shouldDelayIdentityInserts |
| 296 | + ) ) |
| 297 | + .thenCompose( insert -> cascadeAfterSave( source, persister, entity, context ) |
| 298 | + .thenAccept( unused -> { |
| 299 | + final Object finalId = handleGeneratedId( useIdentityColumn, id, insert ); |
| 300 | + EntityEntry newEntry = persistenceContext.getEntry( entity ); |
| 301 | + |
| 302 | + if ( newEntry != original ) { |
| 303 | + EntityEntryExtraState extraState = newEntry.getExtraState( EntityEntryExtraState.class ); |
| 304 | + if ( extraState == null ) { |
| 305 | + newEntry.addExtraState( original.getExtraState( EntityEntryExtraState.class ) ); |
| 306 | + } |
322 | 307 | }
|
323 |
| - } |
324 |
| - |
325 |
| - return insert; |
326 |
| - } ) |
327 |
| - .thenCompose( vv -> cascadeAfterSave( source, persister, entity, context ) ); |
328 |
| - |
329 |
| -// .thenAccept( v -> { |
330 |
| - // postpone initializing id in case the insert has non-nullable transient dependencies |
331 |
| - // that are not resolved until cascadeAfterSave() is executed |
332 |
| - |
333 |
| -// Object newId = id; |
334 |
| -// if ( useIdentityColumn && insert.isEarlyInsert() ) { |
335 |
| -// if ( !EntityIdentityInsertAction.class.isInstance( insert ) ) { |
336 |
| -// throw new IllegalStateException( |
337 |
| -// "Insert should be using an identity column, but action is of unexpected type: " + |
338 |
| -// insert.getClass().getName() |
339 |
| -// ); |
340 |
| -// } |
341 |
| -// newId = ( (EntityIdentityInsertAction) insert ).getGeneratedId(); |
342 |
| -// |
343 |
| -// insert.handleNaturalIdPostSaveNotifications( newId ); |
344 |
| -// } |
345 |
| -// return newId; |
346 |
| -// } ); |
| 308 | + } ) |
| 309 | + ); |
| 310 | + } |
| 311 | + |
| 312 | + private static Object handleGeneratedId(boolean useIdentityColumn, Object id, AbstractEntityInsertAction insert) { |
| 313 | + if ( useIdentityColumn && insert.isEarlyInsert() ) { |
| 314 | + if ( insert instanceof EntityIdentityInsertAction ) { |
| 315 | + Object generatedId = ( (EntityIdentityInsertAction) insert ).getGeneratedId(); |
| 316 | + insert.handleNaturalIdPostSaveNotifications( generatedId ); |
| 317 | + return generatedId; |
| 318 | + } |
| 319 | + throw new IllegalStateException( |
| 320 | + "Insert should be using an identity column, but action is of unexpected type: " + insert.getClass() |
| 321 | + .getName() ); |
| 322 | + } |
| 323 | + |
| 324 | + return id; |
| 325 | + } |
| 326 | + |
| 327 | + private Object[] cloneAndSubstituteValues(Object entity, EntityPersister persister, C context, EventSource source, Object id) { |
| 328 | + Object[] values = persister.getPropertyValuesToInsert( entity, getMergeMap(context), source ); |
| 329 | + Type[] types = persister.getPropertyTypes(); |
| 330 | + |
| 331 | + boolean substitute = substituteValuesIfNecessary( entity, id, values, persister, source ); |
| 332 | + if ( persister.hasCollections() ) { |
| 333 | + substitute = visitCollectionsBeforeSave( entity, id, values, types, source ) || substitute; |
| 334 | + } |
| 335 | + |
| 336 | + if ( substitute ) { |
| 337 | + persister.setValues( entity, values ); |
| 338 | + } |
| 339 | + |
| 340 | + TypeHelper.deepCopy( |
| 341 | + values, |
| 342 | + types, |
| 343 | + persister.getPropertyUpdateability(), |
| 344 | + values, |
| 345 | + source |
| 346 | + ); |
| 347 | + return values; |
347 | 348 | }
|
348 | 349 |
|
349 | 350 | protected Map<Object,Object> getMergeMap(C anything) {
|
|
0 commit comments