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:eu.uqasar.service.ProductService.java

public List<Product> sortDescendingDates() {
    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 av a2s  .  c  om
    return em.createQuery(criteria.orderBy(cb.desc(from.get("releaseDate")))).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) {// www  . j  a v  a  2s . 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.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);// w  w w  .j a  va 2 s .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:com.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java

/**
 * Adds the sorting./*from  w  w w  .j  ava  2 s  . co  m*/
 *
 * @param sortField
 *            the sort field
 * @param sortOrder
 *            the sort order
 * @param query
 *            the query
 */
private void addSorting(String sortField, String sortOrder, CriteriaQuery query, CriteriaBuilder builder,
        Root<BulkUpload> 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.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  w w.j a  v  a2s.c  om*/
    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:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java

/**
 * {@inheritDoc}//from  w ww. j ava  2  s  .com
 */
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.github.aynu.mosir.core.enterprise.persistence.SmartRepositoryProducer.java

/**
 * ???/*from  ww  w  . j ava 2s. co m*/
 * @return ?()
 */
@Produces
public SmartRepository<Testee, TesteeFilter> createTestee() {
    final SmartRepositoryListener<Testee, TesteeFilter> listener = new AbstractSmartRepositoryListener<Testee, TesteeFilter>() {
        @Override
        public CriteriaQuery<Testee> query(final CriteriaBuilder builder, final CriteriaQuery<Testee> query,
                final Root<Testee> root, final TesteeFilter filter) {
            query.select(root);
            if (StringUtils.isNotEmpty(filter.getCode())) {
                query.where(builder.equal(root.get("code"), filter.getCode()));
            }
            return query.orderBy(createOrders(builder, root, filter));
        }
    };
    return new SmartRepositoryImpl<>(Testee.class, manager, listener);
}

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

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

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

/**
 * {@inheritDoc}/*w  w w.j a v a 2 s  .  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: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>();
    }/*from  w  ww .j ava2 s  .c o  m*/

    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();
}