1 package org.hibernate.event.def; 3 4 import java.io.Serializable ; 5 import java.util.Map ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.hibernate.HibernateException; 10 import org.hibernate.PersistentObjectException; 11 import org.hibernate.UnresolvableObjectException; 12 import org.hibernate.cache.CacheKey; 13 import org.hibernate.engine.Cascade; 14 import org.hibernate.engine.CascadingAction; 15 import org.hibernate.engine.EntityEntry; 16 import org.hibernate.engine.EntityKey; 17 import org.hibernate.engine.SessionFactoryImplementor; 18 import org.hibernate.event.EventSource; 19 import org.hibernate.event.RefreshEvent; 20 import org.hibernate.event.RefreshEventListener; 21 import org.hibernate.persister.entity.EntityPersister; 22 import org.hibernate.pretty.MessageHelper; 23 import org.hibernate.type.AbstractComponentType; 24 import org.hibernate.type.CollectionType; 25 import org.hibernate.type.Type; 26 import org.hibernate.util.IdentityMap; 27 28 34 public class DefaultRefreshEventListener extends AbstractEventListener implements RefreshEventListener { 35 36 private static final Log log = LogFactory.getLog(DefaultRefreshEventListener.class); 37 38 public void onRefresh(RefreshEvent event) throws HibernateException { 39 onRefresh( event, IdentityMap.instantiate(10) ); 40 } 41 42 48 public void onRefresh(RefreshEvent event, Map refreshedAlready) throws HibernateException { 49 50 final EventSource source = event.getSession(); 51 52 if ( source.getPersistenceContext().reassociateIfUninitializedProxy( event.getObject() ) ) return; 53 54 final Object object = source.getPersistenceContext().unproxyAndReassociate( event.getObject() ); 55 56 if ( refreshedAlready.containsKey(object) ) { 57 log.trace("already refreshed"); 58 return; 59 } 60 61 final EntityEntry e = source.getPersistenceContext().getEntry( object ); 62 final EntityPersister persister; 63 final Serializable id; 64 65 if ( e == null ) { 66 persister = source.getEntityPersister(null, object); id = persister.getIdentifier( object, event.getSession().getEntityMode() ); 68 if ( log.isTraceEnabled() ) { 69 log.trace( 70 "refreshing transient " + 71 MessageHelper.infoString( persister, id, source.getFactory() ) 72 ); 73 } 74 if ( source.getPersistenceContext().getEntry( new EntityKey( id, persister, source.getEntityMode() ) ) != null ) { 75 throw new PersistentObjectException( 76 "attempted to refresh transient instance when persistent instance was already associated with the Session: " + 77 MessageHelper.infoString(persister, id, source.getFactory() ) 78 ); 79 } 80 } 81 else { 82 if ( log.isTraceEnabled() ) { 83 log.trace( 84 "refreshing " + 85 MessageHelper.infoString( e.getPersister(), e.getId(), source.getFactory() ) 86 ); 87 } 88 if ( !e.isExistsInDatabase() ) { 89 throw new HibernateException( "this instance does not yet exist as a row in the database" ); 90 } 91 92 persister = e.getPersister(); 93 id = e.getId(); 94 } 95 96 refreshedAlready.put(object, object); 98 new Cascade(CascadingAction.REFRESH, Cascade.AFTER_REFRESH, source) 99 .cascade( persister, object, refreshedAlready ); 100 101 if ( e != null ) { 102 EntityKey key = new EntityKey( id, persister, source.getEntityMode() ); 103 source.getPersistenceContext().removeEntity(key); 104 if ( persister.hasCollections() ) new EvictVisitor( source ).process(object, persister); 105 } 106 107 if ( persister.hasCache() ) { 108 final CacheKey ck = new CacheKey( 109 id, 110 persister.getIdentifierType(), 111 persister.getRootEntityName(), 112 source.getEntityMode(), 113 source.getFactory() 114 ); 115 persister.getCache().remove(ck); 116 } 117 evictCachedCollections( persister, id, source.getFactory() ); 118 Object result = persister.load( id, object, event.getLockMode(), source ); 119 UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() ); 120 121 } 122 123 126 private void evictCachedCollections(EntityPersister persister, Serializable id, SessionFactoryImplementor factory) 127 throws HibernateException { 128 evictCachedCollections( persister.getPropertyTypes(), id, factory ); 129 } 130 131 private void evictCachedCollections(Type[] types, Serializable id, SessionFactoryImplementor factory) 132 throws HibernateException { 133 for ( int i = 0; i < types.length; i++ ) { 134 if ( types[i].isCollectionType() ) { 135 factory.evictCollection( ( (CollectionType) types[i] ).getRole(), id ); 136 } 137 else if ( types[i].isComponentType() ) { 138 AbstractComponentType actype = (AbstractComponentType) types[i]; 139 evictCachedCollections( actype.getSubtypes(), id, factory ); 140 } 141 } 142 } 143 144 } 145 | Popular Tags |