HHH-14029 Generally improve handling of all post-load events

This commit is contained in:
Sanne Grinovero 2020-05-18 13:53:38 +01:00
parent a40ba7aff7
commit b28038e53d
3 changed files with 22 additions and 56 deletions

View File

@ -35,6 +35,7 @@ import org.hibernate.event.spi.PreLoadEventListener;
import org.hibernate.graph.spi.AttributeNodeImplementor;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.FastSessionServices;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
@ -488,6 +489,20 @@ public final class TwoPhaseLoad {
return null;
}
/**
* This method will be removed.
* @deprecated Use {@link #postLoad(Object, SharedSessionContractImplementor, PostLoadEvent)}
* instead.
*/
@Deprecated
public static void postLoad(
final Object entity,
final SharedSessionContractImplementor session,
final PostLoadEvent postLoadEvent,
final Iterable<PostLoadEventListener> postLoadEventListeners) {
postLoad( entity, session, postLoadEvent );
}
/**
* PostLoad cannot occur during initializeEntity, as that call occurs *before*
* the Set collections are added to the persistence context by Loader.
@ -500,42 +515,17 @@ public final class TwoPhaseLoad {
* @param session The Session
* @param postLoadEvent The (re-used) post-load event
*/
public static void postLoad(
final Object entity,
final SharedSessionContractImplementor session,
final PostLoadEvent postLoadEvent,
final Iterable<PostLoadEventListener> postLoadEventListeners) {
if ( session.isEventSource() ) {
final PersistenceContext persistenceContext
= session.getPersistenceContextInternal();
final EntityEntry entityEntry = persistenceContext.getEntry( entity );
postLoadEvent.setEntity( entity ).setId( entityEntry.getId() ).setPersister( entityEntry.getPersister() );
for ( PostLoadEventListener listener : postLoadEventListeners ) {
listener.onPostLoad( postLoadEvent );
}
}
}
/**
* This method will be removed.
* @deprecated Use {@link #postLoad(Object, SharedSessionContractImplementor, PostLoadEvent, Iterable)}
* instead.
*/
@Deprecated
public static void postLoad(
final Object entity,
final SharedSessionContractImplementor session,
final PostLoadEvent postLoadEvent) {
if ( session.isEventSource() ) {
final EntityEntry entityEntry = session.getPersistenceContextInternal().getEntry( entity );
final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory()
.getServiceRegistry()
.getService( EventListenerRegistry.class )
.getEventListenerGroup( EventType.POST_LOAD );
postLoadEvent.setEntity( entity ).setId( entityEntry.getId() ).setPersister( entityEntry.getPersister() );
postLoad( entity, session, postLoadEvent, listenerGroup.listeners() );
session.getFactory().getFastSessionServices().firePostLoadEvent( postLoadEvent );
}
}
private static boolean useMinimalPuts(SharedSessionContractImplementor session, EntityEntry entityEntry) {

View File

@ -1206,21 +1206,8 @@ public abstract class Loader {
// persistence context.
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
if ( hydratedObjects != null && hydratedObjects.size() > 0 ) {
final Iterable<PostLoadEventListener> postLoadEventListeners;
if ( session.isEventSource() ) {
final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory()
.getServiceRegistry()
.getService( EventListenerRegistry.class )
.getEventListenerGroup( EventType.POST_LOAD );
postLoadEventListeners = listenerGroup.listeners();
}
else {
postLoadEventListeners = Collections.emptyList();
}
for ( Object hydratedObject : hydratedObjects ) {
TwoPhaseLoad.postLoad( hydratedObject, session, post, postLoadEventListeners );
TwoPhaseLoad.postLoad( hydratedObject, session, post );
if ( afterLoadActions != null ) {
for ( AfterLoadAction afterLoadAction : afterLoadActions ) {
final EntityEntry entityEntry = persistenceContext.getEntry( hydratedObject );

View File

@ -293,20 +293,9 @@ public abstract class AbstractRowReader implements RowReader {
}
final SharedSessionContractImplementor session = context.getSession();
final Iterable<PostLoadEventListener> postLoadEventListeners;
if ( session.isEventSource() ) {
final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory()
.getServiceRegistry()
.getService( EventListenerRegistry.class )
.getEventListenerGroup( EventType.POST_LOAD );
postLoadEventListeners = listenerGroup.listeners();
}
else {
postLoadEventListeners = Collections.emptyList();
}
for ( HydratedEntityRegistration registration : hydratedEntityRegistrations ) {
TwoPhaseLoad.postLoad( registration.getInstance(), session, postLoadEvent, postLoadEventListeners );
TwoPhaseLoad.postLoad( registration.getInstance(), session, postLoadEvent );
if ( afterLoadActionList != null ) {
for ( AfterLoadAction afterLoadAction : afterLoadActionList ) {
afterLoadAction.afterLoad(