private function setRelationships(ClassMetadataInfo $metadata, Metadata\EntityMetadata $entity)
    {
        $allTypes = $this->getAllTypeNames();
        foreach ($metadata->fieldMappings as $fieldKey => $mapping) {
            if (!isset($mapping['reference'])) {
                // Not a relationship. Skip.
                continue;
            }
            if (!isset($mapping['targetDocument'])) {
                // No target found. Skip.
                // @todo Should this throw an Exception?
                continue;
            }

            $type = $this->getTypeForClassName($mapping['targetDocument']);
            if (!in_array($type, $allTypes)) {
                throw new RuntimeException(sprintf('No metadata was found for related entity type "%s" as found on relationship field "%s::%s"', $type, $entity->type, $fieldKey));
            }

            $relationship = new Metadata\RelationshipMetadata($fieldKey, $mapping['type'], $type);
            if (isset($mapping['isInverseSide']) && true === $mapping['isInverseSide']) {
                $relationship->isInverse = true;
            }
            $entity->addRelationship($relationship);
        }
        return $entity;
    }