FileDocCategorySizeDatePackage
DefaultRefreshEventListener.javaAPI DocHibernate 3.2.55198Mon Aug 08 18:24:44 BST 2005org.hibernate.event.def

DefaultRefreshEventListener

public class DefaultRefreshEventListener extends Object implements org.hibernate.event.RefreshEventListener
Defines the default refresh event listener used by hibernate for refreshing entities in response to generated refresh events.
author
Steve Ebersole

Fields Summary
private static final Log
log
Constructors Summary
Methods Summary
private voidevictCachedCollections(org.hibernate.persister.entity.EntityPersister persister, java.io.Serializable id, org.hibernate.engine.SessionFactoryImplementor factory)
Evict collections from the factory-level cache

		evictCachedCollections( persister.getPropertyTypes(), id, factory );
	
private voidevictCachedCollections(org.hibernate.type.Type[] types, java.io.Serializable id, org.hibernate.engine.SessionFactoryImplementor factory)

		for ( int i = 0; i < types.length; i++ ) {
			if ( types[i].isCollectionType() ) {
				factory.evictCollection( ( (CollectionType) types[i] ).getRole(), id );
			}
			else if ( types[i].isComponentType() ) {
				AbstractComponentType actype = (AbstractComponentType) types[i];
				evictCachedCollections( actype.getSubtypes(), id, factory );
			}
		}
	
public voidonRefresh(org.hibernate.event.RefreshEvent event)

	
	      
		onRefresh( event, IdentityMap.instantiate(10) );
	
public voidonRefresh(org.hibernate.event.RefreshEvent event, java.util.Map refreshedAlready)
Handle the given refresh event.

param
event The refresh event to be handled.
throws
HibernateException


		final EventSource source = event.getSession();
		
		if ( source.getPersistenceContext().reassociateIfUninitializedProxy( event.getObject() ) ) return;

		final Object object = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );

		if ( refreshedAlready.containsKey(object) ) {
			log.trace("already refreshed");
			return;
		}

		final EntityEntry e = source.getPersistenceContext().getEntry( object );
		final EntityPersister persister;
		final Serializable id;
		
		if ( e == null ) {
			persister = source.getEntityPersister(null, object); //refresh() does not pass an entityName
			id = persister.getIdentifier( object, event.getSession().getEntityMode() );
			if ( log.isTraceEnabled() ) {
				log.trace(
						"refreshing transient " +
						MessageHelper.infoString( persister, id, source.getFactory() )
					);
			}
			EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
			if ( source.getPersistenceContext().getEntry(key) != null ) {
				throw new PersistentObjectException(
						"attempted to refresh transient instance when persistent instance was already associated with the Session: " +
						MessageHelper.infoString(persister, id, source.getFactory() )
					);
			}
		}
		else {
			if ( log.isTraceEnabled() ) {
				log.trace(
						"refreshing " +
						MessageHelper.infoString( e.getPersister(), e.getId(), source.getFactory()  )
					);
			}
			if ( !e.isExistsInDatabase() ) {
				throw new HibernateException( "this instance does not yet exist as a row in the database" );
			}

			persister = e.getPersister();
			id = e.getId();
		}

		// cascade the refresh prior to refreshing this entity
		refreshedAlready.put(object, object);
		new Cascade(CascadingAction.REFRESH, Cascade.BEFORE_REFRESH, source)
				.cascade( persister, object, refreshedAlready );

		if ( e != null ) {
			EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
			source.getPersistenceContext().removeEntity(key);
			if ( persister.hasCollections() ) new EvictVisitor( source ).process(object, persister);
		}

		if ( persister.hasCache() ) {
			final CacheKey ck = new CacheKey(
					id,
					persister.getIdentifierType(),
					persister.getRootEntityName(),
					source.getEntityMode(), 
					source.getFactory()
				);
			persister.getCache().remove(ck);
		}
		
		evictCachedCollections( persister, id, source.getFactory() );
		
		String previousFetchProfile = source.getFetchProfile();
		source.setFetchProfile("refresh");
		Object result = persister.load( id, object, event.getLockMode(), source );
		source.setFetchProfile(previousFetchProfile);
		
		UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );