1 package org.hibernate.event.def; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.hibernate.EntityMode; 7 import org.hibernate.HibernateException; 8 import org.hibernate.collection.PersistentCollection; 9 import org.hibernate.engine.PersistenceContext; 10 import org.hibernate.engine.SessionImplementor; 11 import org.hibernate.event.EventSource; 12 import org.hibernate.persister.collection.CollectionPersister; 13 import org.hibernate.persister.entity.EntityPersister; 14 import org.hibernate.type.AbstractComponentType; 15 import org.hibernate.type.CollectionType; 16 import org.hibernate.type.Type; 17 18 23 public class WrapVisitor extends ProxyVisitor { 24 25 private static final Log log = LogFactory.getLog(WrapVisitor.class); 26 27 boolean substitute = false; 28 29 boolean isSubstitutionRequired() { 30 return substitute; 31 } 32 33 WrapVisitor(EventSource session) { 34 super(session); 35 } 36 37 Object processCollection(Object collection, CollectionType collectionType) 38 throws HibernateException { 39 40 if ( collection!=null && (collection instanceof PersistentCollection) ) { 41 42 final SessionImplementor session = getSession(); 43 PersistentCollection coll = (PersistentCollection) collection; 44 if ( coll.setCurrentSession(session) ) { 45 reattachCollection( coll, collectionType ); 46 } 47 return null; 48 49 } 50 else { 51 return processArrayOrNewCollection(collection, collectionType); 52 } 53 54 } 55 56 final Object processArrayOrNewCollection(Object collection, CollectionType collectionType) 57 throws HibernateException { 58 59 final SessionImplementor session = getSession(); 60 61 if (collection==null) { 62 return null; 64 } 65 else { 66 CollectionPersister persister = session.getFactory().getCollectionPersister( collectionType.getRole() ); 67 68 final PersistenceContext persistenceContext = session.getPersistenceContext(); 69 if ( collectionType.hasHolder( session.getEntityMode() ) ) { 71 72 if (collection==CollectionType.UNFETCHED_COLLECTION) return null; 73 74 PersistentCollection ah = persistenceContext.getCollectionHolder(collection); 75 if (ah==null) { 76 ah = collectionType.wrap(session, collection); 77 persistenceContext.addNewCollection( persister, ah ); 78 persistenceContext.addCollectionHolder(ah); 79 } 80 return null; 81 } 82 else { 83 84 PersistentCollection persistentCollection = collectionType.wrap(session, collection); 85 persistenceContext.addNewCollection( persister, persistentCollection ); 86 87 if ( log.isTraceEnabled() ) log.trace( "Wrapped collection in role: " + collectionType.getRole() ); 88 89 return persistentCollection; 91 } 92 93 } 94 95 } 96 97 void processValue(int i, Object [] values, Type[] types) { 98 Object result = processValue( values[i], types[i] ); 99 if (result!=null) { 100 substitute = true; 101 values[i] = result; 102 } 103 } 104 105 Object processComponent(Object component, AbstractComponentType componentType) 106 throws HibernateException { 107 108 if (component!=null) { 109 Object [] values = componentType.getPropertyValues( component, getSession() ); 110 Type[] types = componentType.getSubtypes(); 111 boolean substituteComponent = false; 112 for ( int i=0; i<types.length; i++ ) { 113 Object result = processValue( values[i], types[i] ); 114 if (result!=null) { 115 values[i] = result; 116 substituteComponent = true; 117 } 118 } 119 if (substituteComponent) { 120 componentType.setPropertyValues( component, values, getSession().getEntityMode() ); 121 } 122 } 123 124 return null; 125 } 126 127 void process(Object object, EntityPersister persister) throws HibernateException { 128 EntityMode entityMode = getSession().getEntityMode(); 129 Object [] values = persister.getPropertyValues( object, entityMode ); 130 Type[] types = persister.getPropertyTypes(); 131 processEntityPropertyValues(values, types); 132 if ( isSubstitutionRequired() ) { 133 persister.setPropertyValues( object, values, entityMode ); 134 } 135 } 136 137 } 138 | Popular Tags |