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:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

protected int applySlicing(CriteriaQuery<E> criteriaQuery, CriteriaBuilder criteriaBuilder,
        Path<? extends Number> taskIdPath, Integer first, Integer last) {
    // apply slicing
    int maxResults = -1;

    // first is set
    if (first != null) {
        if (first < 0) {
            throw new IllegalArgumentException("Argument 'first' must be equal or greater than 0");
        }/*  ww w .j  a  v a2 s  .  co  m*/

        criteriaQuery.orderBy(criteriaBuilder.asc(taskIdPath));
        maxResults = first;
    }

    // last is set
    if (last != null) {
        if (last < 0) {
            throw new IllegalArgumentException("Argument 'last' must be equal or greater than 0");
        }

        criteriaQuery.orderBy(criteriaBuilder.desc(taskIdPath));
        maxResults = last;
    }

    if (first == null && last == null) {
        criteriaQuery.orderBy(criteriaBuilder.asc(taskIdPath));
    }
    return maxResults;
}

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

/**
 * /* www. j av a 2  s. c  om*/
 * @param first
 * @param count
 * @param adapter
 * @return
 */
public List<CubesMetricMeasurement> getAllByAdapter(int first, int count, AdapterSettings adapter) {
    logger.info("Get measurements for adapter: " + adapter);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.adapter), adapter);
    query.orderBy(cb.asc(root.get(CubesMetricMeasurement_.self)));
    query.where(condition);
    return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

@Override
public List<T> findAll(SingularAttribute<T, ?> orderAttribute, boolean orderAsc, int firstResult,
        int maxResults, Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);// w w w. j  a  v a2s. c  om
    select.where(predicateList.toArray(new Predicate[0]));

    if (orderAsc) {
        criteriaQuery.orderBy(queryBuilder.asc(rootQuery.get(orderAttribute)));
    } else {
        criteriaQuery.orderBy(queryBuilder.desc(rootQuery.get(orderAttribute)));
    }

    TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);

    if (firstResult != -1) {
        typedQuery.setFirstResult(firstResult);
    }
    if (maxResults != -1) {
        typedQuery.setMaxResults(maxResults);
    }

    return typedQuery.getResultList();
}

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

/**
 * @param project//w ww. j  a v  a  2 s  .  c  o  m
 * @return List with all the measurements of a project
 */
public List<CubesMetricMeasurement> getMeasurementByProject(Project project) {
    logger.info("Obtaining measurements for the project: " + project);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.project), project);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();

}

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

/**
 * /*from   w  ww  . j  av a  2  s . c o  m*/
 * @param metric
 * @return
 * @throws uQasarException
 */
public List<CubesMetricMeasurement> getMeasurementsForMetric(String metric) throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of serviceIds available.
 *
 * @param modelVersion/*from w w  w .ja  va  2  s. c om*/
 *            the model version.
 * @return the list of serviceIds available.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<String> getRevisions() {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<String> q = cb.createQuery(String.class);
    final Root<EnterpriseType> f = q.from(EnterpriseType.class);
    q.select(f.<String>get(EnterpriseType_.modelVersion));
    q.orderBy(cb.asc(f.<String>get(EnterpriseType_.modelVersion)));
    final TypedQuery<String> typedQuery = this.em.createQuery(q);
    final List<String> value = typedQuery.getResultList();
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the last model version (lexical compare).
 *
 * @return the last model version./*from   ww w  .j  a  v  a 2 s  .c om*/
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public String getLastModelVersion() {
    String value = null;
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<String> q = cb.createQuery(String.class);
    final Root<EnterpriseType> f = q.from(EnterpriseType.class);
    q.select(f.<String>get(EnterpriseType_.modelVersion));
    q.orderBy(cb.desc(f.<String>get(EnterpriseType_.modelVersion)));
    final TypedQuery<String> typedQuery = this.em.createQuery(q);
    final List<String> list = typedQuery.getResultList();
    if (Objects.nonNull(list) && list.size() > 0) {
        value = list.get(0);
    }
    return value;
}

From source file:com.sapito.db.dao.AbstractDao.java

/**
 * Obtiene todos los registros para una entidad dada ordenados con los
 * criterios establecidos a traves de la lista <code>ordering</code>
 * //from   w  w  w . j  a  v  a  2  s  .com
 * @param ordering : ["fieldName", "ASC" | "DESC"] <br>
 *                   (if != "ASC" se utiliza "DESC" por default)
 * 
 * @return Registros recuperados ordenados mediante los criterios dados
 */
public List<T> findAll(LinkedHashMap<String, String> ordering) {
    CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
    Root<T> root = cq.from(entityClass);
    cq.select(root);

    if (ordering != null) {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        Set<String> set = ordering.keySet();
        List<Order> orders = new ArrayList<>();
        for (String orderingField : set) {
            Order order = (ordering.get(orderingField).equals("ASC")) ? cb.asc(root.get(orderingField))
                    : cb.desc(root.get(orderingField));
            orders.add(order);
        }

        cq.orderBy(orders);
    }

    return entityManager.createQuery(cq).setMaxResults(MAX_RECORDS_RETURNED).getResultList();
}

From source file:com.uni.dao.etc.UniJpaRepository.java

/**
 * Creates a {@link TypedQuery} for the given {@link Specification} and {@link Sort}.
 * /*  www.jav a2 s .c  o m*/
 * @param spec can be {@literal null}.
 * @param sort can be {@literal null}.
 * @return
 */
private TypedQuery<T> getQuery(Specification<T> spec, Sort sort) {

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

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

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

    return applyLockMode(em.createQuery(query));
}

From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java

private Query applicationServersForSuggestBox(String input) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> q = cb.createQuery(String.class);
    Root<ResourceEntity> r = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> resType = r.join("resourceType");
    Predicate appServerNamePred = cb.like(resType.<String>get("name"),
            DefaultResourceTypeDefinition.APPLICATIONSERVER.name());
    input = input + "%";
    q.where(cb.and(appServerNamePred, cb.like(r.<String>get("name"), input)));
    q.select(r.<String>get("name"));
    q.distinct(true);//from  w ww  .  j a  va  2 s.co  m

    q.orderBy(cb.asc(r.get("name")));

    return entityManager.createQuery(q);
}