)
throws EntityTypeNotFoundException, NoSuchEntityException
{
checkOpen();
EntityInstance entityInstance = instanceCache.get( identity );
if( entityInstance == null )
{ // Not yet in cache
// Check if this is a root UoW, or if no parent UoW knows about this entity
EntityState entityState = null;
EntityModel model = null;
ModuleInstance module = null;
// Figure out what EntityStore to use
for( ModelModule<EntityModel> potentialModel : potentialModels )
{
EntityStore store = potentialModel.module().entityStore();
EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork( store, potentialModel.module() );
try
{
entityState = storeUow.entityStateOf( identity );
}
catch( EntityNotFoundException e )
{
continue;
}
// Get the selected model
model = (EntityModel) entityState.entityDescriptor();
module = potentialModel.module();
}
// Check if model was found
if( model == null )
{
// Check if state was found
if( entityState == null )
{
throw new NoSuchEntityException( identity, mixinType );
}
else
{
throw new EntityTypeNotFoundException( mixinType.getName() );
}
}
// Create instance
entityInstance = new EntityInstance( uow, module, model, entityState );
instanceCache.put( identity, entityInstance );
}
else
{
// Check if it has been removed
if( entityInstance.status() == EntityStatus.REMOVED )
{
throw new NoSuchEntityException( identity, mixinType );
}
}
return entityInstance.proxy();
}