public function persistNew(ClassMetadata $class, $document, $overrideIdGenerator = null, $parent = null)
    {
        if ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::prePersist)) {
            $this->eventListenersInvoker->invoke(
                $class,
                Event::prePersist,
                $document,
                new LifecycleEventArgs($document, $this->dm),
                $invoke
            );
        }

        $generator = $this->getIdGenerator($overrideIdGenerator ? $overrideIdGenerator : $class->idGenerator);
        $id = $generator->generate($document, $class, $this->dm, $parent);
        $this->registerDocument($document, $id);

        if (!$generator instanceof AssignedIdGenerator) {
            $class->setIdentifierValue($document, $id);
        }

        // If the UUID is mapped, generate it early resp. validate if already present.
        $uuidFieldName = $class->getUuidFieldName();
        if ($uuidFieldName) {
            $existingUuid = $class->getFieldValue($document, $uuidFieldName);
            if (!$existingUuid) {
                $class->setFieldValue($document, $uuidFieldName, $this->generateUuid());
            } elseif (!UUIDHelper::isUUID($existingUuid)) {
                throw RuntimeException::invalidUuid($id, ClassUtils::getClass($document), $existingUuid);
            }
        }
    }