KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ProxyVisitor.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 import org.hibernate.type.EntityType;
12
13 /**
14  * Reassociates uninitialized proxies with the session
15  * @author Gavin King
16  */

17 public abstract class ProxyVisitor extends AbstractVisitor {
18
19
20     public ProxyVisitor(EventSource session) {
21         super(session);
22     }
23
24     Object JavaDoc processEntity(Object JavaDoc value, EntityType entityType) throws HibernateException {
25
26         if (value!=null) {
27             getSession().getPersistenceContext().reassociateIfUninitializedProxy(value);
28             // if it is an initialized proxy, let cascade
29
// handle it later on
30
}
31
32         return null;
33     }
34
35     /**
36      * Has the owner of the collection changed since the collection
37      * was snapshotted and detached?
38      */

39     protected static boolean isOwnerUnchanged(
40             final PersistentCollection snapshot,
41             final CollectionPersister persister,
42             final Serializable JavaDoc id
43     ) {
44         return isCollectionSnapshotValid(snapshot) &&
45                 persister.getRole().equals( snapshot.getRole() ) &&
46                 id.equals( snapshot.getKey() );
47     }
48
49     private static boolean isCollectionSnapshotValid(PersistentCollection snapshot) {
50         return snapshot != null &&
51                 snapshot.getRole() != null &&
52                 snapshot.getKey() != null;
53     }
54     
55     /**
56      * Reattach a detached (disassociated) initialized or uninitialized
57      * collection wrapper, using a snapshot carried with the collection
58      * wrapper
59      */

60     protected void reattachCollection(PersistentCollection collection, CollectionType type)
61     throws HibernateException {
62         if ( collection.wasInitialized() ) {
63             CollectionPersister collectionPersister = getSession().getFactory()
64             .getCollectionPersister( type.getRole() );
65             getSession().getPersistenceContext()
66                 .addInitializedDetachedCollection( collectionPersister, collection );
67         }
68         else {
69             if ( !isCollectionSnapshotValid(collection) ) {
70                 throw new HibernateException( "could not reassociate uninitialized transient collection" );
71             }
72             CollectionPersister collectionPersister = getSession().getFactory()
73                     .getCollectionPersister( collection.getRole() );
74             getSession().getPersistenceContext()
75                 .addUninitializedDetachedCollection( collectionPersister, collection );
76         }
77     }
78
79 }
80
Popular Tags