1 package org.hibernate.event.def; 3 4 import java.util.Map ; 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 27 public class DefaultPersistEventListener extends AbstractSaveEventListener implements PersistEventListener { 28 29 private static final Log log = LogFactory.getLog(DefaultPersistEventListener.class); 30 31 37 public void onPersist(PersistEvent event) throws HibernateException { 38 onPersist( event, IdentityMap.instantiate(10) ); 39 } 40 41 42 48 public void onPersist(PersistEvent event, Map createCache) throws HibernateException { 49 50 final SessionImplementor source = event.getSession(); 51 final Object object = event.getObject(); 52 53 final Object entity; 54 if (object instanceof HibernateProxy) { 55 LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); 56 if ( li.isUninitialized() ) { 57 if ( li.getSession()==source ) { 58 return; } 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 createCache) { 93 log.trace("ignoring persistent instance"); 94 final EventSource source = event.getSession(); 95 96 98 final Object entity = source.getPersistenceContext().unproxy( event.getObject() ); 99 final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity ); 100 101 if ( createCache.put(entity, entity)==null ) { 102 cascadeBeforeSave(source, persister, entity, createCache); 104 cascadeAfterSave(source, persister, entity, createCache); 105 } 106 107 } 108 109 115 protected void entityIsTransient(PersistEvent event, Map createCache) throws HibernateException { 116 117 log.trace("saving transient instance"); 118 119 final EventSource source = event.getSession(); 120 121 final Object 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 getAssumedUnsaved() { 134 return Boolean.TRUE; 135 } 136 137 } 138 | Popular Tags |