@@ -144,55 +144,58 @@ else if ( generator instanceof Assigned ) {
144
144
// the entity instance, so it will be available
145
145
// to the entity in the @PrePersist callback
146
146
if ( generator instanceof ReactiveIdentifierGenerator ) {
147
- return ( (ReactiveIdentifierGenerator <?>) generator )
148
- .generate ( ( ReactiveConnectionSupplier ) source , entity )
149
- .thenApply ( id -> castToIdentifierType ( id , persister ) )
150
- .thenCompose ( gid -> performSaveWithId (
151
- entity ,
152
- context ,
153
- source ,
154
- persister ,
155
- generator ,
156
- gid ,
157
- requiresImmediateIdAccess ,
158
- false
159
- ) );
147
+ return generateId ( entity , source , (ReactiveIdentifierGenerator <?>) generator , persister )
148
+ .thenCompose ( gid -> {
149
+ if ( gid == SHORT_CIRCUIT_INDICATOR ) {
150
+ source .getIdentifier ( entity );
151
+ return voidFuture ();
152
+ }
153
+ persister .setIdentifier ( entity , gid , source );
154
+ return reactivePerformSave (
155
+ entity ,
156
+ gid ,
157
+ persister ,
158
+ generatedOnExecution ,
159
+ context ,
160
+ source ,
161
+ false
162
+ );
163
+ } );
160
164
}
161
165
162
166
generatedId = ( (BeforeExecutionGenerator ) generator ).generate ( source , entity , null , INSERT );
167
+ if ( generatedId == SHORT_CIRCUIT_INDICATOR ) {
168
+ source .getIdentifier ( entity );
169
+ return voidFuture ();
170
+ }
171
+ persister .setIdentifier ( entity , generatedId , source );
163
172
}
164
173
final Object id = castToIdentifierType ( generatedId , persister );
165
- return reactivePerformSave ( entity , id , persister , generatedOnExecution , context , source , requiresImmediateIdAccess );
174
+ final boolean delayIdentityInserts = !source .isTransactionInProgress () && !requiresImmediateIdAccess && generatedOnExecution ;
175
+ return reactivePerformSave ( entity , id , persister , generatedOnExecution , context , source , delayIdentityInserts );
166
176
}
167
177
168
- private CompletionStage <Void > performSaveWithId (
178
+ private CompletionStage <Object > generateId (
169
179
Object entity ,
170
- C context ,
171
180
EventSource source ,
172
- EntityPersister persister ,
173
- Generator generator ,
174
- Object generatedId ,
175
- boolean requiresImmediateIdAccess ,
176
- boolean generatedOnExecution ) {
177
- if ( generatedId == null ) {
178
- throw new IdentifierGenerationException ( "null id generated for: " + entity .getClass () );
179
- }
180
- if ( generatedId == SHORT_CIRCUIT_INDICATOR ) {
181
- source .getIdentifier ( entity );
182
- return voidFuture ();
183
- }
184
- if ( LOG .isDebugEnabled () ) {
185
- LOG .debugf (
186
- "Generated identifier: %s, using strategy: %s" ,
187
- persister .getIdentifierType ().toLoggableString ( generatedId , source .getFactory () ),
188
- generator .getClass ().getName ()
189
- );
190
- }
191
- final boolean delayIdentityInserts =
192
- !source .isTransactionInProgress ()
193
- && !requiresImmediateIdAccess
194
- && generatedOnExecution ;
195
- return reactivePerformSave ( entity , generatedId , persister , false , context , source , delayIdentityInserts );
181
+ ReactiveIdentifierGenerator <?> generator ,
182
+ EntityPersister persister ) {
183
+ return generator
184
+ .generate ( (ReactiveConnectionSupplier ) source , entity )
185
+ .thenApply ( id -> castToIdentifierType ( id , persister ) )
186
+ .thenCompose ( generatedId -> {
187
+ if ( generatedId == null ) {
188
+ return failedFuture ( new IdentifierGenerationException ( "null id generated for: " + entity .getClass () ) );
189
+ }
190
+ if ( LOG .isDebugEnabled () ) {
191
+ LOG .debugf (
192
+ "Generated identifier: %s, using strategy: %s" ,
193
+ persister .getIdentifierType ().toLoggableString ( generatedId , source .getFactory () ),
194
+ generator .getClass ().getName ()
195
+ );
196
+ }
197
+ return completedFuture ( generatedId );
198
+ } );
196
199
}
197
200
198
201
/**
@@ -232,10 +235,7 @@ protected CompletionStage<Void> reactivePerformSave(
232
235
if ( persister .getGenerator () instanceof Assigned ) {
233
236
id = persister .getIdentifier ( entity , source );
234
237
if ( id == null ) {
235
- throw new IdentifierGenerationException (
236
- "Identifier of entity '" + persister .getEntityName ()
237
- + "' must be manually assigned before calling 'persist()'"
238
- );
238
+ return failedFuture ( new IdentifierGenerationException ( "Identifier of entity '" + persister .getEntityName () + "' must be manually assigned before calling 'persist()'" ) );
239
239
}
240
240
}
241
241
@@ -420,7 +420,7 @@ private CompletionStage<AbstractEntityInsertAction> addInsertAction(
420
420
boolean useIdentityColumn ,
421
421
EventSource source ,
422
422
boolean shouldDelayIdentityInserts ) {
423
- final ReactiveActionQueue actionQueue = source .unwrap ( ReactiveSession .class ).getReactiveActionQueue ();
423
+ final ReactiveActionQueue actionQueue = source .unwrap (ReactiveSession .class ).getReactiveActionQueue ();
424
424
if ( useIdentityColumn ) {
425
425
final ReactiveEntityIdentityInsertAction insert = new ReactiveEntityIdentityInsertAction (
426
426
values , entity , persister , false , source , shouldDelayIdentityInserts
0 commit comments