KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > action > EntityInsertAction


1 //$Id: EntityInsertAction.java,v 1.25 2005/06/15 13:20:12 oneovthafew Exp $
2
package org.hibernate.action;
3
4 import java.io.Serializable JavaDoc;
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 JavaDoc[] state;
19     private Object JavaDoc cacheEntry;
20     private final Object JavaDoc version;
21
22     public EntityInsertAction(Serializable JavaDoc id,
23                               Object JavaDoc[] state,
24                               Object JavaDoc instance,
25                               Object JavaDoc 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 JavaDoc instance = getInstance();
38         Serializable JavaDoc 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         // Don't need to lock the cache here, since if someone
46
// else inserted the same pk first, the insert would fail
47

48         if ( !veto ) persister.insert( id, state, instance, session );
49         
50         //postInsert:
51
// After actually inserting a row, record the fact that the instance exists on the
52
// database (needed for identity-column key generation)
53
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     //Make 100% certain that this is called before any subsequent ScheduledUpdate.afterTransactionCompletion()!!
101
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