Example usage for javax.persistence.criteria Root getJavaType

List of usage examples for javax.persistence.criteria Root getJavaType

Introduction

In this page you can find the example usage for javax.persistence.criteria Root getJavaType.

Prototype

Class<? extends X> getJavaType();

Source Link

Document

Return the Java type of the tuple element.

Usage

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

/**
 * Method to translate criteriaQuery into JPQL.
 * /*  ww w.j  av  a2s .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:org.jdal.dao.jpa.JpaUtils.java

/**
 * Find the Root with type class on CriteriaQuery Root Set
 * @param <T> root type/*from  ww w  . j av a 2  s .c  o  m*/
 * @param query criteria query
 * @param clazz root type
 * @return Root<T> of null if none
 */
@SuppressWarnings("unchecked")
public static <T> Root<T> findRoot(CriteriaQuery<?> query, Class<T> clazz) {

    for (Root<?> r : query.getRoots()) {
        if (clazz.equals(r.getJavaType())) {
            return (Root<T>) r.as(clazz);
        }
    }
    return (Root<T>) query.getRoots().iterator().next();
}

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

/**
 * Copy criteria without selection and order.
 * @param from source Criteria.//from   w w w.  j ava  2  s.  c  o m
 * @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.zero.dao.impl.BaseDaoImpl.java

private Root<T> getRoot(CriteriaQuery<?> criteriaQuery, Class<T> clazz) {
    if (criteriaQuery != null && criteriaQuery.getRoots() != null && clazz != null) {
        for (Root<?> root : criteriaQuery.getRoots()) {
            if (clazz.equals(root.getJavaType())) {
                return (Root<T>) root.as(clazz);
            }/*from   ww  w. ja  v a 2  s  . c o  m*/
        }
    }
    return null;
}

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

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);//from   w w  w .  ja v a2 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:net.groupbuy.dao.impl.BaseDaoImpl.java

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);/*from w  w  w. ja va 2s.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:org.agric.oxm.utils.JpaUtils.java

/**
 * Find the Root with type class on CriteriaQuery Root Set
 * /* w  w w  .  j  a  va2 s . co  m*/
 * @param <T>
 *            root type
 * @param query
 *            criteria query
 * @param clazz
 *            root type
 * @return Root<T> of null if none
 */
public static <T> Root<T> findRoot(CriteriaQuery<?> query, Class<T> clazz) {

    for (Root<?> r : query.getRoots()) {
        if (clazz.equals(r.getJavaType())) {
            return (Root<T>) r.as(clazz);
        }
    }
    return null;
}

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

/**
 * Copy Criteria without Selection/* w  w  w  .  j  a  v a2s  .  co  m*/
 * 
 * @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.apache.ambari.server.api.query.JpaSortBuilder.java

/**
 * Builds the list of sort orders based on the supplied request and JPA
 * predicate visitor.//w ww. j a v a 2s  .c o  m
 *
 * @param sortRequests
 *          the Ambari sort request properties to turn into a JPA sort
 *          request. If {@code null} or the {@link SortRequestProperty} list
 *          is null, an empty list is returned.
 * @param visitor
 *          a visitor that knows how to convert the Ambari properties into
 *          {@link SingularAttribute} (not {@code null}).
 * @return a list of sorts or an empty list if none (never {@code null}).
 */
public List<Order> buildSortOrders(SortRequest sortRequest, JpaPredicateVisitor<T> visitor) {

    if (null == sortRequest || null == sortRequest.getProperties()) {
        return Collections.emptyList();
    }

    CriteriaBuilder builder = visitor.getCriteriaBuilder();
    List<SortRequestProperty> sortProperties = sortRequest.getProperties();
    List<Order> sortOrders = new ArrayList<Order>(sortProperties.size());

    for (SortRequestProperty sort : sortProperties) {
        String propertyId = sort.getPropertyId();

        List<? extends SingularAttribute<?, ?>> singularAttributes = visitor.getPredicateMapping(propertyId);

        if (null == singularAttributes || singularAttributes.size() == 0) {
            continue;
        }

        Path<?> path = null;
        for (SingularAttribute<?, ?> singularAttribute : singularAttributes) {
            if (null == path) {

                CriteriaQuery<T> query = visitor.getCriteriaQuery();
                Set<Root<?>> roots = query.getRoots();

                // if there are existing roots; use the existing roots to prevent more
                // roots from being added potentially causing a cartesian product
                // where we don't want one
                if (null != roots && !roots.isEmpty()) {
                    Iterator<Root<?>> iterator = roots.iterator();
                    while (iterator.hasNext()) {
                        Root<?> root = iterator.next();

                        Class<?> visitorEntityClass = visitor.getEntityClass();
                        if (ObjectUtils.equals(visitorEntityClass, root.getJavaType())
                                || ObjectUtils.equals(visitorEntityClass, root.getModel().getJavaType())) {
                            path = root.get(singularAttribute.getName());
                            break;
                        }
                    }
                }

                // no roots exist already which match this entity class, create a new
                // path
                if (null == path) {
                    path = query.from(visitor.getEntityClass()).get(singularAttribute.getName());
                }
            } else {
                path = path.get(singularAttribute.getName());
            }
        }

        Order sortOrder = null;
        if (sort.getOrder() == org.apache.ambari.server.controller.spi.SortRequest.Order.ASC) {
            sortOrder = builder.asc(path);
        } else {
            sortOrder = builder.desc(path);
        }

        sortOrders.add(sortOrder);
    }

    return sortOrders;
}

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

/**
 * Copy Criteria without Selection/*from   w w w . j a va  2 s  . c o  m*/
 *
 * @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());
}