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