Example usage for javax.persistence.criteria CriteriaBuilder desc

List of usage examples for javax.persistence.criteria CriteriaBuilder desc

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder desc.

Prototype

Order desc(Expression<?> x);

Source Link

Document

Create an ordering by the descending value of the expression.

Usage

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  www. ja va 2s . co  m

    //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()])));
    }// w w w .j ava  2s  . c  o  m

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

}

From source file:net.dontdrinkandroot.persistence.dao.TypedJpaDao.java

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public List<E> findAll(final SingularAttribute<? super E, ?> attribute, final boolean asc,
        final int firstResult, final int maxResults) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass);
    final Root<E> from = criteriaQuery.from(this.entityClass);

    if (attribute != null) {
        if (asc) {
            criteriaQuery.orderBy(builder.asc(from.get(attribute)));
        } else {/*from  w w w  . j  ava  2  s.  c om*/
            criteriaQuery.orderBy(builder.desc(from.get(attribute)));
        }
    }

    return this.find(criteriaQuery, firstResult, maxResults);
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static List<RunView> viewList(User user, String moduleResourceUri, Integer offset, Integer limit,
        String cloudServiceName) throws ConfigurationException, ValidationException {
    List<RunView> views = null;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {//ww w .  j  a va 2 s.  c  om
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Run> critQuery = builder.createQuery(Run.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(rootQuery);
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        critQuery.orderBy(builder.desc(rootQuery.get("startTime")));
        TypedQuery<Run> query = em.createQuery(critQuery);
        if (offset != null) {
            query.setFirstResult(offset);
        }
        query.setMaxResults((limit != null) ? limit : DEFAULT_LIMIT);
        List<Run> runs = query.getResultList();
        views = convertRunsToRunViews(runs);
    } finally {
        em.close();
    }
    return views;
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * order by://  w  w w . j  a v  a  2  s .co  m
 * SELECT e.name, e.salary FROM jpa_query_employee e ORDER BY e.name DESC, e.salary DESC
 */
public void doOrderby() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
    Root<Employee> e = c.from(Employee.class);
    c.multiselect(e.get("name"), e.get("salary")).orderBy(cb.desc(e.get("name")), cb.desc(e.get("salary")));

    showResult(c);
}

From source file:com.june.app.board.repository.jpa.BoardRepositoryImpl.java

@Override
public Collection<Board> boardListWithPaging(Board vo) {
    int bbsId = vo.getBbsId();
    int pageSize = vo.getPageSize();
    int pageNumber = (int) vo.getPageIndex();

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Board> criteriaQuery = criteriaBuilder.createQuery(Board.class);

    Root<Board> from = criteriaQuery.from(Board.class);
    CriteriaQuery<Board> select = criteriaQuery.select(from);
    if (bbsId > 0) {
        criteriaQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId));
    }// w  w w .  j  a v  a  2  s. c o  m
    /**list desc for date*/
    criteriaQuery.orderBy(criteriaBuilder.desc(from.get("frstRegistPnttm")));
    TypedQuery<Board> typedQuery = em.createQuery(select);

    typedQuery.setFirstResult((pageNumber - 1) * pageSize);
    typedQuery.setMaxResults(pageSize);

    Collection<Board> fooList = typedQuery.getResultList();

    return fooList;

}

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

public List<Process> getAllByDescendingEndDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.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  a  v a 2  s. c o  m

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

}

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

public List<Process> getAllByDescendingStartDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from   w  w  w. j a v a 2  s .com

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

}

From source file:com.aimdek.ccm.dao.impl.CreditCardRepositoryImpl.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 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)));
        }
    }
}