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

/**
 * {@inheritDoc}/* w  w w  . ja v  a 2s .c  o m*/
 */
public Statement findLastStatement() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statement> query = builder.createQuery(Statement.class);
    Root<Statement> root = query.from(Statement.class);
    query.select(root);
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_STATEMENT_DATE)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last statement", e);
    }
    return null;
}

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

/**
 * {@inheritDoc}/*from w  ww.  j a  v a2s  . c  o  m*/
 */
public Transaction findLastTransaction() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> root = query.from(Transaction.class);
    query.select(root);
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_TRANSACTION_ID)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last transaction", e);
    }
    return null;

}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Account fetchNewestAccount() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    List<Account> candidates = entityManager
            .createQuery(q.select(account).orderBy(cb.desc(account.get(Account_.id)))).setMaxResults(1)
            .getResultList();//from w  w  w  . j a  v a 2  s.c o  m

    if (candidates.isEmpty()) {
        return null;
    } else {
        return candidates.get(0);
    }
}

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

/**
 * Adds the sorting.//  w  w w .  java2  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:org.apereo.lap.dao.RiskConfidenceRepositoryImpl.java

public List<RiskConfidence> findByUserCourseDate(final String user, final String course) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<RiskConfidence> criteria = builder.createQuery(RiskConfidence.class);
    Root<RiskConfidence> root = criteria.from(RiskConfidence.class);
    EntityType<RiskConfidence> type = entityManager.getMetamodel().entity(RiskConfidence.class);
    criteria.orderBy(builder.desc(root.get("dateCreated")));

    List<RiskConfidence> lastRiskConfidences = entityManager.createQuery(criteria).setFirstResult(0)
            .setMaxResults(1).getResultList();

    if (lastRiskConfidences == null || lastRiskConfidences.isEmpty()) {
        logger.warn("No risk confidence records found");
        return new ArrayList<RiskConfidence>();
    }// w  w w. ja  v a2  s  . com

    RiskConfidence lastRickConfidence = lastRiskConfidences.get(0);

    builder = entityManager.getCriteriaBuilder();
    criteria = builder.createQuery(RiskConfidence.class);
    root = criteria.from(RiskConfidence.class);
    type = entityManager.getMetamodel().entity(RiskConfidence.class);

    Predicate groupPredicate = builder.equal(root.get("groupId"), lastRickConfidence.getGroupId());

    if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(course)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(
                                root.get(type.getDeclaredSingularAttribute("alternativeId", String.class))),
                        user.toLowerCase()),
                builder.equal(
                        builder.lower(root.get(type.getDeclaredSingularAttribute("courseId", String.class))),
                        course.toLowerCase()));
    } else if (!StringUtils.isEmpty(user)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(
                                root.get(type.getDeclaredSingularAttribute("alternativeId", String.class))),
                        user.toLowerCase()));
    } else if (!StringUtils.isEmpty(course)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(root.get(type.getDeclaredSingularAttribute("courseId", String.class))),
                        course.toLowerCase()));
    } else {
        criteria.where(groupPredicate);
    }

    return entityManager.createQuery(criteria).getResultList();
}

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

/**
 * {@inheritDoc}//from w w w .j  a  v  a2 s .c  o 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.//from   w w w .  j av  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: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 www. j  a  v a  2s  .  c  o 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.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 CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass);
    final Root<E> from = criteriaQuery.from(this.entityClass);

    if (asc) {/*from  w  ww .j  ava2  s . c  o  m*/
        criteriaQuery.orderBy(builder.asc(from.get(attribute)));
    } else {
        criteriaQuery.orderBy(builder.desc(from.get(attribute)));
    }

    return this.find(criteriaQuery);
}

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);// www.  jav  a  2 s  .c om
    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);
}