protected function fetchEntity(string $id) : EntityInterface
    {
        $table = $this->loadModel();
        Assert::isInstanceOf($table, Table::class);

        $primaryKey = $table->getPrimaryKey();
        if (! is_string($primaryKey)) {
            throw new UnsupportedPrimaryKeyException();
        }

        try {
            $entity = $table->find()
                ->where([$table->aliasField($primaryKey) => $id])
                ->enableHydration(true)
                ->firstOrFail();
            Assert::isInstanceOf($entity, EntityInterface::class);

            return $entity;
        } catch (Exception $e) {
            // $id is a UUID, re-throwing the exception as we cannot fetch the record by lookup field(s)
            if (Validation::uuid($id)) {
                throw $e;
            }
        }

        /**
         * Try to fetch record by lookup field(s)
         *
         * @var \Cake\Datasource\EntityInterface
         */
        $entity = $table->find()
            ->applyOptions(['lookup' => true, 'value' => $id])
            ->enableHydration(true)
            ->firstOrFail();

        return $entity;
    }