1 package org.hibernate.action; 3 4 import java.io.Serializable ; 5 6 import org.hibernate.AssertionFailure; 7 import org.hibernate.HibernateException; 8 import org.hibernate.cache.CacheException; 9 import org.hibernate.cache.CacheKey; 10 import org.hibernate.cache.CacheConcurrencyStrategy.SoftLock; 11 import org.hibernate.cache.entry.CacheEntry; 12 import org.hibernate.engine.EntityEntry; 13 import org.hibernate.engine.SessionFactoryImplementor; 14 import org.hibernate.engine.SessionImplementor; 15 import org.hibernate.event.PostUpdateEvent; 16 import org.hibernate.event.PreUpdateEvent; 17 import org.hibernate.persister.entity.EntityPersister; 18 19 public final class EntityUpdateAction extends EntityAction { 20 21 private final Object [] fields; 22 private final Object [] oldFields; 23 private final Object lastVersion; 24 private final Object nextVersion; 25 private final int[] dirtyFields; 26 private final boolean hasDirtyCollection; 27 private final Object [] updatedState; 28 private final Object rowId; 29 private Object cacheEntry; 30 private SoftLock lock; 31 32 public EntityUpdateAction(final Serializable id, 33 final Object [] fields, 34 final int[] dirtyProperties, 35 final boolean hasDirtyCollection, 36 final Object [] oldFields, 37 final Object lastVersion, 38 final Object nextVersion, 39 final Object instance, 40 final Object [] updatedState, 41 final Object rowId, 42 final EntityPersister persister, 43 final SessionImplementor session) throws HibernateException { 44 super( session, id, instance, persister ); 45 this.fields = fields; 46 this.oldFields = oldFields; 47 this.lastVersion = lastVersion; 48 this.nextVersion = nextVersion; 49 this.dirtyFields = dirtyProperties; 50 this.hasDirtyCollection = hasDirtyCollection; 51 this.updatedState = updatedState; 52 this.rowId = rowId; 53 } 54 55 public void execute() throws HibernateException { 56 Serializable id = getId(); 57 EntityPersister persister = getPersister(); 58 SessionImplementor session = getSession(); 59 Object instance = getInstance(); 60 61 PreUpdateEvent preEvent = new PreUpdateEvent( instance, id, fields, oldFields, persister ); 62 final boolean veto = session.getListeners() 63 .getPreUpdateEventListener() 64 .onPreUpdate( preEvent ); 65 66 final SessionFactoryImplementor factory = getSession().getFactory(); 67 68 final CacheKey ck; 69 if ( persister.hasCache() ) { 70 ck = new CacheKey( 71 id, 72 persister.getIdentifierType(), 73 persister.getRootEntityName(), 74 session.getEntityMode(), 75 session.getFactory() 76 ); 77 lock = persister.getCache().lock(ck, lastVersion); 78 } 79 else { 80 ck = null; 81 } 82 83 if ( !veto ) { 84 persister.update( 85 id, 86 fields, 87 dirtyFields, 88 hasDirtyCollection, 89 oldFields, 90 lastVersion, 91 instance, 92 rowId, 93 session 94 ); 95 } 96 97 EntityEntry entry = getSession().getPersistenceContext().getEntry( instance ); 101 if ( entry == null ) { 102 throw new AssertionFailure( "possible nonthreadsafe access to session" ); 103 } 104 entry.postUpdate( instance, updatedState, nextVersion ); 105 106 if ( persister.hasCache() ) { 107 if ( persister.isCacheInvalidationRequired() ) { 108 persister.getCache().evict(ck); 109 } 110 else { 111 CacheEntry ce = new CacheEntry( 113 fields, 114 persister, 115 persister.hasUninitializedLazyProperties( instance, session.getEntityMode() ), 116 getSession(), 117 instance 118 ); 119 cacheEntry = persister.getCacheEntryStructure().structure(ce); 120 boolean put = persister.getCache().update(ck, cacheEntry); 121 122 if ( put && factory.getStatistics().isStatisticsEnabled() ) { 123 factory.getStatisticsImplementor() 124 .secondLevelCachePut( getPersister().getCache().getRegionName() ); 125 } 126 127 } 128 } 129 130 PostUpdateEvent postEvent = new PostUpdateEvent( instance, id, fields, oldFields, persister ); 131 session.getListeners() 132 .getPostUpdateEventListener() 133 .onPostUpdate( postEvent ); 134 135 if ( factory.getStatistics().isStatisticsEnabled() && !veto ) { 136 factory.getStatisticsImplementor() 137 .updateEntity( getPersister().getEntityName() ); 138 } 139 } 140 141 public void afterTransactionCompletion(boolean success) throws CacheException { 142 EntityPersister persister = getPersister(); 143 if ( persister.hasCache() ) { 144 145 final CacheKey ck = new CacheKey( 146 getId(), 147 persister.getIdentifierType(), 148 persister.getRootEntityName(), 149 getSession().getEntityMode(), 150 getSession().getFactory() 151 ); 152 153 if ( success && !persister.isCacheInvalidationRequired() ) { 154 boolean put = persister.getCache().afterUpdate(ck, cacheEntry, nextVersion, lock ); 155 156 if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) { 157 getSession().getFactory().getStatisticsImplementor() 158 .secondLevelCachePut( getPersister().getCache().getRegionName() ); 159 } 160 } 161 else { 162 persister.getCache().release(ck, lock ); 163 } 164 } 165 } 166 167 } 168 169 170 171 172 173 174 175 | Popular Tags |