)
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> potentialModule : potentialModels )
{
EntityStore store = potentialModule.module().entityStore();
EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork( store, potentialModule.module() );
try
{
entityState = storeUow.getEntityState( identity );
}
catch( EntityNotFoundException e )
{
continue;
}
// Get the selected model
model = (EntityModel) entityState.entityDescriptor();
module = potentialModule.module();
}
// Check if model was found
if( model == null )
{
// Check if state was found
if( entityState == null )
{
throw new NoSuchEntityException( identity );
}
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 );
}
}
return entityInstance.<T>proxy();
}