Example usage for javax.persistence.criteria Selection getAlias

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

Introduction

In this page you can find the example usage for javax.persistence.criteria Selection 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:org.jdal.dao.jpa.JpaUtils.java

/**
 * Gets The result alias, if none set a default one and return it
 * @param selection /*from w  w  w . j av a  2 s .c o  m*/
 * @return root alias or generated one
 */
public static synchronized <T> String getOrCreateAlias(Selection<T> selection) {
    // reset alias count
    if (aliasCount > 1000)
        aliasCount = 0;

    String alias = selection.getAlias();
    if (alias == null) {
        alias = "JDAL_generatedAlias" + aliasCount++;
        selection.alias(alias);
    }
    return alias;

}

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

/**
 * Method to translate criteriaQuery into JPQL.
 * /*  www. j  av  a2s  .  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:com.zero.dao.impl.BaseDaoImpl.java

private synchronized String getAlias(Selection<?> selection) {
    if (selection != null) {
        String alias = selection.getAlias();
        if (alias == null) {
            if (aliasCount >= 1000) {
                aliasCount = 0;/*from   ww  w  .  jav  a2  s  . c o  m*/
            }
            alias = "shopxxGeneratedAlias" + aliasCount++;
            selection.alias(alias);
        }
        return alias;
    }
    return null;
}

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

private synchronized String getAlias(Selection<?> selection) {
    if (selection != null) {
        String alias = selection.getAlias();
        if (alias == null) {
            if (aliasCount >= 1000) {
                aliasCount = 0;//w  w w . ja  v a2 s .  c  o  m
            }
            alias = "groupbuyGeneratedAlias" + aliasCount++;
            selection.alias(alias);
        }
        return alias;
    }
    return null;
}

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

private synchronized String getAlias(Selection<?> selection) {
    if (selection != null) {
        String alias = selection.getAlias();
        if (alias == null) {
            if (aliasCount >= 1000) {
                aliasCount = 0;/*from w ww .  ja v  a  2 s .co m*/
            }
            alias = "dp2345GeneratedAlias" + aliasCount++;
            selection.alias(alias);
        }
        return alias;
    }
    return null;
}

From source file:gov.guilin.dao.impl.BaseDaoImpl.java

private synchronized String getAlias(Selection<?> selection) {
    if (selection != null) {
        String alias = selection.getAlias();
        if (alias == null) {
            if (aliasCount >= 1000) {
                aliasCount = 0;// w  w  w .  j  a  va2  s .c  o m
            }
            alias = "guilinGeneratedAlias" + aliasCount++;
            selection.alias(alias);
        }
        return alias;
    }
    return null;
}

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

private synchronized String getAlias(Selection<?> selection) {
    if (selection != null) {
        String alias = selection.getAlias();
        if (alias == null) {
            if (aliasCount >= 1000) {
                aliasCount = 0;//  w  w  w. j a  v a2  s .c  o  m
            }
            alias = "osxxGeneratedAlias" + aliasCount++;
            selection.alias(alias);
        }
        return alias;
    }
    return null;
}

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

private synchronized String getAlias(Selection<?> selection) {
    if (selection != null) {
        String alias = selection.getAlias();
        if (alias == null) {
            if (aliasCount >= 1000) {
                aliasCount = 0;//from w  w  w.  ja v a 2s  . com
            }
            alias = "preschoolEduGeneratedAlias" + aliasCount++;
            selection.alias(alias);
        }
        return alias;
    }
    return null;
}

From source file:org.apache.openjpa.persistence.criteria.CriteriaQueryImpl.java

/**
 * Gets the alias of the given node. Creates an automatic alias, if necessary.
 *///from   w w w.  j  a  v a2s  .co  m
public String getAlias(Selection<?> selection) {
    String alias = selection.getAlias();
    if (alias != null) {
        _aliases.put(selection, alias);
        return alias;
    }
    alias = ALIAS_BASE + (++aliasCount);
    while (_aliases.containsValue(alias))
        alias = ALIAS_BASE + (++aliasCount);
    ((SelectionImpl<?>) selection).setAutoAlias(alias);
    _aliases.put(selection, alias);
    return _aliases.get(selection);
}

From source file:org.batoo.jpa.core.impl.criteria.jpql.JpqlQuery.java

/**
 * Constructs the order by fragment of the query.
 * /* w ww  . ja  va  2s .com*/
 * @param cb
 *            the criteria builder
 * @param q
 *            the query
 * @param selections
 *            the selections
 * @param orderBy
 *            the order by definitions
 * 
 * @since 2.0.0
 */
private void constructOrder(CriteriaBuilderImpl cb, CriteriaQueryImpl<?> q, List<Selection<?>> selections,
        Tree orderBy) {
    final List<Order> orders = Lists.newArrayList();

    for (int i = 0; i < orderBy.getChildCount(); i++) {
        final Tree orderByItem = orderBy.getChild(i);
        final Tree orderItem = orderByItem.getChild(0);

        Order order = null;

        if (orderItem.getType() == JpqlParser.ID) {
            final String alias = orderItem.getText();
            for (final Selection<?> selection : selections) {
                if (alias.equals(selection.getAlias())) {
                    order = orderByItem.getChildCount() == 2 ? //
                            cb.desc((Expression<?>) selection) : //
                            cb.asc((Expression<?>) selection);

                    break;
                }
            }

            if (order == null) {
                throw new PersistenceException("Alias is not bound: " + alias);
            }
        } else {

            order = orderByItem.getChildCount() == 2 ? //
                    cb.desc(this.getExpression(cb, q, orderItem, null)) : //
                    cb.asc(this.getExpression(cb, q, orderByItem.getChild(0), null));

        }

        orders.add(order);
    }

    q.orderBy(orders);
}