KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: DefaultUpdateEventListener.java,v 1.2 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.HibernateException;
7 import org.hibernate.ObjectDeletedException;
8 import org.hibernate.EntityMode;
9 import org.hibernate.engine.EntityEntry;
10 import org.hibernate.engine.Status;
11 import org.hibernate.event.SaveOrUpdateEvent;
12 import org.hibernate.persister.entity.EntityPersister;
13
14 /**
15  * An event handler for update() events
16  * @author Gavin King
17  */

18 public class DefaultUpdateEventListener extends DefaultSaveOrUpdateEventListener {
19
20     protected Serializable JavaDoc performSaveOrUpdate(SaveOrUpdateEvent event) {
21         // this implementation is supposed to tolerate incorrect unsaved-value
22
// mappings, for the purpose of backward-compatibility
23
EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
24         if ( entry!=null ) {
25             if ( entry.getStatus()==Status.DELETED ) {
26                 throw new ObjectDeletedException( "deleted instance passed to update()", null, event.getEntityName() );
27             }
28             else {
29                 return entityIsPersistent(event);
30             }
31         }
32         else {
33             entityIsDetached(event);
34             return null;
35         }
36     }
37     
38     /**
39      * If the user specified an id, assign it to the instance and use that,
40      * otherwise use the id already assigned to the instance
41      */

42     protected Serializable JavaDoc getUpdateId(Object JavaDoc entity, EntityPersister persister, Serializable JavaDoc requestedId, EntityMode entityMode)
43     throws HibernateException {
44
45         if ( requestedId==null ) {
46             return super.getUpdateId(entity, persister, requestedId, entityMode);
47         }
48         else {
49             persister.setIdentifier(entity, requestedId, entityMode);
50             return requestedId;
51         }
52     }
53
54 }
55
Popular Tags