/**
  * {@inheritdoc}
  */
 public function loadById(array $identifier, $entity = null)
 {
     $cacheKey = new EntityCacheKey($this->class->rootEntityName, $identifier);
     $cacheEntry = $this->region->get($cacheKey);
     $class = $this->class;
     if ($cacheEntry !== null) {
         if ($cacheEntry->class !== $this->class->name) {
             $class = $this->metadataFactory->getMetadataFor($cacheEntry->class);
         }
         if (($entity = $this->hydrator->loadCacheEntry($class, $cacheKey, $cacheEntry, $entity)) !== null) {
             if ($this->cacheLogger) {
                 $this->cacheLogger->entityCacheHit($this->regionName, $cacheKey);
             }
             return $entity;
         }
     }
     $entity = $this->persister->loadById($identifier, $entity);
     if ($entity === null) {
         return null;
     }
     $class = $this->class;
     $className = ClassUtils::getClass($entity);
     if ($className !== $this->class->name) {
         $class = $this->metadataFactory->getMetadataFor($className);
     }
     $cacheEntry = $this->hydrator->buildCacheEntry($class, $cacheKey, $entity);
     $cached = $this->region->put($cacheKey, $cacheEntry);
     if ($cached && ($this->joinedAssociations === null || count($this->joinedAssociations) > 0)) {
         $this->storeJoinedAssociations($entity);
     }
     if ($this->cacheLogger) {
         if ($cached) {
             $this->cacheLogger->entityCachePut($this->regionName, $cacheKey);
         }
         $this->cacheLogger->entityCacheMiss($this->regionName, $cacheKey);
     }
     return $entity;
 }
 /**
  * {@inheritdoc}
  */
 public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = array())
 {
     if (!($key->cacheMode & Cache::MODE_GET)) {
         return null;
     }
     $entry = $this->region->get($key);
     if (!$entry instanceof QueryCacheEntry) {
         return null;
     }
     if (!$this->validator->isValid($key, $entry)) {
         $this->region->evict($key);
         return null;
     }
     $result = array();
     $entityName = reset($rsm->aliasMap);
     $hasRelation = !empty($rsm->relationMap);
     $persister = $this->uow->getEntityPersister($entityName);
     $region = $persister->getCacheRegion();
     $regionName = $region->getName();
     // @TODO - move to cache hydration component
     foreach ($entry->result as $index => $entry) {
         if (($entityEntry = $region->get($entityKey = new EntityCacheKey($entityName, $entry['identifier']))) === null) {
             if ($this->cacheLogger !== null) {
                 $this->cacheLogger->entityCacheMiss($regionName, $entityKey);
             }
             return null;
         }
         if ($this->cacheLogger !== null) {
             $this->cacheLogger->entityCacheHit($regionName, $entityKey);
         }
         if (!$hasRelation) {
             $result[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);
             continue;
         }
         $data = $entityEntry->data;
         foreach ($entry['associations'] as $name => $assoc) {
             $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
             $assocRegion = $assocPersister->getCacheRegion();
             if ($assoc['type'] & ClassMetadata::TO_ONE) {
                 if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assoc['targetEntity'], $assoc['identifier']))) === null) {
                     if ($this->cacheLogger !== null) {
                         $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);
                     }
                     $this->uow->hydrationComplete();
                     return null;
                 }
                 $data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);
                 if ($this->cacheLogger !== null) {
                     $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey);
                 }
                 continue;
             }
             if (!isset($assoc['list']) || empty($assoc['list'])) {
                 continue;
             }
             $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
             $collection = new PersistentCollection($this->em, $targetClass, new ArrayCollection());
             foreach ($assoc['list'] as $assocIndex => $assocId) {
                 if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId))) === null) {
                     if ($this->cacheLogger !== null) {
                         $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);
                     }
                     $this->uow->hydrationComplete();
                     return null;
                 }
                 $element = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);
                 $collection->hydrateSet($assocIndex, $element);
                 if ($this->cacheLogger !== null) {
                     $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey);
                 }
             }
             $data[$name] = $collection;
             $collection->setInitialized(true);
         }
         $result[$index] = $this->uow->createEntity($entityEntry->class, $data, self::$hints);
     }
     $this->uow->hydrationComplete();
     return $result;
 }