KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: OnLockVisitor.java,v 1.5 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.engine.SessionImplementor;
9 import org.hibernate.event.EventSource;
10 import org.hibernate.persister.collection.CollectionPersister;
11 import org.hibernate.type.CollectionType;
12
13 /**
14  * When a transient entity is passed to lock(), we must 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. throw an exception for each "new" collection
19  *
20  * @author Gavin King
21  */

22 public class OnLockVisitor extends ReattachVisitor {
23
24     public OnLockVisitor(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         SessionImplementor session = getSession();
32         CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
33
34         if (collection==null) {
35             //do nothing
36
}
37         else if ( collection instanceof PersistentCollection ) {
38             PersistentCollection persistentCollection = (PersistentCollection) collection;
39
40             if ( persistentCollection.setCurrentSession(session) ) {
41
42                 if ( isOwnerUnchanged( persistentCollection, persister, getKey() ) ) {
43                     // a "detached" collection that originally belonged to the same entity
44
if ( persistentCollection.isDirty() ) {
45                         throw new HibernateException("reassociated object has dirty collection");
46                     }
47                     reattachCollection(persistentCollection, type);
48                 }
49                 else {
50                     // a "detached" collection that belonged to a different entity
51
throw new HibernateException("reassociated object has dirty collection reference");
52                 }
53
54             }
55             else {
56                 // a collection loaded in the current session
57
// can not possibly be the collection belonging
58
// to the entity passed to update()
59
throw new HibernateException("reassociated object has dirty collection reference");
60             }
61
62         }
63         else {
64             // brand new collection
65
//TODO: or an array!! we can't lock objects with arrays now??
66
throw new HibernateException("reassociated object has dirty collection reference (or an array)");
67         }
68
69         return null;
70
71     }
72     
73 }
74
Popular Tags