|
12 | 12 |
|
13 | 13 | import org.hibernate.HibernateException;
|
14 | 14 | import org.hibernate.collection.spi.PersistentCollection;
|
| 15 | +import org.hibernate.engine.spi.EntityHolder; |
15 | 16 | import org.hibernate.engine.spi.EntityKey;
|
16 | 17 | import org.hibernate.engine.spi.PersistenceContext;
|
17 | 18 | import org.hibernate.engine.spi.SessionImplementor;
|
18 | 19 | import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
| 20 | +import org.hibernate.event.service.spi.EventListenerGroup; |
| 21 | +import org.hibernate.event.spi.PostLoadEvent; |
| 22 | +import org.hibernate.event.spi.PostLoadEventListener; |
19 | 23 | import org.hibernate.persister.entity.EntityPersister;
|
| 24 | +import org.hibernate.reactive.engine.impl.ReactiveCallbackImpl; |
20 | 25 | import org.hibernate.reactive.logging.impl.Log;
|
21 | 26 | import org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister;
|
22 | 27 | import org.hibernate.reactive.session.ReactiveSession;
|
| 28 | +import org.hibernate.sql.exec.spi.Callback; |
| 29 | +import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState; |
23 | 30 |
|
24 | 31 | import static java.lang.invoke.MethodHandles.lookup;
|
25 | 32 | import static org.hibernate.pretty.MessageHelper.infoString;
|
26 | 33 | import static org.hibernate.reactive.logging.impl.LoggerFactory.make;
|
27 | 34 | import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture;
|
| 35 | +import static org.hibernate.reactive.util.impl.CompletionStages.loop; |
28 | 36 | import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
|
29 | 37 |
|
30 | 38 | /**
|
@@ -130,4 +138,89 @@ public Object removeEntity(EntityKey key) {
|
130 | 138 | }
|
131 | 139 | return result;
|
132 | 140 | }
|
| 141 | + |
| 142 | + @Override |
| 143 | + public void postLoad(JdbcValuesSourceProcessingState processingState, Consumer<EntityHolder> holderConsumer) { |
| 144 | + throw LOG.nonReactiveMethodCall( "reactivePostLoad(JdbcValuesSourceProcessingState, Consumer<EntityHolder>) )" ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Reactive version of {@link StatefulPersistenceContext#postLoad(JdbcValuesSourceProcessingState, Consumer)} |
| 149 | + * |
| 150 | + */ |
| 151 | + public CompletionStage<Void> reactivePostLoad(JdbcValuesSourceProcessingState processingState, Consumer<EntityHolder> holderConsumer) { |
| 152 | + final ReactiveCallbackImpl callback = (ReactiveCallbackImpl) processingState.getExecutionContext().getCallback(); |
| 153 | + |
| 154 | + if ( processingState.getLoadingEntityHolders() != null ) { |
| 155 | + final EventListenerGroup<PostLoadEventListener> listenerGroup = |
| 156 | + getSession().getFactory().getEventListenerGroups().eventListenerGroup_POST_LOAD; |
| 157 | + final PostLoadEvent postLoadEvent = processingState.getPostLoadEvent(); |
| 158 | + return loop( |
| 159 | + processingState.getLoadingEntityHolders(), entityHolder -> |
| 160 | + processLoadedEntityHolder( |
| 161 | + entityHolder, |
| 162 | + listenerGroup, |
| 163 | + postLoadEvent, |
| 164 | + callback, |
| 165 | + holderConsumer |
| 166 | + ) |
| 167 | + ).thenAccept( unused -> processingState.getLoadingEntityHolders().clear() ); |
| 168 | + } |
| 169 | + if ( processingState.getReloadedEntityHolders() != null ) { |
| 170 | + return loop( |
| 171 | + processingState.getLoadingEntityHolders(), entityHolder -> |
| 172 | + processLoadedEntityHolder( |
| 173 | + entityHolder, |
| 174 | + null, |
| 175 | + null, |
| 176 | + callback, |
| 177 | + holderConsumer |
| 178 | + ) |
| 179 | + ).thenAccept( unused -> processingState.getLoadingEntityHolders().clear() ); |
| 180 | + } |
| 181 | + return voidFuture(); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Reactive version of {@link StatefulPersistenceContext#processLoadedEntityHolder(EntityHolder, EventListenerGroup, PostLoadEvent, Callback, Consumer)} |
| 186 | + */ |
| 187 | + private CompletionStage<Void> processLoadedEntityHolder( |
| 188 | + EntityHolder holder, |
| 189 | + EventListenerGroup<PostLoadEventListener> listenerGroup, |
| 190 | + PostLoadEvent postLoadEvent, |
| 191 | + ReactiveCallbackImpl callback, |
| 192 | + Consumer<EntityHolder> holderConsumer) { |
| 193 | + if ( holderConsumer != null ) { |
| 194 | + holderConsumer.accept( holder ); |
| 195 | + } |
| 196 | + if ( holder.getEntity() == null ) { |
| 197 | + // It's possible that we tried to load an entity and found out it doesn't exist, |
| 198 | + // in which case we added an entry with a null proxy and entity. |
| 199 | + // Remove that empty entry on post load to avoid unwanted side effects |
| 200 | + getEntitiesByKey().remove( holder.getEntityKey() ); |
| 201 | + } |
| 202 | + else { |
| 203 | + if ( postLoadEvent != null ) { |
| 204 | + postLoadEvent.reset(); |
| 205 | + postLoadEvent.setEntity( holder.getEntity() ) |
| 206 | + .setId( holder.getEntityKey().getIdentifier() ) |
| 207 | + .setPersister( holder.getDescriptor() ); |
| 208 | + listenerGroup.fireEventOnEachListener( |
| 209 | + postLoadEvent, |
| 210 | + PostLoadEventListener::onPostLoad |
| 211 | + ); |
| 212 | + if ( callback != null ) { |
| 213 | + return callback.invokeReactiveLoadActions( |
| 214 | + holder.getEntity(), |
| 215 | + holder.getDescriptor(), |
| 216 | + getSession() |
| 217 | + ).thenAccept( v -> |
| 218 | + holder.resetEntityInitialier() |
| 219 | + ); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + } |
| 224 | + return voidFuture(); |
| 225 | + } |
133 | 226 | }
|
0 commit comments