KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: AbstractReassociateEventListener.java,v 1.8 2005/07/12 20:12:54 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import java.io.Serializable 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.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 /**
21  * A convenience base class for listeners that respond to requests to reassociate an entity
22  * to a session ( such as through lock() or update() ).
23  *
24  * @author Gavin King
25  */

26 public class AbstractReassociateEventListener extends AbstractEventListener {
27
28     private static final Log log = LogFactory.getLog(AbstractReassociateEventListener.class);
29
30     /**
31      * Associates a given entity (either transient or associated with another session) to
32      * the given session.
33      *
34      * @param event The event triggering the re-association
35      * @param object The entity to be associated
36      * @param id The id of the entity.
37      * @param persister The entity's persister instance.
38      * @return An EntityEntry representing the entity within this session.
39      * @throws HibernateException
40      */

41     protected final EntityEntry reassociate(AbstractEvent event, Object JavaDoc object, Serializable JavaDoc 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         //get a snapshot
55
Object JavaDoc[] values = persister.getPropertyValues( object, source.getEntityMode() );
56         TypeFactory.deepCopy(
57                 values,
58                 persister.getPropertyTypes(),
59                 persister.getPropertyUpdateability(),
60                 values,
61                 source
62             );
63         Object JavaDoc 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 //will be ignored, using the existing Entry instead
76
);
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