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:eu.uqasar.service.AbstractService.java

protected <T extends Persistable> List<T> getAllOrderedExcept(Class<T> clazz, Collection<T> toExclude,
        Order... orders) {//  w  ww  . j a va  2s .c o m
    if (toExclude == null || toExclude.isEmpty()) {
        return getAll(clazz);
    }
    logger.infof("loading all %s except %s ...", getReadableClassName(clazz), toExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    Root<T> root = query.from(clazz);
    if (orders != null && orders.length > 0) {
        query.orderBy(orders);
    }
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:org.openmeetings.app.data.basic.dao.LdapConfigDaoImpl.java

public List<LdapConfig> getLdapConfigs(int start, int max, String orderby, boolean asc) {
    try {/*  w w w  .jav a2s .c om*/
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<LdapConfig> cq = cb.createQuery(LdapConfig.class);
        Root<LdapConfig> c = cq.from(LdapConfig.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<LdapConfig> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<LdapConfig> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getLdapConfigs]", ex2);
    }
    return null;
}

From source file:com.aimdek.ccm.dao.impl.CreditCardRepositoryImpl.java

/**
 * Adds the sorting./*from w ww  .j  a  va2  s.  c  o m*/
 *
 * @param sortField the sort field
 * @param sortOrder the sort order
 * @param query the query
 * @param builder the builder
 * @param root the root
 */

private void addSorting(String sortField, String sortOrder, CriteriaQuery query, CriteriaBuilder builder,
        Root<CreditCard> root) {
    if (CommonUtil.isNotNull(sortField) && (sortField.equalsIgnoreCase(FIELDCONSTANT_CARDNUMBER)
            || sortField.equalsIgnoreCase(FIELDCONSTANT_CREDITLIMIT)
            || sortField.equalsIgnoreCase(FIELDCONSTANT_BALANCE))) {
        if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) {
            query.orderBy(builder.asc(root.get(sortField)));
        } else {
            query.orderBy(builder.desc(root.get(sortField)));
        }
    }
}

From source file:com.saake.invoicer.sessionbean.PurchaseOrderFacade.java

@Override
public List<PurchaseOrder> findAll() {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();

    Root<PurchaseOrder> root = cq.from(PurchaseOrder.class);
    cq.select(root);/*from  w  ww. j ava2s.c  om*/

    ParameterExpression<String> status = cb.parameter(String.class);

    //        cq.where(cb.notEqual(invRoot.get("status"), status));
    cq.where(cb.notEqual(root.get("deleted"), 'Y'));
    cq.orderBy(cb.desc(root.get("purchaseOrderId")));
    Query query = getEntityManager().createQuery(cq);
    //        query.setParameter(status, PaymentStatusEnum.DELETE.getValue());

    List<PurchaseOrder> list = query.getResultList();

    for (PurchaseOrder wo : list) {
        List<PurchaseOrderItems> tempDel = new ArrayList<>();
        for (PurchaseOrderItems wot : wo.getPurchaseOrderItems()) {
            if (wot.isDeleted()) {
                tempDel.add(wot);
            }
        }
        wo.getPurchaseOrderItems().removeAll(tempDel);
    }
    return list;
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

/**
 * Creates a new {@link TypedQuery} from the given {@link Specification}.
 * //from   w w w . ja  v  a2 s .  c  o  m
 * @param spec can be {@literal null}
 * @param pageable can be {@literal null}
 * @return
 */
private TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable) {

    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(getDomainClass());

    Root<T> root = applySpecificationToCriteria(spec, query);
    query.select(root);

    if (pageable != null) {
        query.orderBy(toOrders(pageable.getSort(), root, builder));
    }

    return getEntityManager().createQuery(query);
}

From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * Adds the sorting./*from  w w  w. j  a  v  a 2  s  .  c  om*/
 *
 * @param sortField the sort field
 * @param sortOrder the sort order
 * @param query the query
 * @param builder the builder
 * @param root the root
 */
private void addSorting(String sortField, String sortOrder, CriteriaQuery<User> query, CriteriaBuilder builder,
        Root<User> root) {
    boolean isSet = FALSE;
    if (CommonUtil.isNotNull(sortField) && !sortField.contains(FIELDCONSTANT_CARDNUMBER)
            && !sortField.contains(FIELDCONSTANT_CREDITLIMIT) && !sortField.contains(FIELDCONSTANT_BALANCE)) {
        isSet = TRUE;
        if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) {
            query.orderBy(builder.asc(root.get(sortField)));
        } else {
            query.orderBy(builder.desc(root.get(sortField)));
        }
    }
    if (!isSet) {
        query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT)));
    }
}

From source file:eu.uqasar.service.dataadapter.JiraDataService.java

/**
 * //from  ww  w . j av  a 2 s. c om
 * @param first
 * @param count
 * @return
 */
public List<JiraMetricMeasurement> getAllByAscendingName(int first, int count) {
    logger.infof("loading all JiraMetricMeasurement ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> criteria = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = criteria.from(JiraMetricMeasurement.class);
    criteria.orderBy(cb.asc(root.get(JiraMetricMeasurement_.self)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:org.openmeetings.app.data.basic.Configurationmanagement.java

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {//from   w ww  .  j ava2  s  . c om
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
        Root<Configuration> c = cq.from(Configuration.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Configuration> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Configuration> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getConfigurations]", ex2);
    }
    return null;
}

From source file:eu.uqasar.service.dataadapter.JiraDataService.java

/**
 * Get the latest date of measurement snapshots
 * @return// w w  w . j ava  2  s  . com
 */
public Date getLatestDate() {
    logger.info("Get the latest date from JIRA measurements...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    query.select(root);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    Date latest;
    try {
        JiraMetricMeasurement m = em.createQuery(query).setMaxResults(1).getSingleResult();
        latest = m.getTimeStamp();
    } catch (NoResultException nre) {
        latest = null;
    }
    return latest;
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute/*from  www. ja  v a2 s.  co m*/
public List<T> findByFullTexts(final String terms, final OrderBy<T> orders, final long first, final long count,
        final String... fg) {

    LOGGER.debug("searching by text {} terms {}, fg {}", new Object[] { persistentClass, terms, fg });

    if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) {
        return Collections.emptyList();
    }

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(newFullTextPredicate(cb, p, terms));

    if (orders != null) {
        List<Order> orderList = orders.orders(cb, p);
        if (orderList != null && !orderList.isEmpty()) {
            q.orderBy(orderList);
        }
    }

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;
}