org.hibernate.event.spi.PostLoadEvent Java Examples

The following examples show how to use org.hibernate.event.spi.PostLoadEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwoPhaseLoad.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * PostLoad cannot occur during initializeEntity, as that call occurs *before*
 * the Set collections are added to the persistence context by Loader.
 * Without the split, LazyInitializationExceptions can occur in the Entity's
 * postLoad if it acts upon the collection.
 *
 * HHH-6043
 *
 * @param entity The entity
 * @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) {

	if ( session.isEventSource() ) {
		final PersistenceContext persistenceContext
				= session.getPersistenceContext();
		final EntityEntry entityEntry = persistenceContext.getEntry( entity );

		postLoadEvent.setEntity( entity ).setId( entityEntry.getId() ).setPersister( entityEntry.getPersister() );

		final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory()
						.getServiceRegistry()
						.getService( EventListenerRegistry.class )
						.getEventListenerGroup( EventType.POST_LOAD );
		for ( PostLoadEventListener listener : listenerGroup.listeners() ) {
			listener.onPostLoad( postLoadEvent );
		}
	}
}
 
Example #2
Source File: AuditLogPostLoadEventListener.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void onPostLoad(PostLoadEvent event) {
    final Object entity = event.getEntity();
    final EntityPersister entityPersister = event.getPersister();

    if (entity instanceof Auditable) {
        Auditable auditable = (Auditable) entity;

        event.getSession().persist(
            new LoadEventLogEntry()
                .setCreatedBy(LoggedUser.get())
                .setEntityName(entityPersister.getEntityName())
                .setEntityId(String.valueOf(auditable.getId()))
        );
    }
}
 
Example #3
Source File: AbstractRowReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void postLoad(
		PostLoadEvent postLoadEvent,
		ResultSetProcessingContextImpl context,
		List<HydratedEntityRegistration> hydratedEntityRegistrations,
		List<AfterLoadAction> afterLoadActionList) {
	// Until this entire method is refactored w/ polymorphism, postLoad was
	// split off from initializeEntity.  It *must* occur after
	// endCollectionLoad to ensure the collection is in the
	// persistence context.
	if ( hydratedEntityRegistrations == null ) {
		return;
	}

	for ( HydratedEntityRegistration registration : hydratedEntityRegistrations ) {
		TwoPhaseLoad.postLoad( registration.getInstance(), context.getSession(), postLoadEvent );
		if ( afterLoadActionList != null ) {
			for ( AfterLoadAction afterLoadAction : afterLoadActionList ) {
				afterLoadAction.afterLoad(
						context.getSession(),
						registration.getInstance(),
						(Loadable) registration.getEntityReference().getEntityPersister()
				);
			}
		}
	}

}
 
Example #4
Source File: DefaultPostLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPostLoad(PostLoadEvent event) {
	final Object entity = event.getEntity();

	callbackRegistry.postLoad( entity );

	final EntityEntry entry = event.getSession().getPersistenceContext().getEntry( entity );
	if ( entry == null ) {
		throw new AssertionFailure( "possible non-threadsafe access to the session" );
	}

	final LockMode lockMode = entry.getLockMode();
	if ( LockMode.PESSIMISTIC_FORCE_INCREMENT.equals( lockMode ) ) {
		final EntityPersister persister = entry.getPersister();
		final Object nextVersion = persister.forceVersionIncrement(
				entry.getId(),
				entry.getVersion(),
				event.getSession()
		);
		entry.forceLocked( entity, nextVersion );
	}
	else if ( LockMode.OPTIMISTIC_FORCE_INCREMENT.equals( lockMode ) ) {
		final EntityIncrementVersionProcess incrementVersion = new EntityIncrementVersionProcess( entity, entry );
		event.getSession().getActionQueue().registerProcess( incrementVersion );
	}
	else if ( LockMode.OPTIMISTIC.equals( lockMode ) ) {
		final EntityVerifyVersionProcess verifyVersion = new EntityVerifyVersionProcess( entity, entry );
		event.getSession().getActionQueue().registerProcess( verifyVersion );
	}

	if ( event.getPersister().implementsLifecycle() ) {
		//log.debug( "calling onLoad()" );
		( (Lifecycle) event.getEntity() ).onLoad( event.getSession(), event.getId() );
	}

}
 
