KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: OnReplicateVisitor.java,v 1.9 2005/06/17 19:36:08 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.collection.PersistentCollection;
8 import org.hibernate.event.EventSource;
9 import org.hibernate.persister.collection.CollectionPersister;
10 import org.hibernate.type.CollectionType;
11
12 /**
13  * When an entity is passed to replicate(), and there is an existing row, we must
14  * inspect all its collections and
15  * 1. associate any uninitialized PersistentCollections with this session
16  * 2. associate any initialized PersistentCollections with this session, using the
17  * existing snapshot
18  * 3. execute a collection removal (SQL DELETE) for each null collection property
19  * or "new" collection
20  *
21  * @author Gavin King
22  */

23 public class OnReplicateVisitor extends ReattachVisitor {
24     
25     private boolean isUpdate;
26     
27     OnReplicateVisitor(EventSource session, Serializable JavaDoc key, boolean isUpdate) {
28         super(session, key);
29         this.isUpdate = isUpdate;
30     }
31
32     Object JavaDoc processCollection(Object JavaDoc collection, CollectionType type)
33         throws HibernateException {
34         
35         if (collection==CollectionType.UNFETCHED_COLLECTION) return null;
36
37         EventSource session = getSession();
38         Serializable JavaDoc key = getKey();
39         CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
40
41         if (isUpdate) removeCollection(persister, key, session);
42         if ( collection!=null && (collection instanceof PersistentCollection) ) {
43             PersistentCollection wrapper = (PersistentCollection) collection;
44             wrapper.setCurrentSession(session);
45             if ( wrapper.wasInitialized() ) {
46                 session.getPersistenceContext().addNewCollection(persister, wrapper);
47             }
48             else {
49                 reattachCollection( wrapper, type );
50             }
51         }
52         else {
53             // otherwise a null or brand new collection
54
// this will also (inefficiently) handle arrays, which
55
// have no snapshot, so we can't do any better
56
//processArrayOrNewCollection(collection, type);
57
}
58
59         return null;
60
61     }
62
63 }
64
Popular Tags