public function addCriteria(Criteria $criteria)
    {
        $allAliases = $this->getAllAliases();
        if (! isset($allAliases[0])) {
            throw new Query\QueryException('No aliases are set before invoking addCriteria().');
        }

        $visitor         = new QueryExpressionVisitor($this->getAllAliases());
        $whereExpression = $criteria->getWhereExpression();

        if ($whereExpression) {
            $this->andWhere($visitor->dispatch($whereExpression));
            foreach ($visitor->getParameters() as $parameter) {
                $this->parameters->add($parameter);
            }
        }

        if ($criteria->getOrderings()) {
            foreach ($criteria->getOrderings() as $sort => $order) {
                $hasValidAlias = false;
                foreach ($allAliases as $alias) {
                    if (strpos($sort . '.', $alias . '.') === 0) {
                        $hasValidAlias = true;
                        break;
                    }
                }

                if (! $hasValidAlias) {
                    $sort = $allAliases[0] . '.' . $sort;
                }

                $this->addOrderBy($sort, $order);
            }
        }

        $firstResult = $criteria->getFirstResult();
        $maxResults  = $criteria->getMaxResults();

        // Overwrite limits only if they was set in criteria
        if ($firstResult !== null) {
            $this->setFirstResult($firstResult);
        }
        if ($maxResults !== null) {
            $this->setMaxResults($maxResults);
        }

        return $this;
    }