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.progolden.dneutils.utils.DNEUtils.java

License:Apache License

private Long countByCEP(String cep, Class<? extends EntityIF> entityClass) {
    Criteria criteria;
    Long count;//  w ww . j  ava  2  s  . c  o  m
    LOGGER.debug("Contando ocorrncias do CEP " + cep + " na tabela " + entityClass.getCanonicalName());
    criteria = this.dao.newCriteria(entityClass);
    criteria.add(Restrictions.eq("cep", cep));
    criteria.setProjection(Projections.rowCount());
    count = this.dao.uniqueByCriteria(criteria, Long.class);
    LOGGER.debug("Contagem realizada: " + String.valueOf(count));
    return count;
}

From source file:br.com.prototipo.dao.CursoDao.java

public List<String> getListParaTurmas() {
    if (sessao.isConnected()) {
        sessao.close();//w w  w.ja va 2  s  .co  m
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Curso.class);
    Criteria cri2 = sessao.createCriteria(Curso.class);
    cri.addOrder(Order.asc("codigo"));
    cri2.addOrder(Order.asc("codigo"));

    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("codigo"));

    ProjectionList projection2 = Projections.projectionList();
    projection2.add(Projections.property("nome"));

    cri.setProjection(projection);
    cri2.setProjection(projection2);

    List<String> l1 = cri.list();
    List<String> l2 = cri2.list();
    List<String> l3 = new ArrayList<>();
    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();

    while (it1.hasNext()) {
        String aux1 = it1.next();
        String aux2 = it2.next();
        String aux3 = aux1 + " - " + aux2;
        l3.add(aux3);
    }
    return l3;
}

From source file:br.com.prototipo.dao.DisciplinaDao.java

public List<String> getListParaTurmas() {
    if (sessao.isConnected()) {
        sessao.close();/*from w w w .jav  a2 s  . c  o m*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Disciplina.class);
    cri.addOrder(Order.asc("codigo"));
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("codigo"));
    cri.setProjection(projection);
    listaParaTurmas = cri.list();
    return listaParaTurmas;
}

From source file:br.com.prototipo.dao.DisciplinaDao.java

public List<String> getListDisciplinasString() {
    if (sessao.isConnected()) {
        sessao.close();//from  w  w  w .  j  ava2s .c  om
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Disciplina.class);
    Criteria cri2 = sessao.createCriteria(Disciplina.class);
    cri.addOrder(Order.asc("codigo"));
    cri2.addOrder(Order.asc("codigo"));

    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("codigo"));

    ProjectionList projection2 = Projections.projectionList();
    projection2.add(Projections.property("nome"));

    cri.setProjection(projection);
    cri2.setProjection(projection2);

    List<String> l1 = cri.list();
    List<String> l2 = cri2.list();
    List<String> l3 = new ArrayList<>();
    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();

    while (it1.hasNext()) {
        String aux1 = it1.next();
        String aux2 = it2.next();
        String aux3 = aux1 + " - " + aux2;
        l3.add(aux3);
    }
    return l3;
}

From source file:br.com.prototipo.dao.HorarioDao.java

public List<String> getListParaDisciplinas(String dia) {
    if (sessao.isConnected()) {
        sessao.close();/*from  w  ww .ja v a 2s  . co  m*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Horario.class);
    Criterion _nome = Restrictions.like("nomeDia", dia, MatchMode.ANYWHERE);
    cri.add(_nome);
    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("aula"));
    cri.setProjection(projection);
    return cri.list();
}

From source file:br.com.prototipo.dao.ProfessorDao.java

public List<String> getListProfessoresString() {
    if (sessao.isConnected()) {
        sessao.close();/*  ww w  .j  a  v a 2s .c o m*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Professor.class);
    Criteria cri2 = sessao.createCriteria(Professor.class);
    cri.addOrder(Order.asc("registro"));
    cri2.addOrder(Order.asc("registro"));

    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("registro"));

    ProjectionList projection2 = Projections.projectionList();
    projection2.add(Projections.property("nome"));

    cri.setProjection(projection);
    cri2.setProjection(projection2);

    List<String> l1 = cri.list();
    List<String> l2 = cri2.list();
    List<String> l3 = new ArrayList<>();
    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();

    while (it1.hasNext()) {
        String aux1 = it1.next();
        String aux2 = it2.next();
        String aux3 = aux1 + " - " + aux2;
        l3.add(aux3);
    }

    return l3;
}

From source file:br.com.prototipo.dao.TurmaDao.java

public List<String> getListTurmasString() {
    if (sessao.isConnected()) {
        sessao.close();/*from   w ww. j a v a 2s.co m*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Turma.class);
    cri.addOrder(Order.asc("codigo"));

    Criteria cri2 = sessao.createCriteria(Turma.class);
    cri2.addOrder(Order.asc("codigo"));

    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("codigo"));
    cri.setProjection(projection);

    ProjectionList projection2 = Projections.projectionList();
    projection2.add(Projections.property("nomeDisciplina"));
    cri2.setProjection(projection2);

    List<String> l1 = cri.list();
    List<String> l2 = cri2.list();
    List<String> l3 = new ArrayList<>();
    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();
    while (it1.hasNext()) {
        String aux1 = it1.next();
        String aux2 = it2.next();
        String aux3 = aux1 + " - " + aux2;
        l3.add(aux3);
    }

    return l3;
}

From source file:br.com.sales.compra.DAO.DAO.java

public VO getId(VO vo) {
    Criteria criteria = s.createCriteria(classe);
    ProdutoVO id = (ProdutoVO) criteria.setProjection(Projections.id()).uniqueResult();
    return (VO) id;
}

From source file:br.com.sales.compra.DAO.DAO.java

/**
 * public void setById(int id) { nfc.setId(id); }
 *//*from  w ww  .java 2 s  .c  om*/
public int getMaxNotaId(VO vo) {
    Criteria criteria = s.createCriteria(classe);
    int maxId = (int) criteria.setProjection(Projections.max("id")).uniqueResult();
    return maxId;
}

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