Example usage for javax.persistence.criteria CriteriaQuery getResultType

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

Introduction

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

Prototype

Class<T> getResultType();

Source Link

Document

Return the result type of the query or subquery.

Usage

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

/**
 * Find Root of result type//from w w  w  .  j av  a2s.  c o  m
 * @param query criteria query
 * @return the root of result type or null if none
 */
public static <T> Root<T> findRoot(CriteriaQuery<T> query) {
    return findRoot(query, query.getResultType());
}

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

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * @param em entity manager//  ww w . j  a va  2s  .  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:net.kaczmarzyk.spring.data.jpa.domain.JoinFetch.java

@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    if (!Number.class.isAssignableFrom(query.getResultType())) { // do not join in count queries
        for (String path : pathsToFetch) {
            root.fetch(path, joinType);//from www  . j a  v  a 2  s. c  o m
        }
    }
    return null;
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

private Root<T> getRoot(CriteriaQuery<T> criteriaQuery) {
    if (criteriaQuery != null) {
        return getRoot(criteriaQuery, criteriaQuery.getResultType());
    }//w w w . j a  v  a2 s  .c  om
    return null;
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);//from ww w  .  j  a  v  a2  s .c  om
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    addRestrictions(criteriaQuery, filters);

    CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class);

    for (Root<?> root : criteriaQuery.getRoots()) {
        Root<?> dest = countCriteriaQuery.from(root.getJavaType());
        dest.alias(getAlias(root));
        copyJoins(root, dest);
    }

    Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType());
    countCriteriaQuery.select(criteriaBuilder.count(countRoot.get("id").<String>get("stcd")));

    if (criteriaQuery.getGroupList() != null) {
        countCriteriaQuery.groupBy(criteriaQuery.getGroupList());
    }
    if (criteriaQuery.getGroupRestriction() != null) {
        countCriteriaQuery.having(criteriaQuery.getGroupRestriction());
    }
    if (criteriaQuery.getRestriction() != null) {
        countCriteriaQuery.where(criteriaQuery.getRestriction());
    }
    return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}

From source file:com.impetus.kundera.persistence.EntityManagerImpl.java

@Override
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> paramCriteriaQuery) {
    checkClosed();//from   w ww  .ja v a2  s . c  om

    return this.createQuery(CriteriaQueryTranslator.translate(paramCriteriaQuery),
            paramCriteriaQuery.getResultType());
}

From source file:net.groupbuy.dao.impl.BaseDaoImpl.java

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);//  www.ja  v a2 s . co  m
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    addRestrictions(criteriaQuery, filters);

    CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class);
    for (Root<?> root : criteriaQuery.getRoots()) {
        Root<?> dest = countCriteriaQuery.from(root.getJavaType());
        dest.alias(getAlias(root));
        copyJoins(root, dest);
    }

    Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType());
    countCriteriaQuery.select(criteriaBuilder.count(countRoot));

    if (criteriaQuery.getGroupList() != null) {
        countCriteriaQuery.groupBy(criteriaQuery.getGroupList());
    }
    if (criteriaQuery.getGroupRestriction() != null) {
        countCriteriaQuery.having(criteriaQuery.getGroupRestriction());
    }
    if (criteriaQuery.getRestriction() != null) {
        countCriteriaQuery.where(criteriaQuery.getRestriction());
    }
    return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}

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

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * /*w  w  w.  j  a v  a 2  s  . co m*/
 * @param em
 *            entity manager
 * @param criteria
 *            source criteria
 * @return row coutnt CriteriaQuery
 */
public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
    copyCriteriaNoSelection(criteria, countCriteria);
    countCriteria.select(builder.count(findRoot(countCriteria, criteria.getResultType())));

    return countCriteria;
}

From source file:ru.savvy.jpafilterbuilder.FilterCriteriaBuilder.java

/**
 * Creates new instance from existing query
 *
 * @param em/*w w w. j  a  va 2s .c o  m*/
 * @param query
 */
public FilterCriteriaBuilder(EntityManager em, CriteriaQuery<T> query) {
    this.cb = em.getCriteriaBuilder();
    this.clazz = query.getResultType();
    //this.query = query;
    this.options = EnumSet.noneOf(Option.class);
    this.pb = new PredicateBuilder(cb, options);
    this.metamodel = em.getMetamodel();
    //        Set<Root<?>> roots = query.getRoots();
    //        for (Root<?> r : roots) {
    //            if (r.getJavaType().equals(this.clazz)) {
    //                this.root = (Root<T>) r;
    //                break;
    //            }
    //        }
    //        if (this.root == null) {
    //            this.root = query.from(this.clazz);
    //        }
}