1 package org.hibernate.event.def; 3 4 import java.io.Serializable ; 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 17 public abstract class ProxyVisitor extends AbstractVisitor { 18 19 20 public ProxyVisitor(EventSource session) { 21 super(session); 22 } 23 24 Object processEntity(Object value, EntityType entityType) throws HibernateException { 25 26 if (value!=null) { 27 getSession().getPersistenceContext().reassociateIfUninitializedProxy(value); 28 } 31 32 return null; 33 } 34 35 39 protected static boolean isOwnerUnchanged( 40 final PersistentCollection snapshot, 41 final CollectionPersister persister, 42 final Serializable 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 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 |