Example usage for javax.persistence.criteria CriteriaQuery getSelection

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

Introduction

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

Prototype

Selection<T> getSelection();

Source Link

Document

Return the selection of the query, or null if no selection has been set.

Usage

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

public static <T> void copyCriteria(CriteriaQuery<T> from, CriteriaQuery<T> to) {
    copyCriteriaNoSelection(from, to);//from  w ww  .j  av a 2 s.c o  m
    to.select(from.getSelection());
}

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

/**
 * Method to translate criteriaQuery into JPQL.
 * //w w  w  .  ja va  2  s . c  o m
 * @param criteriaQuery
 *            criteria query.
 * 
 * @return JPQL string.
 */
static <S> String translate(CriteriaQuery criteriaQuery) {
    QueryBuilder builder = new CriteriaQueryTranslator.QueryBuilder();

    // validate if criteria query is valid

    /**
     * select, from clause is mandatory
     * 
     * multiple from clause not support where clause is optional
     * 
     */

    Selection<S> select = criteriaQuery.getSelection();

    if (select != null) {
        builder.appendSelectClause();

    }

    if (select.getClass().isAssignableFrom(DefaultCompoundSelection.class)
            && ((CompoundSelection) select).isCompoundSelection()) {
        List<Selection<?>> selections = ((CompoundSelection) select).getCompoundSelectionItems();
        builder.appendMultiSelect(selections);
    } else if (select instanceof AggregateExpression) {
        builder.appendAggregate(((AggregateExpression) select).getAggregation());
    } else {
        String alias = select.getAlias();

        if (!StringUtils.isEmpty(alias)) {
            builder.appendAlias(alias);
        }

        Attribute attribute = ((DefaultPath) select).getAttribute();

        if (attribute != null) {
            builder.appendAttribute(attribute);
        }
    }
    Class<? extends S> clazzType = select.getJavaType();

    Set<Root<?>> roots = criteriaQuery.getRoots();

    Root<?> from = roots.iterator().next();

    Class entityClazz = from.getJavaType();

    builder.appendFromClause();

    // select.alias(paramString)
    builder.appendFrom(entityClazz);
    builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias());
    Predicate where = criteriaQuery.getRestriction(); // this could be null.
    if (where != null) {
        builder.appendWhereClause();
        List<Expression<Boolean>> expressions = where.getExpressions();
        for (Expression expr : expressions) {
            builder.appendWhere(expr, from.getAlias());
        }

    }

    List<Order> orderings = criteriaQuery.getOrderList();

    if (orderings != null) {
        if (!orderings.isEmpty()) {
            builder.appendOrderClause(where == null);
        }

        for (Order order : orderings) {
            builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias());
            builder.appendOrdering(order);
        }
    }
    return builder.getQuery();

    // check that roots has to be one. multiple clause not yet supported

}

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

protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) {
    Assert.notNull(criteriaQuery);//  w w  w.j av a2s.c  o m
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    if (pageable == null) {
        pageable = new Pageable();
    }
    // CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    // Root<T> root = getRoot(criteriaQuery);
    addRestrictions(criteriaQuery, pageable);
    // addOrders(criteriaQuery, pageable);
    // if (criteriaQuery.getOrderList().isEmpty()) {
    // if (OrderEntity.class.isAssignableFrom(entityClass)) {
    // criteriaQuery.orderBy(criteriaBuilder.asc(root
    // .get(OrderEntity.ORDER_PROPERTY_NAME)));
    // } else {
    // criteriaQuery.orderBy(criteriaBuilder.desc(root
    // .get(OrderEntity.CREATE_DATE_PROPERTY_NAME)));
    // }
    // }
    long total = count(criteriaQuery, null);
    int totalPages = (int) Math.ceil(total / (double) pageable.getPageSize());
    if (totalPages < pageable.getPageNumber()) {
        pageable.setPageNumber(totalPages);
    }
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
    query.setMaxResults(pageable.getPageSize());
    return new Page<T>(query.getResultList(), total, pageable);
}

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

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);/*w w w. j  ava2 s .  c  o 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.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.zero.dao.impl.BaseDaoImpl.java

protected List<T> findList(CriteriaQuery<T> criteriaQuery, Integer first, Integer count, List<Filter> filters,
        List<Order> orders) {
    Assert.notNull(criteriaQuery);/*from  w  w w . j a v a 2 s.co m*/
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    addRestrictions(criteriaQuery, filters);
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (first != null) {
        query.setFirstResult(first);
    }
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

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

protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) {
    Assert.notNull(criteriaQuery);/*from ww w. java  2  s  . c  o  m*/
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    if (pageable == null) {
        pageable = new Pageable();
    }
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Root<T> root = getRoot(criteriaQuery);
    addRestrictions(criteriaQuery, pageable);
    addOrders(criteriaQuery, pageable);
    if (criteriaQuery.getOrderList().isEmpty()) {
        if (OrderEntity.class.isAssignableFrom(entityClass)) {
            criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME)));
        } else {
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME)));
        }
    }
    long total = count(criteriaQuery, null);
    int totalPages = (int) Math.ceil((double) total / (double) pageable.getPageSize());
    if (totalPages < pageable.getPageNumber()) {
        pageable.setPageNumber(totalPages);
    }
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
    query.setMaxResults(pageable.getPageSize());
    return new Page<T>(query.getResultList(), total, pageable);
}

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

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);//from  ww w. ja v a 2  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));

    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:net.groupbuy.dao.impl.BaseDaoImpl.java

protected List<T> findList(CriteriaQuery<T> criteriaQuery, Integer first, Integer count, List<Filter> filters,
        List<Order> orders) {
    Assert.notNull(criteriaQuery);//from ww w  .j a v a  2s.com
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Root<T> root = getRoot(criteriaQuery);
    addRestrictions(criteriaQuery, filters);
    addOrders(criteriaQuery, orders);
    if (criteriaQuery.getOrderList().isEmpty()) {
        if (OrderEntity.class.isAssignableFrom(entityClass)) {
            criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME)));
        } else {
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME)));
        }
    }
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (first != null) {
        query.setFirstResult(first);
    }
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

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

/**
 * Create a TypedQuery from a request page
 * @param page request page/*from ww  w  .j  a  v  a2  s .c o  m*/
 * @return new TypedQuery
 */
@SuppressWarnings("unchecked")
private <K> TypedQuery<K> getCriteriaQuery(Page<K> page) {
    CriteriaQuery<K> criteria = getCriteria(page);

    CriteriaQuery<Long> countCriteria = (CriteriaQuery<Long>) getCriteria(page);
    CriteriaBuilder cb = em.getCriteriaBuilder();

    Root<?> root = countCriteria.getRoots().iterator().next();
    countCriteria.select(cb.count(root));

    page.setCount((em.createQuery(countCriteria).getSingleResult()).intValue());

    criteria.orderBy(getOrder(page, criteria));

    // Add default select to entity class if none was set.
    if (criteria.getSelection() == null) {
        criteria.select((Selection<? extends K>) root);
    }

    return em.createQuery(criteria);
}