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.CacheKey; 9 import org.hibernate.cache.entry.CacheEntry; 10 import org.hibernate.engine.EntityEntry; 11 import org.hibernate.engine.SessionFactoryImplementor; 12 import org.hibernate.engine.SessionImplementor; 13 import org.hibernate.event.PostInsertEvent; 14 import org.hibernate.event.PreInsertEvent; 15 import org.hibernate.persister.entity.EntityPersister; 16 17 public final class EntityInsertAction extends EntityAction { 18 private final Object [] state; 19 private Object cacheEntry; 20 private final Object version; 21 22 public EntityInsertAction(Serializable id, 23 Object [] state, 24 Object instance, 25 Object version, 26 EntityPersister persister, 27 SessionImplementor session) 28 throws HibernateException { 29 super( session, id, instance, persister ); 30 this.state = state; 31 this.version = version; 32 } 33 34 public void execute() throws HibernateException { 35 EntityPersister persister = getPersister(); 36 SessionImplementor session = getSession(); 37 Object instance = getInstance(); 38 Serializable id = getId(); 39 40 PreInsertEvent preEvent = new PreInsertEvent( instance, id, state, persister ); 41 final boolean veto = session.getListeners() 42 .getPreInsertEventListener() 43 .onPreInsert( preEvent ); 44 45 48 if ( !veto ) persister.insert( id, state, instance, session ); 49 50 EntityEntry entry = session.getPersistenceContext().getEntry( instance ); 54 if ( entry == null ) { 55 throw new AssertionFailure( "possible nonthreadsafe access to session" ); 56 } 57 entry.postInsert(); 58 59 final SessionFactoryImplementor factory = getSession().getFactory(); 60 61 if ( isCachePutEnabled( persister, session ) ) { 62 63 CacheEntry ce = new CacheEntry( 64 state, 65 persister, 66 persister.hasUninitializedLazyProperties( instance, session.getEntityMode() ), 67 session, 68 instance 69 ); 70 71 cacheEntry = persister.getCacheEntryStructure().structure(ce); 72 final CacheKey ck = new CacheKey( 73 id, 74 persister.getIdentifierType(), 75 persister.getRootEntityName(), 76 session.getEntityMode(), 77 session.getFactory() 78 ); 79 boolean put = persister.getCache().insert(ck, cacheEntry); 80 81 if ( put && factory.getStatistics().isStatisticsEnabled() ) { 82 factory.getStatisticsImplementor() 83 .secondLevelCachePut( getPersister().getCache().getRegionName() ); 84 } 85 86 } 87 88 PostInsertEvent postEvent = new PostInsertEvent( instance, id, state, persister ); 89 session.getListeners() 90 .getPostInsertEventListener() 91 .onPostInsert( postEvent ); 92 93 if ( factory.getStatistics().isStatisticsEnabled() && !veto ) { 94 factory.getStatisticsImplementor() 95 .insertEntity( getPersister().getEntityName() ); 96 } 97 98 } 99 100 public void afterTransactionCompletion(boolean success) throws HibernateException { 102 EntityPersister persister = getPersister(); 103 if ( success && isCachePutEnabled( persister, getSession() ) ) { 104 final CacheKey ck = new CacheKey( 105 getId(), 106 persister.getIdentifierType(), 107 persister.getRootEntityName(), 108 getSession().getEntityMode(), 109 getSession().getFactory() 110 ); 111 boolean put = persister.getCache().afterInsert(ck, cacheEntry, version ); 112 113 if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) { 114 getSession().getFactory().getStatisticsImplementor() 115 .secondLevelCachePut( getPersister().getCache().getRegionName() ); 116 } 117 } 118 } 119 120 private boolean isCachePutEnabled(EntityPersister persister, SessionImplementor session) { 121 return persister.hasCache() && 122 !persister.isCacheInvalidationRequired() && 123 session.getCacheMode().isPutEnabled(); 124 } 125 126 } 127 128 129 130 131 132 133 134 | Popular Tags |