Skip to content

Commit 98069e9

Browse files
committed
[hibernate#1904] PropertyAccessException when creating a new object with a one-to-one association
1 parent 434f0ec commit 98069e9

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/event/impl/AbstractReactiveSaveEventListener.java

+45-45
Original file line numberDiff line numberDiff line change
@@ -144,55 +144,58 @@ else if ( generator instanceof Assigned ) {
144144
// the entity instance, so it will be available
145145
// to the entity in the @PrePersist callback
146146
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+
} );
160164
}
161165

162166
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 );
163172
}
164173
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 );
166176
}
167177

168-
private CompletionStage<Void> performSaveWithId(
178+
private CompletionStage<Object> generateId(
169179
Object entity,
170-
C context,
171180
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+
} );
196199
}
197200

198201
/**
@@ -232,10 +235,7 @@ protected CompletionStage<Void> reactivePerformSave(
232235
if ( persister.getGenerator() instanceof Assigned ) {
233236
id = persister.getIdentifier( entity, source );
234237
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()'" ) );
239239
}
240240
}
241241

@@ -420,7 +420,7 @@ private CompletionStage<AbstractEntityInsertAction> addInsertAction(
420420
boolean useIdentityColumn,
421421
EventSource source,
422422
boolean shouldDelayIdentityInserts) {
423-
final ReactiveActionQueue actionQueue = source.unwrap( ReactiveSession.class ).getReactiveActionQueue();
423+
final ReactiveActionQueue actionQueue = source.unwrap(ReactiveSession.class).getReactiveActionQueue();
424424
if ( useIdentityColumn ) {
425425
final ReactiveEntityIdentityInsertAction insert = new ReactiveEntityIdentityInsertAction(
426426
values, entity, persister, false, source, shouldDelayIdentityInserts

0 commit comments

Comments
 (0)