private function createDocument($data, $document = null, $refresh = false)
    {
        if ($this->class->isLockable) {
            $lockAttribute = $this->class->getLockMetadata()->attributeName;

            if (isset($result[$lockAttribute]) && $data[$lockAttribute] === LockMode::PESSIMISTIC_WRITE) {
                throw LockException::lockFailed($data);
            }
        }

        if ($document !== null) {
            $refresh    = true;
            $identifier = $this->class->createIdentifier($data);

            $this->uow->registerManaged($document, $identifier, $data);
        }

        return $this->uow->getOrCreateDocument($this->class, $data, $document, $refresh);
    }