Example usage for javax.persistence.criteria Root getAlias

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

Introduction

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

Prototype

String getAlias();

Source Link

Document

Return the alias assigned to the tuple element or null, if no alias has been assigned.

Usage

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

/**
 * Method to translate criteriaQuery into JPQL.
 * // www  .  jav a2  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:org.batoo.jpa.core.impl.criteria.AbstractCriteriaQueryImpl.java

/**
 * {@inheritDoc}/*  w  w  w  .  j  a  v  a 2 s . c o m*/
 * 
 */
@Override
public String generateJpql() {
    final StringBuilder builder = new StringBuilder();

    this.ensureSelection();

    builder.append("select ");

    // append distinct if necessary
    if (this.distinct) {
        builder.append("distinct ");
    }

    builder.append(this.selection.generateJpqlSelect(this, true));

    final Collection<String> roots = Collections2.transform(this.getRoots(), new Function<Root<?>, String>() {

        @Override
        public String apply(Root<?> input) {
            final RootImpl<?> root = (RootImpl<?>) input;

            final StringBuilder builder = new StringBuilder(input.getModel().getName());

            if (StringUtils.isNotBlank(input.getAlias())) {
                builder.append(" as ").append(input.getAlias());
            }

            final String joins = root.generateJpqlJoins(AbstractCriteriaQueryImpl.this);

            if (StringUtils.isNotBlank(joins)) {
                builder.append("\n").append(BatooUtils.indent(joins));
            }
            return builder.toString();
        }
    });
    builder.append("\nfrom ").append(Joiner.on(", ").join(roots));

    if (this.getRestriction() != null) {
        builder.append("\nwhere\n\t").append(this.getRestriction().generateJpqlRestriction(this));
    }

    if (this.getGroupList().size() > 0) {
        final String groupBy = Joiner.on(", ")
                .join(Lists.transform(this.getGroupList(), new Function<Expression<?>, String>() {

                    @Override
                    public String apply(Expression<?> input) {
                        return ((AbstractExpression<?>) input)
                                .generateJpqlRestriction(AbstractCriteriaQueryImpl.this);
                    }
                }));

        builder.append("\ngroup by\n\t").append(groupBy);
    }

    if (this.getGroupRestriction() != null) {
        builder.append("\nhaving\n\t").append(this.getGroupRestriction().generateJpqlRestriction(this));
    }

    return builder.toString();
}