1 package org.hibernate.loader.entity; 3 4 import java.io.Serializable ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.hibernate.HibernateException; 10 import org.hibernate.LockMode; 11 import org.hibernate.MappingException; 12 import org.hibernate.engine.SessionFactoryImplementor; 13 import org.hibernate.engine.SessionImplementor; 14 import org.hibernate.loader.Loader; 15 import org.hibernate.persister.entity.EntityPersister; 16 import org.hibernate.persister.entity.OuterJoinLoadable; 17 import org.hibernate.type.Type; 18 import org.hibernate.util.ArrayHelper; 19 20 27 public class BatchingEntityLoader implements UniqueEntityLoader { 28 29 private final Loader[] loaders; 30 private final int[] batchSizes; 31 private final EntityPersister persister; 32 private final Type idType; 33 34 public BatchingEntityLoader(EntityPersister persister, int[] batchSizes, Loader[] loaders) { 35 this.batchSizes = batchSizes; 36 this.loaders = loaders; 37 this.persister = persister; 38 idType = persister.getIdentifierType(); 39 } 40 41 private Object getObjectFromList(List results, Serializable id, SessionImplementor session) { 42 Iterator iter = results.iterator(); 44 while ( iter.hasNext() ) { 45 Object obj = iter.next(); 46 final boolean equal = idType.isEqual( 47 id, 48 session.getContextEntityIdentifier(obj), 49 session.getEntityMode(), 50 session.getFactory() 51 ); 52 if ( equal ) return obj; 53 } 54 return null; 55 } 56 57 public Object load(Serializable id, Object optionalObject, SessionImplementor session) 58 throws HibernateException { 59 60 Serializable [] batch = session.getPersistenceContext() 61 .getBatchFetchQueue() 62 .getEntityBatch( persister, id, batchSizes[0], session.getEntityMode() ); 63 64 for ( int i=0; i<batchSizes.length-1; i++) { 65 final int smallBatchSize = batchSizes[i]; 66 if ( batch[smallBatchSize-1]!=null ) { 67 Serializable [] smallBatch = new Serializable [smallBatchSize]; 68 System.arraycopy(batch, 0, smallBatch, 0, smallBatchSize); 69 final List results = loaders[i].loadEntityBatch( 70 session, 71 smallBatch, 72 idType, 73 optionalObject, 74 persister.getEntityName(), 75 id, 76 persister 77 ); 78 return getObjectFromList(results, id, session); } 80 } 81 82 return ( (UniqueEntityLoader) loaders[batchSizes.length-1] ).load(id, optionalObject, session); 83 84 } 85 86 public static UniqueEntityLoader createBatchingEntityLoader( 87 final OuterJoinLoadable persister, 88 final int maxBatchSize, 89 final LockMode lockMode, 90 final SessionFactoryImplementor factory, 91 final Map enabledFilters) 92 throws MappingException { 93 94 if ( maxBatchSize>1 ) { 95 int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize); 96 Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ]; 97 for ( int i=0; i<batchSizesToCreate.length; i++ ) { 98 loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockMode, factory, enabledFilters); 99 } 100 return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate); 101 } 102 else { 103 return new EntityLoader(persister, lockMode, factory, enabledFilters); 104 } 105 } 106 107 } 108 | Popular Tags |