|
35 | 35 | import org.hibernate.reactive.logging.impl.Log; |
36 | 36 | import org.hibernate.reactive.logging.impl.LoggerFactory; |
37 | 37 | import org.hibernate.reactive.session.ReactiveSession; |
| 38 | +import org.hibernate.reactive.util.impl.CompletionStages; |
38 | 39 | import org.hibernate.type.Type; |
39 | 40 | import org.hibernate.type.TypeHelper; |
40 | 41 |
|
@@ -162,34 +163,40 @@ protected CompletionStage<Void> reactivePerformSave( |
162 | 163 | LOG.tracev( "Saving {0}", infoString( persister, id, source.getFactory() ) ); |
163 | 164 | } |
164 | 165 |
|
165 | | - final EntityKey key; |
166 | | - if ( !useIdentityColumn ) { |
167 | | - key = source.generateEntityKey( id, persister ); |
168 | | - final PersistenceContext persistenceContext = source.getPersistenceContextInternal(); |
169 | | - Object old = persistenceContext.getEntity( key ); |
170 | | - if ( old != null ) { |
171 | | - if ( persistenceContext.getEntry( old ).getStatus() == Status.DELETED ) { |
172 | | - source.forceFlush( persistenceContext.getEntry( old ) ); |
173 | | - } |
174 | | - else { |
175 | | - return failedFuture( new NonUniqueObjectException( id, persister.getEntityName() ) ); |
176 | | - } |
177 | | - } |
| 166 | + CompletionStage<EntityKey> keyStage = useIdentityColumn |
| 167 | + ? CompletionStages.nullFuture() |
| 168 | + : generateEntityKey( id, persister, source ).thenCompose( generatedKey -> { |
178 | 169 | persister.setIdentifier( entity, id, source ); |
179 | | - } |
180 | | - else { |
181 | | - key = null; |
182 | | - } |
| 170 | + return CompletionStages.completedFuture( generatedKey ); |
| 171 | + } ); |
183 | 172 |
|
184 | | - return reactivePerformSaveOrReplicate( |
| 173 | + return keyStage.thenCompose( key -> reactivePerformSaveOrReplicate( |
185 | 174 | entity, |
186 | 175 | key, |
187 | 176 | persister, |
188 | 177 | useIdentityColumn, |
189 | 178 | context, |
190 | 179 | source, |
191 | 180 | requiresImmediateIdAccess |
192 | | - ); |
| 181 | + ) ); |
| 182 | + } |
| 183 | + |
| 184 | + private CompletionStage<EntityKey> generateEntityKey(Serializable id, EntityPersister persister, |
| 185 | + EventSource source) { |
| 186 | + final EntityKey key = source.generateEntityKey( id, persister ); |
| 187 | + final PersistenceContext persistenceContext = source.getPersistenceContextInternal(); |
| 188 | + Object old = persistenceContext.getEntity( key ); |
| 189 | + if ( old != null ) { |
| 190 | + if ( persistenceContext.getEntry( old ).getStatus() == Status.DELETED ) { |
| 191 | + return source.unwrap( ReactiveSession.class ).reactiveForceFlush( |
| 192 | + persistenceContext.getEntry( old ) ).thenApply( v -> key ); |
| 193 | + } |
| 194 | + else { |
| 195 | + return failedFuture( new NonUniqueObjectException( id, persister.getEntityName() ) ); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return CompletionStages.completedFuture( key ); |
193 | 200 | } |
194 | 201 |
|
195 | 202 | /** |
|
0 commit comments