Example usage for org.hibernate Criteria addOrder

List of usage examples for org.hibernate Criteria addOrder

Introduction

In this page you can find the example usage for org.hibernate Criteria addOrder.

Prototype

public Criteria addOrder(Order order);

Source Link

Document

Add an Order ordering to the result set.

Usage

From source file:br.com.senac.DAO.CidadeDAO.java

public List<Cidade> buscaPorEstado(int estadoId) {
    Session sessao = HibernateUtil.getSessionFactory().openSession();
    try {//from   w  ww. j  a v a2 s. c  om
        Criteria consulta = sessao.createCriteria(Cidade.class);
        consulta.add(Restrictions.eq("estado.id", estadoId));
        consulta.addOrder(Order.asc("nome"));
        List<Cidade> resultado = consulta.list();
        return resultado;
    } catch (RuntimeException erro) {
        throw erro;
    } finally {
        sessao.close();
    }
}

From source file:br.com.sgejs.dao.DaoArquivo.java

public List getAnos() throws HibernateException {
    Criteria cri = sessao.createCriteria(classe);
    cri.setProjection(Projections.distinct(Projections.property("ano")));
    cri.addOrder(Order.asc("ano"));
    return cri.list();
}

From source file:br.com.sgejs.dao.DaoArquivo.java

public List<Arquivo> getPropriedadesArquivo() throws HibernateException {
    Criteria cri = sessao.createCriteria(classe, "p");
    cri.addOrder(Order.asc("ano")).setProjection(Projections.projectionList()
            .add(Projections.property("p.id").as("id")).add(Projections.property("p.ano").as("ano"))
            .add(Projections.property("p.curso").as("curso")).add(Projections.property("p.tipo").as("tipo"))
            .add(Projections.property("p.caixa").as("caixa"))
            .add(Projections.property("p.criacao").as("criacao")))
            .setResultTransformer(new AliasToBeanResultTransformer(Arquivo.class));
    return cri.list();
}

From source file:br.com.sgejs.dao.DaoServidor.java

public List<Servidor> listPropriedades() {
    Criteria cri = sessao.createCriteria(classe, "s");
    cri.addOrder(Order.asc("nome"))
            .setProjection(Projections.projectionList().add(Projections.property("s.id").as("id"))
                    .add(Projections.property("s.nome").as("nome"))
                    .add(Projections.property("s.sexo").as("sexo"))
                    .add(Projections.property("s.contato").as("contato")))
            .setResultTransformer(new AliasToBeanResultTransformer(Servidor.class));
    return cri.list();
}

From source file:br.com.suricattus.surispring.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a lista de entidades do tipo <T> ordenada pelas propriedades informadas, nesta ordem.
 * @param <T>/*from   w  w  w  . j  a  va2 s . c om*/
 * @param classe
 * @param sort
 * @param propertiesName
 * @return lista de entidades ordadenadas
 */
@SuppressWarnings("unchecked")
public <T> List<T> retrieveAll(Class<T> classe, SearchSort sort, String... propertiesName) {
    Criteria criteria = getSession().createCriteria(classe);
    if (SearchSort.ASCENDING.equals(sort)) {
        for (String property : propertiesName)
            criteria.addOrder(Order.asc(property));
    } else if (SearchSort.DESCENDING.equals(sort)) {
        for (String property : propertiesName)
            criteria.addOrder(Order.desc(property));
    }
    return criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}

From source file:br.com.tcc.service.persistence.CriteriaHelper.java

/**
 * Adiciona na {@link Criteria} vrios critrios de ordenao.
 * Sempre ser adicionado uma ordenao pelo id da Entity
 * como ltimo critrio de ordenao das entidades.
 *
 * @param criteria/*w  w  w  . j ava  2 s.c  o  m*/
 *          a instncia de {@link Criteria} na qual se
 *          adicionaro critrios de ordenao.
 * @param orderByList
 *          a lista de critrios de ordenao a adicionar.
 */
public static void addOrderCriteria(final Criteria criteria, final OrderBy... orderByList) {
    if (orderByList != null) {
        for (OrderBy orderBy : orderByList) {
            criteria.addOrder(CriteriaHelper.convertOrderByToOrder(orderBy));
        }
    }
    //sempre adiciona o id como ultima ordenacao
    //default do orderBy vazio  id
    criteria.addOrder(convertOrderByToOrder(new OrderBy()));
}

From source file:br.com.web2.ew.controladores.OperacaoJpaController.java

public List<Operacao> pesquisarOperacao(FiltroBuscaOperacao filtro) {
    EntityManager em = getEntityManager();
    Session s = em.unwrap(Session.class);
    try {/*w w w  .j ava  2s  .co m*/
        Criteria c = s.createCriteria(Operacao.class, "operacao");
        for (Criterion crit : filtro.getAllRestrictions()) {
            c.add(crit);
        }
        c.addOrder(Order.asc("operacao.dataOperacao"));
        return c.list();
    } finally {
        em.close();
    }
}

From source file:br.com.webbudget.domain.model.repository.entries.CardInvoiceRepository.java

License:Open Source License

/**
 *
 * @param card/*from w ww .  j a v a 2  s . c  o  m*/
 * @param pageRequest
 * @return
 */
@Override
public Page<CardInvoice> listByCard(Card card, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    // filtra
    if (card != null) {
        criteria.createAlias("card", "ca");
        criteria.add(Restrictions.eq("ca.id", card.getId()));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.CardRepository.java

License:Open Source License

/**
 *
 * @param isBlocked//from w  ww  .  j  a v a  2s  . c  om
 * @return
 */
@Override
public List<Card> listDebit(Boolean isBlocked) {

    final Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    criteria.add(Restrictions.eq("cardType", CardType.DEBIT));

    criteria.addOrder(Order.desc("inclusion"));

    return criteria.list();
}

From source file:br.com.webbudget.domain.model.repository.entries.CardRepository.java

License:Open Source License

/**
 *
 * @param isBlocked/*from   w  w w  .  j  a  v  a2 s.co m*/
 * @return
 */
@Override
public List<Card> listCredit(Boolean isBlocked) {

    final Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    criteria.add(Restrictions.eq("cardType", CardType.CREDIT));

    criteria.addOrder(Order.desc("inclusion"));

    return criteria.list();
}