org.hibernate.event.spi.PostLoadEventListener Java Examples

The following examples show how to use org.hibernate.event.spi.PostLoadEventListener. 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: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Iterable<PostLoadEventListener> postLoadEventListeners(EventSource session) {
	return session
			.getFactory()
			.getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( EventType.POST_LOAD )
			.listeners();
}
 
Example #3
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;
}