@Override
    public long fetchCount() {
        QueryModifiers modifiers = getMetadata().getModifiers();
        try {
            Query query = createQuery(modifiers, true);
            Long rv = (Long) query.uniqueResult();
            if (rv != null) {
                return rv;
            } else {
                throw new QueryException("Query returned null");
            }
        } finally {
            reset();
        }
    }
Abstract base class for Hibernate API based implementations of the JPQL interface

@param <T> result type
@param <Q> concrete subtype

@author tiwe
querydsl-jpa/src/main/java/com/querydsl/jpa/hibernate/AbstractHibernateQuery.java