KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: EntityIdentityInsertAction.java,v 1.12 2005/05/27 03:53:58 oneovthafew Exp $
2
package org.hibernate.action;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.HibernateException;
7 import org.hibernate.engine.SessionImplementor;
8 import org.hibernate.event.PostInsertEvent;
9 import org.hibernate.event.PreInsertEvent;
10 import org.hibernate.persister.entity.EntityPersister;
11
12 public final class EntityIdentityInsertAction extends EntityAction {
13     private final Object JavaDoc[] state;
14     //private CacheEntry cacheEntry;
15
private Serializable JavaDoc generatedId;
16
17     public EntityIdentityInsertAction(Object JavaDoc[] state, Object JavaDoc instance, EntityPersister persister, SessionImplementor session)
18             throws HibernateException {
19         super( session, null, instance, persister );
20         this.state = state;
21     }
22
23     public void execute() throws HibernateException {
24         EntityPersister persister = getPersister();
25         SessionImplementor session = getSession();
26         Object JavaDoc instance = getInstance();
27
28         PreInsertEvent preEvent = new PreInsertEvent( instance, null, state, persister );
29         final boolean veto = session.getListeners().getPreInsertEventListener().onPreInsert( preEvent );
30
31         // Don't need to lock the cache here, since if someone
32
// else inserted the same pk first, the insert would fail
33

34         if ( !veto ) generatedId = persister.insert( state, instance, session );
35
36         //TODO: this bit actually has to be called after all cascades!
37
// but since identity insert is called *synchronously*,
38
// instead of asynchronously as other actions, it isn't
39
/*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
40             cacheEntry = new CacheEntry(object, persister, session);
41             persister.getCache().insert(generatedId, cacheEntry);
42         }*/

43         
44         PostInsertEvent postEvent = new PostInsertEvent( instance, generatedId, state, getPersister() );
45         session.getListeners().getPostInsertEventListener().onPostInsert( postEvent );
46
47         if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
48             getSession().getFactory().getStatisticsImplementor()
49                     .insertEntity( getPersister().getEntityName() );
50         }
51
52     }
53
54     //Make 100% certain that this is called before any subsequent ScheduledUpdate.afterTransactionCompletion()!!
55
public void afterTransactionCompletion(boolean success) throws HibernateException {
56         //TODO: reenable if we also fix the above todo
57
/*EntityPersister persister = getEntityPersister();
58         if ( success && persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
59             persister.getCache().afterInsert( getGeneratedId(), cacheEntry );
60         }*/

61     }
62
63     public boolean hasAfterTransactionCompletion() {
64         //TODO: simply remove this override
65
// if we fix the above todos
66
return false;
67     }
68
69     public final Serializable JavaDoc getGeneratedId() {
70         return generatedId;
71     }
72
73 }
74
75
76
77
78
79
80
81
Popular Tags