Example usage for javax.persistence.criteria Selection getJavaType

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

Introduction

In this page you can find the example usage for javax.persistence.criteria Selection 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.
 * //from   w  ww. j a v  a  2  s  . c  om
 * @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.batoo.jpa.core.impl.criteria.AbstractCriteriaQueryImpl.java

/**
 * @param selections//www. ja  v a 2 s  .c  o  m
 *            the selections
 * 
 * @since 2.0.0
 */
public void updateResultClass(List<Selection<?>> selections) {
    if (selections.size() == 1) {
        final Selection<?> selection = selections.get(0);
        this.resultType = (Class<T>) selection.getJavaType();
    } else {
        this.resultType = (Class<T>) Object[].class;
    }
}