KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: OnUpdateVisitor.java,v 1.6 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 update(), we must inspect all its collections and
14  * 1. associate any uninitialized PersistentCollections with this session
15  * 2. associate any initialized PersistentCollections with this session, using the
16  * existing snapshot
17  * 3. execute a collection removal (SQL DELETE) for each null collection property
18  * or "new" collection
19  *
20  * @author Gavin King
21  */

22 public class OnUpdateVisitor extends ReattachVisitor {
23
24     OnUpdateVisitor(EventSource session, Serializable JavaDoc key) {
25         super(session, key);
26     }
27
28     Object JavaDoc processCollection(Object JavaDoc collection, CollectionType type)
29         throws HibernateException {
30         
31         if (collection==CollectionType.UNFETCHED_COLLECTION) return null;
32
33         EventSource session = getSession();
34         Serializable JavaDoc key = getKey();
35         CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
36
37         if ( collection!=null && (collection instanceof PersistentCollection) ) {
38             PersistentCollection wrapper = (PersistentCollection) collection;
39
40             if ( wrapper.setCurrentSession(session) ) {
41                 //a "detached" collection!
42

43                 if ( !isOwnerUnchanged(wrapper, persister, key) ) {
44                     // if the collection belonged to a different entity,
45
// clean up the existing state of the collection
46
removeCollection(persister, key, session);
47                 }
48
49                 reattachCollection(wrapper, type);
50             }
51             else {
52                 // a collection loaded in the current session
53
// can not possibly be the collection belonging
54
// to the entity passed to update()
55
removeCollection(persister, key, session);
56             }
57
58         }
59         else {
60             // null or brand new collection
61
// this will also (inefficiently) handle arrays, which have
62
// no snapshot, so we can't do any better
63
removeCollection(persister, key, session);
64             //processArrayOrNewCollection(collection, type);
65
}
66
67         return null;
68
69     }
70
71 }
72
Popular Tags