KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > event > def > DefaultPersistEventListener


1 // $Id: DefaultPersistEventListener.java,v 1.6 2005/07/15 03:35:18 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import java.util.Map JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.hibernate.HibernateException;
9 import org.hibernate.ObjectDeletedException;
10 import org.hibernate.PersistentObjectException;
11 import org.hibernate.engine.CascadingAction;
12 import org.hibernate.event.EventSource;
13 import org.hibernate.event.PersistEvent;
14 import org.hibernate.event.PersistEventListener;
15 import org.hibernate.engine.SessionImplementor;
16 import org.hibernate.persister.entity.EntityPersister;
17 import org.hibernate.proxy.HibernateProxy;
18 import org.hibernate.proxy.LazyInitializer;
19 import org.hibernate.util.IdentityMap;
20
21 /**
22  * Defines the default create event listener used by hibernate for creating
23  * transient entities in response to generated create events.
24  *
25  * @author Gavin King
26  */

27 public class DefaultPersistEventListener extends AbstractSaveEventListener implements PersistEventListener {
28
29     private static final Log log = LogFactory.getLog(DefaultPersistEventListener.class);
30
31     /**
32      * Handle the given create event.
33      *
34      * @param event The create event to be handled.
35      * @throws HibernateException
36      */

37     public void onPersist(PersistEvent event) throws HibernateException {
38         onPersist( event, IdentityMap.instantiate(10) );
39     }
40         
41
42     /**
43      * Handle the given create event.
44      *
45      * @param event The create event to be handled.
46      * @throws HibernateException
47      */

48     public void onPersist(PersistEvent event, Map JavaDoc createCache) throws HibernateException {
49             
50         final SessionImplementor source = event.getSession();
51         final Object JavaDoc object = event.getObject();
52         
53         final Object JavaDoc entity;
54         if (object instanceof HibernateProxy) {
55             LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
56             if ( li.isUninitialized() ) {
57                 if ( li.getSession()==source ) {
58                     return; //NOTE EARLY EXIT!
59
}
60                 else {
61                     throw new PersistentObjectException("uninitialized proxy passed to persist()");
62                 }
63             }
64             entity = li.getImplementation();
65         }
66         else {
67             entity = object;
68         }
69         
70         int entityState = getEntityState(
71                 entity,
72                 event.getEntityName(),
73                 source.getPersistenceContext().getEntry(entity),
74                 source
75             );
76         
77         switch (entityState) {
78             case DETACHED:
79                 throw new PersistentObjectException( "detached entity passed to persist: " + event.getEntityName() );
80             case PERSISTENT:
81                 entityIsPersistent(event, createCache);
82                 break;
83             case TRANSIENT:
84                 entityIsTransient(event, createCache);
85                 break;
86             default:
87                 throw new ObjectDeletedException( "deleted entity passed to persist", null, event.getEntityName() );
88         }
89
90     }
91         
92     protected void entityIsPersistent(PersistEvent event, Map JavaDoc createCache) {
93         log.trace("ignoring persistent instance");
94         final EventSource source = event.getSession();
95         
96         //TODO: check that entry.getIdentifier().equals(requestedId)
97

98         final Object JavaDoc entity = source.getPersistenceContext().unproxy( event.getObject() );
99         final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
100         
101         if ( createCache.put(entity, entity)==null ) {
102             //TODO: merge into one method!
103
cascadeBeforeSave(source, persister, entity, createCache);
104             cascadeAfterSave(source, persister, entity, createCache);
105         }
106
107     }
108     
109     /**
110      * Handle the given create event.
111      *
112      * @param event The save event to be handled.
113      * @throws HibernateException
114      */

115     protected void entityIsTransient(PersistEvent event, Map JavaDoc createCache) throws HibernateException {
116         
117         log.trace("saving transient instance");
118
119         final EventSource source = event.getSession();
120         
121         final Object JavaDoc entity = source.getPersistenceContext().unproxy( event.getObject() );
122         
123         if ( createCache.put(entity, entity)==null ) {
124             saveWithGeneratedId( entity, event.getEntityName(), createCache, source );
125         }
126
127     }
128
129     protected CascadingAction getCascadeAction() {
130         return CascadingAction.PERSIST;
131     }
132     
133     protected Boolean JavaDoc getAssumedUnsaved() {
134         return Boolean.TRUE;
135     }
136
137 }
138
Popular Tags