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:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java

/**
 * Adds the sorting./*from www  . j  ava2 s  .co 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<Statement> root) {
    if (CommonUtil.isNotNull(sortField)) {
        if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) {
            query.orderBy(builder.asc(root.get(sortField)));
        } else {
            query.orderBy(builder.desc(root.get(sortField)));
        }
    }
}

From source file:nc.noumea.mairie.organigramme.core.dao.PersistentManager.java

@SuppressWarnings("unchecked")
private TypedQuery<T> constructTypedQueryByPropertyOrderBy(Class<? extends T> classe, String property,
        Object value, String orderByProperty) {
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<T> c = (CriteriaQuery<T>) qb.createQuery(classe);
    Root<T> p = (Root<T>) c.from(classe);
    Predicate condition = qb.equal(p.get(property), value);
    c.where(condition);/*w w  w .  ja  v  a  2  s.c  o  m*/
    if (orderByProperty != null) {
        c.orderBy(qb.asc(p.get(orderByProperty)));
    }
    TypedQuery<T> q = em.createQuery(c);
    return q;
}

From source file:de.egore911.persistence.selector.AbstractSelector.java

private TypedQuery<T> buildQuery() {
    EntityManager em = EntityManagerUtil.getEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(getEntityClass());
    Root<T> from = cq.from(getEntityClass());
    List<Predicate> predicates = generatePredicateList(builder, from, cq);
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    cq.orderBy(generateOrderList(builder, from));
    cq.select(from);/*from  w  w  w  .ja  v a2s .  com*/
    return em.createQuery(cq);
}

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

/**
 * {@inheritDoc}//from   www . j  a va  2  s . co m
 */
public User findLastCustomer() {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.select(root);
    query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER));
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last customer", e);
    }
    return null;
}

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

/**
 * Adds the sorting./*  w  ww  .  j a  v  a  2 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<Transaction> root) {
    if (CommonUtil.isNotNull(sortField)) {
        if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) {
            query.orderBy(builder.asc(root.get(sortField)));
        } else {
            query.orderBy(builder.desc(root.get(sortField)));
        }
    }
}

From source file:net.shopxx.dao.impl.MemberDaoImpl.java

public Page<Member> findPage(Member.RankingType rankingType, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class);
    Root<Member> root = criteriaQuery.from(Member.class);
    criteriaQuery.select(root);//from w  ww.ja va 2 s  .com
    if (rankingType != null) {
        switch (rankingType) {
        case point:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("point")));
            break;
        case balance:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("balance")));
            break;
        case amount:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("amount")));
            break;
        }
    }
    return super.findPage(criteriaQuery, pageable);
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllUsersByDescendingSkillCount(int first, int count) {

    logger.infof("loading all Users ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> root = criteria.from(User.class);

    criteria.orderBy(cb.desc(root.get(User_.skillCount)));

    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:utils.jpa.EntityResource.java

private List<T> getAllPriv(@BeanParam TableSearchQuery tb, List<String> fieldsSearchBy) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    cq.select(root);//from ww  w.  java 2  s . c  om

    //setting order
    if (tb.getOrderType().equals(OrderType.DESC)) {
        cq.orderBy(cb.desc(root.get(tb.getOrder())));
    } else {
        cq.orderBy(cb.asc(root.get(tb.getOrder())));
    }

    Query query = tb.createQuery(em, cb, cq, root);
    if (query == null) {
        query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy);
    }

    if (tb.getOffset() > 0) {
        query.setFirstResult(tb.getOffset());
    }
    if (tb.getLimit() > 0) {
        query.setMaxResults(tb.getLimit());
    }

    return query.getResultList();
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> getAllByDescendingNameFiltered(ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> root = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from   ww w .  j  av  a 2 s.c om*/

    criteria.orderBy(cb.desc(root.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:eu.uqasar.service.ProductService.java

public List<Product> getAllByAscendingNameFiltered(
        eu.uqasar.web.pages.products.panels.ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//  w w w.j  ava  2s  . c o  m

    criteria.orderBy(cb.asc(from.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}