private function doRefresh($document, array &$visited)
    {
        $oid = spl_object_hash($document);
        if (isset($visited[$oid])) {
            return; // Prevent infinite recursion
        }

        $visited[$oid] = $document; // mark visited

        $class = $this->dm->getClassMetadata(get_class($document));

        if (!$class->isEmbeddedDocument) {
            if ($this->getDocumentState($document) !== self::STATE_MANAGED) {
                throw new \InvalidArgumentException('Document is not MANAGED.');
            }

            $identifier = $this->documentIdentifiers[$oid];
            $this->getDocumentPersister($class->name)->refresh($identifier, $document);
        }

        $this->cascadeRefresh($document, $visited);
    }