Example #5
Source File: PostLoadAuditListener.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onPostLoad( PostLoadEvent postLoadEvent )
{
    Object entity = postLoadEvent.getEntity();

    getAuditable( entity, "read" ).ifPresent( auditable ->
        auditManager.send( Audit.builder()
            .auditType( getAuditType() )
            .auditScope( auditable.scope() )
            .createdAt( LocalDateTime.now() )
            .createdBy( getCreatedBy() )
            .object( entity )
            .auditableEntity( new AuditableEntity( entity ) )
            .build() ) );
}
 
Example #6
Source File: HibernateQueryMetrics.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Override
public void onPostLoad(PostLoadEvent event) {
    registerQueryMetric(event.getSession().getFactory().getStatistics());
}
 
Example #7
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private Object convertCacheEntryToEntity(
		CacheEntry entry,
		Serializable entityId,
		EntityPersister persister,
		LoadEvent event,
		EntityKey entityKey) {

	final EventSource session = event.getSession();
	final SessionFactoryImplementor factory = session.getFactory();
	final EntityPersister subclassPersister;

	if ( traceEnabled ) {
		LOG.tracef(
				"Converting second-level cache entry [%s] into entity : %s",
				entry,
				MessageHelper.infoString( persister, entityId, factory )
		);
	}

	final Object entity;

	subclassPersister = factory.getEntityPersister( entry.getSubclass() );
	final Object optionalObject = event.getInstanceToLoad();
	entity = optionalObject == null
			? session.instantiate( subclassPersister, entityId )
			: optionalObject;

	// make it circular-reference safe
	TwoPhaseLoad.addUninitializedCachedEntity(
			entityKey,
			entity,
			subclassPersister,
			LockMode.NONE,
			entry.getVersion(),
			session
	);

	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final Object[] values;
	final Object version;
	final boolean isReadOnly;

	final Type[] types = subclassPersister.getPropertyTypes();
	// initializes the entity by (desired) side-effect
	values = ( (StandardCacheEntryImpl) entry ).assemble(
			entity, entityId, subclassPersister, session.getInterceptor(), session
	);
	if ( ( (StandardCacheEntryImpl) entry ).isDeepCopyNeeded() ) {
		TypeHelper.deepCopy(
				values,
				types,
				subclassPersister.getPropertyUpdateability(),
				values,
				session
		);
	}
	version = Versioning.getVersion( values, subclassPersister );
	LOG.tracef( "Cached Version : %s", version );

	final Object proxy = persistenceContext.getProxy( entityKey );
	if ( proxy != null ) {
		// there is already a proxy for this impl
		// only set the status to read-only if the proxy is read-only
		isReadOnly = ( (HibernateProxy) proxy ).getHibernateLazyInitializer().isReadOnly();
	}
	else {
		isReadOnly = session.isDefaultReadOnly();
	}

	persistenceContext.addEntry(
			entity,
			( isReadOnly ? Status.READ_ONLY : Status.MANAGED ),
			values,
			null,
			entityId,
			version,
			LockMode.NONE,
			true,
			subclassPersister,
			false
	);
	subclassPersister.afterInitialize( entity, session );
	persistenceContext.initializeNonLazyCollections();

	//PostLoad is needed for EJB3
	PostLoadEvent postLoadEvent = event.getPostLoadEvent()
			.setEntity( entity )
			.setId( entityId )
			.setPersister( persister );

	for ( PostLoadEventListener listener : postLoadEventListeners( session ) ) {
		listener.onPostLoad( postLoadEvent );
	}

	return entity;
}
 
Example #8
Source File: ClosureEventTriggeringInterceptor.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
public void onPostLoad(PostLoadEvent hibernateEvent) {
    Object entity = hibernateEvent.getEntity();
    activateDirtyChecking(entity);
    publishEvent(hibernateEvent, new org.grails.datastore.mapping.engine.event.PostLoadEvent(
            this.datastore, entity));
}