public function createReferenceInverseSideQuery(PropertyMetadata $mapping, $owner)
    {
        $targetClass = $this->dm->getClassMetadata($mapping->targetDocument);
        $mappedBy    = $targetClass->getPropertyMetadata($mapping->mappedBy);
        $qb          = $this->dm->createQueryBuilder($mapping->targetDocument);

        //add the owning document's identifier as criteria for the mapped reference
        $qb->attr($mappedBy->name)->equals($owner);

        if ($mapping->mappedIndex !== null) {
            $qb->index($mapping->mappedIndex);

            //if mappedIndex is not set, requireIndexes is in effect and the
            //querybuilder cannot find a usable index then the query will throw
            //an exception on execution
        }

        if ($mapping->requireIndexes !== null) {
            //if configured this will override any setting on the dm or class
            $qb->requireIndexes($mapping->requireIndexes);
        }

        $qb->addCriteria((array)$mapping->criteria);

        foreach ((array)$mapping->prime as $attribute) {
            $qb->attr($attribute)->prime(true);
        }

        $query = $qb->getQuery();

        $query->reverse($mapping->reverse);
        $query->limit($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_ONE ? 1 : $mapping->limit);

        return $query;
    }