KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: DefaultSaveEventListener.java,v 1.3 2005/02/22 03:09:35 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.Hibernate;
7 import org.hibernate.PersistentObjectException;
8 import org.hibernate.engine.EntityEntry;
9 import org.hibernate.engine.Status;
10 import org.hibernate.event.SaveOrUpdateEvent;
11 import org.hibernate.engine.SessionImplementor;
12
13 /**
14  * An event handler for save() events
15  * @author Gavin King
16  */

17 public class DefaultSaveEventListener extends DefaultSaveOrUpdateEventListener {
18
19     protected Serializable JavaDoc performSaveOrUpdate(SaveOrUpdateEvent event) {
20         // this implementation is supposed to tolerate incorrect unsaved-value
21
// mappings, for the purpose of backward-compatibility
22
EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
23         if ( entry!=null && entry.getStatus() != Status.DELETED ) {
24             return entityIsPersistent(event);
25         }
26         else {
27             return entityIsTransient(event);
28         }
29     }
30     
31     protected Serializable JavaDoc saveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) {
32         if ( event.getRequestedId() == null ) {
33             return super.saveWithGeneratedOrRequestedId(event);
34         }
35         else {
36             return saveWithRequestedId(
37                     event.getEntity(),
38                     event.getRequestedId(),
39                     event.getEntityName(),
40                     null,
41                     event.getSession()
42             );
43         }
44         
45     }
46
47     protected boolean reassociateIfUninitializedProxy(Object JavaDoc object, SessionImplementor source) {
48         if ( !Hibernate.isInitialized(object) ) {
49             throw new PersistentObjectException("uninitialized proxy passed to save()");
50         }
51         else {
52             return false;
53         }
54     }
55     
56
57 }
58
Popular Tags