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.ufg.calendario.dao.RegionalDao.java

@Transactional(readOnly = true)
public List<Regional> listar() {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Regional.class);
    criteria.addOrder(Order.asc("nome"));
    return criteria.list();
}

From source file:br.ufg.calendario.dao.RegionalDao.java

@Transactional(readOnly = true)
public List<Regional> listar(int first, int pageSize, String sortField, String sortOrder,
        Map<String, Object> filters) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Regional.class);
    if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
        if (sortOrder.equals("ASCENDING")) {
            criteria.addOrder(Order.asc(sortField));
        }//from www.  j ava 2  s.  c  om
        if (sortOrder.equals("DESCENDING")) {
            criteria.addOrder(Order.desc(sortField));
        }
    }
    if (filters != null && !filters.isEmpty()) {
        for (Map.Entry<String, Object> filter : filters.entrySet()) {
            criteria.add(Restrictions.eq(filter.getKey(), filter.getValue()));
        }
    }
    return criteria.list();
}

From source file:br.ufg.calendario.dao.UsuarioDao.java

@Transactional(readOnly = true)
public List<Usuario> listar(int first, int pageSize, String sortField, String sortOrder,
        Map<String, Object> filters) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Usuario.class);
    criteria.setFirstResult(first);/*from w w  w . j  a  v a 2  s  .c  om*/
    criteria.setMaxResults(pageSize);
    if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
        if (sortOrder.equals("ASCENDING")) {
            criteria.addOrder(Order.asc(sortField));
        }
        if (sortOrder.equals("DESCENDING")) {
            criteria.addOrder(Order.desc(sortField));
        }
    } else {
        criteria.addOrder(Order.asc("id"));
    }
    if (filters != null && !filters.isEmpty()) {
        for (String key : filters.keySet()) {
            if (key.equals("termo")) {
                criteria.add(Restrictions.or(
                        Restrictions.like("nome", filters.get(key).toString(), MatchMode.ANYWHERE).ignoreCase(),
                        Restrictions.like("login", filters.get(key).toString(), MatchMode.ANYWHERE)
                                .ignoreCase()));
            }

            if (key.equals("perfil")) {
                PerfilEnum p = (PerfilEnum) filters.get(key);
                criteria.add(Restrictions.eq("perfil", p));
            }

        }
    }
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<Usuario> resultado = criteria.list();
    for (Usuario u : resultado) {
        Hibernate.initialize(u.getPerfil());
    }
    return resultado;
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 * find by discente nome// w  w w .j a  v  a 2 s . c o  m
 *
 * @param termo
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(String termo, String sortField, String sortOrder) {
    try {
        List<Requerimento> requerimentos;
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .createAlias("discente", "d").add(Restrictions.or(Restrictions.eq("d.matricula", termo),
                        Restrictions.like("d.nome", termo, MatchMode.ANYWHERE).ignoreCase()));

        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 *
 * @param tipoRequerimento/*  w ww . j av a 2  s  .  co m*/
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(TipoRequerimentoEnum tipoRequerimento, String sortField, String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .add(Restrictions.eq("tipoRequerimento", tipoRequerimento));
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 *
 * @param login/*from  ww w. j a v a 2  s .c om*/
 * @param tipoRequerimento
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(String login, TipoRequerimentoEnum tipoRequerimento, String sortField,
        String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .createAlias("discente", "d").add(Restrictions.eq("d.login", login).ignoreCase())
                .add(Restrictions.eq("tipoRequerimento", tipoRequerimento));
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 * find by dataCriacao interval/*  w  w  w . j a  v a  2  s  . c  o  m*/
 *
 * @param dateA
 * @param dateB
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(Date dateA, Date dateB, String sortField, String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .add(Restrictions.and(Restrictions.between("dataCriacao", dateA, dateB)));
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 *
 * @param login//from w  w w.ja  va 2  s  . c o m
 * @param dateA
 * @param dateB
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(String login, Date dateA, Date dateB, String sortField, String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .createAlias("discente", "d").add(Restrictions.eq("d.login", login).ignoreCase())
                .add(Restrictions.and(Restrictions.between("dataCriacao", dateA, dateB)));
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 *
 * @param first/*from w  ww .java  2 s.  com*/
 * @param pageSize
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(int first, int pageSize, String sortField, String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .setFirstResult(first).setMaxResults(pageSize);
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 *
 * @param login//from   w  ww  .  j  a v a  2  s. c om
 * @param first
 * @param pageSize
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(String login, int first, int pageSize, String sortField, String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .createAlias("discente", "d").add(Restrictions.eq("d.login", login).ignoreCase())
                .setFirstResult(first).setMaxResults(pageSize);
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}