Example usage for javax.persistence.criteria CriteriaQuery orderBy

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

Introduction

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

Prototype

CriteriaQuery<T> orderBy(List<Order> o);

Source Link

Document

Specify the ordering expressions that are used to order the query results.

Usage

From source file:ru.codeinside.adm.AdminServiceImpl.java

@Override
public List<InfoSystem> queryInfoSystems(boolean source, String[] sort, boolean[] asc, int start, int count) {
    final CriteriaBuilder c = em.getCriteriaBuilder();
    final CriteriaQuery<InfoSystem> query = c.createQuery(InfoSystem.class);
    final Root<InfoSystem> system = query.from(InfoSystem.class);
    if (source) {
        query.where(c.equal(system.get(InfoSystem_.source), true));
    }/*from w  w w  . ja v a  2 s. c  o  m*/
    query.select(system);
    if (sort != null) {
        final Order[] orders = new Order[sort.length];
        for (int i = 0; i < sort.length; i++) {
            final Path<String> path = system.get(sort[i]);
            orders[i] = asc[i] ? c.asc(path) : c.desc(path);
        }
        query.orderBy(orders);
    }
    return chunk(start, count, query);
}

From source file:ru.savvy.jpafilterbuilder.FilterCriteriaBuilder.java

private void applyOrders(Root<T> rootPath, CriteriaQuery<T> query) {
    List<Order> orderList = new ArrayList<>();

    for (Map.Entry<String, Boolean> me : orders.entrySet()) {
        Path<?> path = getCompoundJoinedPath(rootPath, me.getKey(), true);
        if (me.getValue() == null || me.getValue().equals(true)) {
            orderList.add(cb.asc(path));
        } else {/*w ww .j a v a2s  .com*/
            orderList.add(cb.desc(path));
        }
    }
    query.orderBy(orderList);
}

From source file:ru.savvy.jpafilterbuilder.FilterCriteriaBuilder.java

/**
 * Resulting query with filters and orders, if orders are empty, than makes
 * default ascending ordering by root id to prevent paging confuses
 *
 * @return//from w w  w  .j  av a2  s  .c o m
 */
public CriteriaQuery<T> getQuery() {
    CriteriaQuery<T> query = cb.createQuery(clazz);
    Root<T> root = query.from(clazz);
    applyFilters(root, query);
    applyOrders(root, query);

    // add default ordering
    if (query.getOrderList() == null || query.getOrderList().isEmpty()) {
        EntityType<T> entityType = root.getModel();
        try {
            Field sortField = getSortAnnotation(entityType.getBindableJavaType());
            if (sortField == null)
                query.orderBy(
                        cb.asc(root.get(entityType.getId(entityType.getIdType().getJavaType()).getName())));
            else {
                DefaultOrder order = sortField.getAnnotation(DefaultOrder.class);
                if (order.asc()) {
                    query.orderBy(cb.asc(root.get(sortField.getName())));
                } else {
                    query.orderBy(cb.desc(root.get(sortField.getName())));
                }

            }
        } catch (Exception ex) {
            logger.warn("In" + this.getClass().getName(), ex);
        }
    }
    return query;
}