Example usage for javax.persistence.criteria CriteriaBuilder asc

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

Introduction

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

Prototype

Order asc(Expression<?> x);

Source Link

Document

Create an ordering by the ascending value of the expression.

Usage

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

public List<FriendLink> findList(Type type) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FriendLink> criteriaQuery = criteriaBuilder.createQuery(FriendLink.class);
    Root<FriendLink> root = criteriaQuery.from(FriendLink.class);
    criteriaQuery.select(root);//from   w ww  .  j ava2 s . c om
    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: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>
 * //  www.j  ava2s.  co  m
 * @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:net.groupbuy.dao.impl.NavigationDaoImpl.java

public List<Navigation> findList(Position position) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Navigation> criteriaQuery = criteriaBuilder.createQuery(Navigation.class);
    Root<Navigation> root = criteriaQuery.from(Navigation.class);
    criteriaQuery.select(root);/*from  w  ww  .j  a v  a2s.com*/
    if (position != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("position"), position));
    }
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

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);/*from w  ww  .ja  v  a 2s  .  c  om*/
    if (orderByProperty != null) {
        c.orderBy(qb.asc(p.get(orderByProperty)));
    }
    TypedQuery<T> q = em.createQuery(c);
    return q;
}

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 {/*w w w .j a  v  a2 s. c o  m*/
            criteriaQuery.orderBy(builder.desc(from.get(attribute)));
        }
    }

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

From source file:br.com.compositeam.data.MemberDaoImpl.java

public List<Member> findAllOrderedByName() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    /*//from  w  w  w.  j a v  a2  s.  com
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}

From source file:$.MemberDaoImpl.java

public List<Member> findAllOrderedByName() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
        Root<Member> member = criteria.from(Member.class);

        /*//from w  ww  .  j a  v  a  2 s  . c o m
         * Swap criteria statements if you would like to try out type-safe criteria queries, a new
         * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
         */

        criteria.select(member).orderBy(cb.asc(member.get("name")));
        return em.createQuery(criteria).getResultList();
    }

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

public List<User> getAllByAscendingName(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.asc(root.get(User_.lastName)), cb.asc(root.get(User_.firstName)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:com.fantasy.stataggregator.workers.GameDataRetrieverTask.java

/**
 * Sets the statistical year to be requested.
 *
 * @param year/* ww  w  .j  a  v  a2  s .co  m*/
 * @throws java.text.ParseException
 */
@Override
public void setYear(int year) throws ParseException {
    if (Objects.nonNull(ctx)) {
        this.year = year;
        isTaskComplete = false;
        GameScheduleRepository gsr = ctx.getBean(GameScheduleRepository.class);
        if (year == Integer.MAX_VALUE) {
            schedules = gsr.findAll();
        } else {
            SimpleDateFormat sdf = ctx.getBean(SimpleDateFormat.class);
            sdf.applyLocalizedPattern("yyyyMMdd");

            Date min = sdf.parse(year + START_OF_YEAR);
            Date max = sdf.parse(year + END_OF_YEAR);

            CriteriaBuilder cb = gsr.getCriteriaBuilder();
            CriteriaQuery<GameSchedule> cq = gsr.getCriteriaQuery();
            Root<GameSchedule> gameSchedule = gsr.getRoot();
            cq.select(gameSchedule).where(cb.between(gameSchedule.get(GameSchedule_.gamedate), min, max))
                    .orderBy(cb.asc(gameSchedule.get(GameSchedule_.gameid)));

            schedules = gsr.getCriteriaList(cq);
            System.out.println(schedules.size());
        }
    }
}

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

/**
 * Adds the sorting./*from w ww .j a  v a  2s  .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)));
        }
    }
}