FileDocCategorySizeDatePackage
BatchingEntityLoader.javaAPI DocHibernate 3.2.53418Thu May 26 22:54:08 BST 2005org.hibernate.loader.entity

BatchingEntityLoader

public class BatchingEntityLoader extends Object implements UniqueEntityLoader
"Batch" loads entities, using multiple primary key values in the SQL where clause.
see
EntityLoader
author
Gavin King

Fields Summary
private final org.hibernate.loader.Loader[]
loaders
private final int[]
batchSizes
private final org.hibernate.persister.entity.EntityPersister
persister
private final org.hibernate.type.Type
idType
Constructors Summary
public BatchingEntityLoader(org.hibernate.persister.entity.EntityPersister persister, int[] batchSizes, org.hibernate.loader.Loader[] loaders)

		this.batchSizes = batchSizes;
		this.loaders = loaders;
		this.persister = persister;
		idType = persister.getIdentifierType();
	
Methods Summary
public static UniqueEntityLoadercreateBatchingEntityLoader(org.hibernate.persister.entity.OuterJoinLoadable persister, int maxBatchSize, org.hibernate.LockMode lockMode, org.hibernate.engine.SessionFactoryImplementor factory, java.util.Map enabledFilters)


		if ( maxBatchSize>1 ) {
			int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
			Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
			for ( int i=0; i<batchSizesToCreate.length; i++ ) {
				loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockMode, factory, enabledFilters);
			}
			return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate);
		}
		else {
			return new EntityLoader(persister, lockMode, factory, enabledFilters);
		}
	
private java.lang.ObjectgetObjectFromList(java.util.List results, java.io.Serializable id, org.hibernate.engine.SessionImplementor session)

		// get the right object from the list ... would it be easier to just call getEntity() ??
		Iterator iter = results.iterator();
		while ( iter.hasNext() ) {
			Object obj = iter.next();
			final boolean equal = idType.isEqual( 
					id, 
					session.getContextEntityIdentifier(obj), 
					session.getEntityMode(), 
					session.getFactory() 
			);
			if ( equal ) return obj;
		}
		return null;
	
public java.lang.Objectload(java.io.Serializable id, java.lang.Object optionalObject, org.hibernate.engine.SessionImplementor session)

		
		Serializable[] batch = session.getPersistenceContext()
			.getBatchFetchQueue()
			.getEntityBatch( persister, id, batchSizes[0], session.getEntityMode() );
		
		for ( int i=0; i<batchSizes.length-1; i++) {
			final int smallBatchSize = batchSizes[i];
			if ( batch[smallBatchSize-1]!=null ) {
				Serializable[] smallBatch = new Serializable[smallBatchSize];
				System.arraycopy(batch, 0, smallBatch, 0, smallBatchSize);
				final List results = loaders[i].loadEntityBatch(
						session, 
						smallBatch, 
						idType, 
						optionalObject, 
						persister.getEntityName(), 
						id, 
						persister
				);
				return getObjectFromList(results, id, session); //EARLY EXIT
			}
		}
		
		return ( (UniqueEntityLoader) loaders[batchSizes.length-1] ).load(id, optionalObject, session);