public List load(EntityInfo entityInfos) {
if ( entityInfos.length == 0 ) return Collections.EMPTY_LIST;
if ( entityInfos.length == 1 ) {
final Object entity = load( entityInfos[0] );
if ( entity == null ) {
return Collections.EMPTY_LIST;
}
else {
final List< Object > list = new ArrayList< Object >( 1 );
list.add( entity );
return list;
}
}
//split EntityInfo per root entity
Map< RootEntityMetadata, List< EntityInfo > > entityinfoBuckets =
new HashMap< RootEntityMetadata, List< EntityInfo > >( entityMatadata.size());
for (EntityInfo entityInfo : entityInfos) {
boolean found = false;
for (RootEntityMetadata rootEntityInfo : entityMatadata) {
if ( rootEntityInfo.mappedSubclasses.contains( entityInfo.clazz ) ) {
List< EntityInfo > bucket = entityinfoBuckets.get( rootEntityInfo );
if ( bucket == null ) {
bucket = new ArrayList< EntityInfo >();
entityinfoBuckets.put( rootEntityInfo, bucket );
}
bucket.add( entityInfo );
found = true;
break; //we stop looping for the right bucket
}
}
if (!found) throw new AssertionFailure( "Could not find root entity for " + entityInfo.clazz );
}
//initialize objects by bucket
for ( Map.Entry< RootEntityMetadata, List< EntityInfo > > entry : entityinfoBuckets.entrySet() ) {
final RootEntityMetadata key = entry.getKey();
final List< EntityInfo > value = entry.getValue();
final EntityInfo[] bucketEntityInfos = value.toArray( new EntityInfo[value.size()] );
if ( key.useObjectLoader ) {
objectLoader.load( bucketEntityInfos );
}
else {
ObjectLoaderHelper.initializeObjects( bucketEntityInfos,
key.criteria, key.rootEntity, searchFactoryImplementor);
}
}
return ObjectLoaderHelper.returnAlreadyLoadedObjectsInCorrectOrder( entityInfos, session );
}
|