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: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 {//from w w w .  java2s.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: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 ww .j ava2s  .co  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:org.jnap.core.persistence.jpa.DaoSupport.java

@Override
public List<E> findAll() {
    CriteriaQuery criteria = createCriteriaQuery();
    if (getDefaultOrder() != null) {
        criteria.orderBy(getDefaultOrder());
    }//from w  ww. j  a  v a 2  s . co m
    return findByCriteria(criteria);
}

From source file:com.samples.platform.core.SystemUserInitDao.java

/**
 * Get the {@link AuthenticationType}s out of the database.
 *
 * @param enabled//from  w w  w .j a  v a  2s  . c  om
 *            if not <code>null</code> and <code>true</code> only the
 *            enabled {@link AuthenticationType}s are replied.
 * @return the list of {@link AuthenticationType}s.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void enterSystemUser(final String contextName, final String userName, final String password,
        final String... roleNames) {
    AuthenticationType ac = this.of.createAuthenticationType();
    ac.setContext(contextName);
    ac.setEnabled(true);
    GrantedAuthorityType r;
    for (String roleName : roleNames) {
        r = this.of.createGrantedAuthorityType();
        r.setRoleName(roleName);
        ac.getGrantedAuthority().add(r);
    }
    ac.setPassword(password);
    ac.setUserName(userName);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    Predicate ands = cb.conjunction();
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), contextName));
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.userName), userName));
    q.where(ands);
    q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName)));
    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    try {
        AuthenticationType stored = typedQuery.getSingleResult();
        if (stored != null) {
            this.em.persist(ac);
        }
    } catch (NoResultException e) {
        this.em.persist(ac);
    }
}

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

private <T extends Persistable> List<T> getRangeOrdered(Class<T> clazz, int first, int count, Order... orders) {
    logger.infof("loading %d %s starting from %d  ...", count, getReadableClassName(clazz), first);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    query.from(clazz);/*from  w  ww . jav  a  2  s  .com*/
    if (orders != null && orders.length > 0) {
        query.orderBy(orders);
    }
    return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList();
}

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

public List<Process> getAllByAscendingEndDateNameFiltered(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 ww . ja  v a 2s  . c om

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

}

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()])));
    }/*  w  ww.ja  v a  2 s. co 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()])));
    }/* www.jav a  2s .  co  m*/

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

}

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

public List<Process> getAllAscendingStartDateNameFiltered(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()])));
    }/* w ww. ja v  a 2  s.com*/

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

}

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

protected List<T> getAllOrdered(Class<T> clazz, Order... orders) {
    logger.infof("loading all %ss ...", getReadableClassName(clazz));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteria = cb.createQuery(clazz);
    criteria.from(clazz);//w  w w  .ja va2  s  .  c  o  m
    if (orders != null && orders.length > 0) {
        criteria.orderBy(orders);
    }
    return em.createQuery(criteria).getResultList();
}