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:th.co.geniustree.dental.spec.EmployeeSpec.java

public static Specification<Employee> emailLike(final String keyword) {
    return new Specification<Employee>() {

        @Override//from w  ww .  j av  a2 s.  c  o  m
        public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
            CriteriaQuery cc = cq.orderBy(cb.desc(root.get(Employee_.id)));
            return cb.like(cb.upper(root.get(Employee_.email)), keyword.toUpperCase());
        }
    };
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Copy Criteria without Selection./*from w ww .ja v  a  2  s. c o m*/
 * @param from source Criteria.
 * @param to destination Criteria.
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {
    copyCriteriaWithoutSelectionAndOrder(from, to);
    to.orderBy(from.getOrderList());
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Adds the "order by" clause to a JPA 2 criteria query
 * //from   www .  j a  v a 2  s .c  o m
 * @param builder
 *            the criteria builder
 * @param cq
 *            the criteria query
 * @param root
 *            the query root
 * @param sortOrder
 *            the sort object
 * @return
 */
private static <T, R> CriteriaQuery<R> addSortInformation(CriteriaBuilder builder, CriteriaQuery<R> cq,
        Root<T> root, SortOrder... sortOrders) {
    if (sortOrders != null && sortOrders.length > 0) {
        List<javax.persistence.criteria.Order> orders = new ArrayList<>();
        for (SortOrder sortOrder : sortOrders) {
            Expression<?> property = (Expression<?>) getPropertyPath(root, sortOrder.getProperty());
            orders.add(sortOrder.isAscending() ? builder.asc(property) : builder.desc(property));
        }
        cq.orderBy(orders);
    }
    return cq;
}

From source file:eu.uqasar.service.company.CompanyService.java

/**
 * /*from  ww  w  .ja  v  a 2 s. c om*/
 * @param innovationObjective
 * @return
 */
public List<Company> getAllByAscendingName() {
    logger.infof("loading all Company ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Company> criteria = cb.createQuery(Company.class);
    Root<Company> root = criteria.from(Company.class);
    criteria.orderBy(cb.asc(root.get(Company_.name)));
    return em.createQuery(criteria).getResultList();
}

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

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

From source file:com.nuevebit.persistence.repository.JPASearchableRepository.java

@Override
public final List<T> search(S searchCriteria, Pageable pageable) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(getEntityClass());

    Root<T> p = createQueryRoot(query, searchCriteria);
    query.select(p);/*from   w  w w .j av  a  2 s.  co m*/

    Order sortOrder = getSortOrder(cb);
    if (sortOrder != null) {
        query.orderBy(sortOrder);
    }

    TypedQuery<T> typedQuery = getEntityManager().createQuery(query);
    if (pageable != null) {
        typedQuery.setFirstResult(pageable.getOffset()).setMaxResults(pageable.getPageSize());
    }

    return typedQuery.getResultList();
}

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

/**
 * //from ww  w  .ja  v  a2  s. c  o  m
 * @param first
 * @param count
 * @return
 */
public List<Process> getAllByAscendingName(int first, int count) {
    logger.infof("loading all Processes ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    criteria.orderBy(cb.asc(root.get(Process_.name)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

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

/**
 * {@inheritDoc}/*  ww w . j a va  2 s .  co  m*/
 */
public BulkUpload findLastBulkUpload() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BulkUpload> query = builder.createQuery(BulkUpload.class);
    Root<BulkUpload> root = query.from(BulkUpload.class);
    query.select(root);
    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 bulkupload", e);
    }
    return null;
}

From source file:net.groupbuy.dao.impl.TagDaoImpl.java

public List<Tag> findList(Type type) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tag> criteriaQuery = criteriaBuilder.createQuery(Tag.class);
    Root<Tag> root = criteriaQuery.from(Tag.class);
    criteriaQuery.select(root);// www . j av a 2s  . c  o  m
    if (type != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("type"), type));
    }
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

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

public List<Product> sortAscendingDates() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.select(from);//from  w  w  w. j ava2  s . c  o  m
    return em.createQuery(criteria.orderBy(cb.asc(from.get("releaseDate")))).getResultList();
}