Example usage for javax.persistence.criteria CriteriaQuery isDistinct

List of usage examples for javax.persistence.criteria CriteriaQuery isDistinct

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery isDistinct.

Prototype

boolean isDistinct();

Source Link

Document

Return whether duplicate query results must be eliminated or retained.

Usage

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * @param em entity manager/*from   w w w.  j a  va  2  s .c om*/
 * @param criteria source criteria
 * @return row count CriteriaQuery
 */
public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
    copyCriteriaWithoutSelectionAndOrder(criteria, countCriteria);

    Expression<Long> countExpression;

    if (criteria.isDistinct()) {
        countExpression = builder.countDistinct(findRoot(countCriteria, criteria.getResultType()));
    } else {
        countExpression = builder.count(findRoot(countCriteria, criteria.getResultType()));
    }

    return countCriteria.select(countExpression);
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Copy criteria without selection and order.
 * @param from source Criteria.//from  w  ww .  java 2s.  c  om
 * @param to destination Criteria.
 */
private static void copyCriteriaWithoutSelectionAndOrder(CriteriaQuery<?> from, CriteriaQuery<?> to) {
    if (isEclipseLink(from) && from.getRestriction() != null) {
        // EclipseLink adds roots from predicate paths to critera. Skip copying 
        // roots as workaround.
    } else {
        // Copy Roots
        for (Root<?> root : from.getRoots()) {
            Root<?> dest = to.from(root.getJavaType());
            dest.alias(getOrCreateAlias(root));
            copyJoins(root, dest);
        }
    }

    to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());

    if (from.getGroupRestriction() != null)
        to.having(from.getGroupRestriction());

    Predicate predicate = from.getRestriction();
    if (predicate != null)
        to.where(predicate);
}

From source file:com.sishuok.es.common.repository.support.SimpleBaseRepository.java

/**
 * Creates a new count query for the given {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param spec can be {@literal null}.//from w w w .j  a va  2s . com
 * @return
 */
private TypedQuery<Long> getCountQuery(Specification<M> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<M> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    TypedQuery<Long> q = em.createQuery(query);
    repositoryHelper.applyEnableQueryCache(q);
    return q;
}

From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

/**
 * Creates a new count query for the given {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param spec can be {@literal null}./*  w  w w.  j  a v  a2s.co m*/
 * @return
 */
protected TypedQuery<Long> getCountQuery(Specification<M> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<M> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    TypedQuery<Long> q = em.createQuery(query);
    repositoryHelper.applyEnableQueryCache(q);
    return q;
}

From source file:org.agric.oxm.utils.JpaUtils.java

/**
 * Copy Criteria without Selection//from   w  w w  .j a v  a  2s. c  om
 * 
 * @param from
 *            source Criteria
 * @param to
 *            destination Criteria
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {

    // Copy Roots
    for (Root<?> root : from.getRoots()) {
        Root<?> dest = to.from(root.getJavaType());
        dest.alias(getOrCreateAlias(root));
        copyJoins(root, dest);
    }

    to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());
    to.having(from.getGroupRestriction());
    to.where(from.getRestriction());
    to.orderBy(from.getOrderList());
}

From source file:org.jboss.pressgang.ccms.filter.utils.JPAUtils.java

/**
 * Copy Criteria without Selection//w  w w  .j  a v  a  2  s.  c om
 *
 * @param from source Criteria
 * @param to   destination Criteria
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {

    // Copy Roots
    for (Root<?> root : from.getRoots()) {
        Root<?> dest = to.from(root.getJavaType());
        dest.alias(getOrCreateAlias(root));
        copyJoins(root, dest);
    }

    if (from.getGroupList() != null)
        to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());
    if (from.getGroupRestriction() != null)
        to.having(from.getGroupRestriction());
    if (from.getRestriction() != null)
        to.where(from.getRestriction());
    if (from.getOrderList() != null)
        to.orderBy(from.getOrderList());
}

From source file:org.springframework.data.jpa.repository.support.SimpleJpaRepository.java

/**
 * Creates a new count query for the given {@link Specification}.
 * //from w  w  w  .j  av a 2  s.  co  m
 * @param spec can be {@literal null}.
 * @return
 */
protected TypedQuery<Long> getCountQuery(Specification<T> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<T> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    return em.createQuery(query);
}