1 package org.hibernate.event.def; 3 4 import java.io.Serializable ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.hibernate.HibernateException; 9 import org.hibernate.LockMode; 10 import org.hibernate.engine.EntityEntry; 11 import org.hibernate.engine.EntityKey; 12 import org.hibernate.engine.Status; 13 import org.hibernate.engine.Versioning; 14 import org.hibernate.event.AbstractEvent; 15 import org.hibernate.event.EventSource; 16 import org.hibernate.persister.entity.EntityPersister; 17 import org.hibernate.pretty.MessageHelper; 18 import org.hibernate.type.TypeFactory; 19 20 26 public class AbstractReassociateEventListener extends AbstractEventListener { 27 28 private static final Log log = LogFactory.getLog(AbstractReassociateEventListener.class); 29 30 41 protected final EntityEntry reassociate(AbstractEvent event, Object object, Serializable id, EntityPersister persister) 42 throws HibernateException { 43 44 if ( log.isTraceEnabled() ) log.trace( 45 "reassociating transient instance: " + 46 MessageHelper.infoString( persister, id, event.getSession().getFactory() ) 47 ); 48 49 EventSource source = event.getSession(); 50 EntityKey key = new EntityKey( id, persister, source.getEntityMode() ); 51 52 source.getPersistenceContext().checkUniqueness(key, object); 53 54 Object [] values = persister.getPropertyValues( object, source.getEntityMode() ); 56 TypeFactory.deepCopy( 57 values, 58 persister.getPropertyTypes(), 59 persister.getPropertyUpdateability(), 60 values, 61 source 62 ); 63 Object version = Versioning.getVersion(values, persister); 64 65 EntityEntry newEntry = source.getPersistenceContext().addEntity( 66 object, 67 Status.MANAGED, 68 values, 69 key, 70 version, 71 LockMode.NONE, 72 true, 73 persister, 74 false, 75 true ); 77 78 new OnLockVisitor( source, id ).process( object, persister ); 79 80 persister.afterReassociate( object, source ); 81 82 return newEntry; 83 84 } 85 86 } 87 | Popular Tags |