Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

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

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

From source file:br.com.itw.commons.persistence.CoreRepository.java

License:Apache License

public Page<T> search(T entity, Pageable pageable) {
    if (pageable == null) {
        pageable = new PageRequest(0, 10);
    }/* w w  w  .  j  a  va  2  s. c om*/

    Session session = (Session) entityManager.getDelegate();
    Criteria criteria = session.createCriteria(entity.getClass());
    // Prepare Example
    Example example = Example.create(entity);
    criteria.add(example.enableLike(MatchMode.ANYWHERE).ignoreCase());

    // Count
    Long totalItems = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

    // Pageable result Result
    criteria.setProjection(null).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    List<T> result = criteria.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize())
            .setMaxResults(pageable.getPageSize()).list();
    return (Page<T>) new PageImpl<T>(result, pageable, totalItems);
}

From source file:br.com.itw.commons.persistence.PageableHelper.java

License:Apache License

public static Page<?> getPage(Criteria criteria, Pageable pageable) {
    // Count//from  w  ww.ja  v a2s  .co  m
    Long totalItems = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

    // Pageable result Result
    criteria.setProjection(null).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    if (pageable.getSort() != null) {
        Iterator<Sort.Order> orders = pageable.getSort().iterator();
        while (orders.hasNext()) {
            Sort.Order order = orders.next();
            if (order.getDirection() == null || Sort.Direction.ASC.equals(order.getDirection())) {
                criteria.addOrder(Order.asc(order.getProperty()));
            } else {
                criteria.addOrder(Order.desc(order.getProperty()));
            }
        }
    }
    List<?> result = criteria.setFirstResult(pageable.getPageNumber() * pageable.getPageSize())
            .setMaxResults(pageable.getPageSize()).list();
    return (Page<?>) new PageImpl<>(result, pageable, totalItems);
}

From source file:br.com.map.marcelo.dao.VendaDaoImp.java

@Override
public double aputadoMes(Calendar dataInicial, Calendar dataFinal) throws DAOException {
    manager = getEntityManager();//from  w  w  w. ja v a2 s  .com
    double valor = 0;
    try {
        Criteria criteria = getCriteria();
        criteria.add(Restrictions.between("dataVenda", dataInicial, dataFinal));
        criteria.setProjection(Projections.sum("precoTotal"));
        if (criteria.uniqueResult() != null) {
            valor = (double) criteria.uniqueResult();
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new DAOException(e.getMessage());
    } finally {
        manager.close();
    }
    return valor;
}

From source file:br.com.muranodesign.dao.impl.AlunoDAOImpl.java

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<Aluno> ListarNomeId() {
    Criteria criteria = getSession().createCriteria(Aluno.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("idAluno"), "idAluno");
    projList.add(Projections.property("nome"), "nome");

    criteria.setProjection(projList);

    criteria.setResultTransformer(Transformers.aliasToBean(Aluno.class));
    List<Aluno> results = criteria.list();

    return results;
}

From source file:br.com.muranodesign.dao.impl.ChamadaDAOImpl.java

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<Chamada> dataPresencaAtual(int id, Date data) {
    Criteria criteria = getSession().createCriteria(Chamada.class);

    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("presenca"), "presenca");

    criteria.createAlias("aluno", "aluno");
    criteria.add(Restrictions.eq("aluno.idAluno", id));
    criteria.add(Restrictions.eq("data", data));

    criteria.setProjection(projList).setCacheable(true);
    criteria.setResultTransformer(Transformers.aliasToBean(Chamada.class));

    List<Chamada> result = criteria.list();

    return result;
}

From source file:br.com.muranodesign.dao.impl.ChamadaDAOImpl.java

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<Compensacao> compensacaoAluno(int idAluno) {
    Criteria criteria = getSession().createCriteria(Chamada.class);
    criteria.createAlias("aluno", "aluno");
    criteria.add(Restrictions.eq("aluno.idAluno", idAluno));
    criteria.add(Restrictions.isNotNull("compensacao"));
    criteria.setProjection(Projections.distinct(Projections.property("compensacao")));
    return criteria.list();
}

From source file:br.com.muranodesign.dao.impl.ObjetivoDAOImpl.java

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<Objetivo> listAllTeste() {

    Criteria criteria = getSession().createCriteria(Objetivo.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("descricao"), "descricao");
    criteria.createAlias("roteiro", "roteiro");
    projList.add(Projections.property("roteiro.idroteiro"));
    criteria.setProjection(projList).setCacheable(true);
    criteria.setResultTransformer(Transformers.aliasToBean(Objetivo.class));

    List<Objetivo> results = criteria.list();

    return results;

}

From source file:br.com.muranodesign.dao.impl.ObjetivoDAOImpl.java

License:Creative Commons License

public long listarRoteiroTotal(int id) {
    Criteria criteria = getSession().createCriteria(Objetivo.class);
    criteria.createAlias("roteiro", "roteiro");
    criteria.add(Restrictions.eq("roteiro.idroteiro", id));
    criteria.add(Restrictions.eq("roteiro.ativo", 1));

    criteria.setProjection(Projections.count("ativo"));

    long result = (Long) criteria.list().get(0);
    return result;
}

From source file:br.com.muranodesign.dao.impl.ObjetivoDAOImpl.java

License:Creative Commons License

public long listarGrafico(int id) {
    Criteria criteria = getSession().createCriteria(Objetivo.class);
    criteria.add(Restrictions.eq("ativo", 1));
    criteria.createAlias("roteiro", "roteiro");
    criteria.add(Restrictions.eq("roteiro.ativo", 1));
    criteria.add(Restrictions.eq("roteiro.anoEstudo.idanoEstudo", id));
    criteria.setProjection(Projections.count("ativo"));

    long result = (Long) criteria.list().get(0);
    return result;
}

From source file:br.com.muranodesign.dao.impl.PlanejamentoRoteiroDAOImpl.java

License:Creative Commons License

public long listarAlunoTotal(int id) {
    Criteria criteria = getSession().createCriteria(PlanejamentoRoteiro.class);
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_YEAR, 1);
    criteria.add(Restrictions.ge("dataStatusPlanejado", cal.getTime()));
    criteria.add(Restrictions.eq("idAluno", id));
    criteria.add(Restrictions.isNotNull("objetivo"));
    criteria.setProjection(Projections.count("idAluno"));

    long result = (Long) criteria.list().get(0);
    return result;
}