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.sgejs.dao.DaoArquivo.java

public List getCursosDoAno(int ano) throws HibernateException {
    Criteria cri = sessao.createCriteria(classe);
    cri.add(Restrictions.eq("ano", ano));
    cri.setProjection(Projections.distinct(Projections.property("curso")));
    return cri.list();
}

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

public List getTipoDeArquivoDoAno(int ano, Curso curso) throws HibernateException {
    Criteria cri = sessao.createCriteria(classe);
    cri.add(Restrictions.eq("ano", ano)).add(Restrictions.eq("curso", curso));
    cri.setProjection(Projections.distinct(Projections.property("tipo")));
    return cri.list();
}

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

public List filtrar(Arquivo arq) throws HibernateException {

    Criteria cri = sessao.createCriteria(classe, "p");
    /*  Example exemplo = Example.create(arq);
    cri.add(exemplo);*//*  w ww. jav a  2 s . co m*/

    if (arq.getAno() > 0) {
        cri.add(Restrictions.eq("ano", arq.getAno()));
    }
    if (arq.getTipo() != null) {
        cri.add(Restrictions.eq("tipo", arq.getTipo()));
    }
    if (arq.getCurso() != null) {
        cri.add(Restrictions.eq("curso", arq.getCurso()));
    }

    cri.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.DaoArquivo.java

public List<Arquivo> getArquivosDoAluno(long id) throws HibernateException {
    Criteria criArquivo = sessao.createCriteria(classe, "p");
    criArquivo.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));
    criArquivo.createCriteria("alunos").add(Restrictions.eq("id", id));
    return criArquivo.list();
}

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

public Servidor getEscolaridade(Long id) {
    Criteria cri = sessao.createCriteria(classe, "s");
    cri.add(Restrictions.eq("s.id", id));
    cri.setProjection(Projections.projectionList().add(Projections.property("s.nome").as("nome"))
            .add(Projections.property("s.instrucao").as("instrucao"))
            .add(Projections.property("s.posGraduacao").as("posGraduacao")))
            .setResultTransformer(new AliasToBeanResultTransformer(Servidor.class));
    Servidor u = (Servidor) cri.uniqueResult();

    SQLQuery q1 = sessao.createSQLQuery("select * from graduacao where servidor_id=" + id)
            .addEntity(Graduacao.class);
    List list = q1.list();//  www .j  av a2  s .  c  om
    System.out.println("Graduacoes: " + list);
    u.setGraduacoes(list);
    return u;
}

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

public Servidor getInformacoesBasicas(long id) {
    Criteria cri = sessao.createCriteria(classe, "s");
    cri.add(Restrictions.eq("s.id", id));
    cri.setProjection(Projections.projectionList().add(Projections.property("s.nome").as("nome"))
            .add(Projections.property("s.nascimento").as("nascimento"))
            .add(Projections.property("s.cidadeNascimento").as("cidadeNascimento"))
            .add(Projections.property("s.ufNascimento").as("ufNascimento"))
            .add(Projections.property("s.sexo").as("sexo")).add(Projections.property("s.raca").as("raca"))
            .add(Projections.property("s.grupoSanguineo").as("grupoSanguineo"))
            .add(Projections.property("s.estadoCivil").as("estadoCivil"))
            .add(Projections.property("s.filiacao").as("filiacao")))
            .setResultTransformer(new AliasToBeanResultTransformer(Servidor.class));
    return (Servidor) cri.uniqueResult();
}

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

public Servidor getDocumentos(long id) throws HibernateException {
    Criteria cri = sessao.createCriteria(classe, "s");
    cri.add(Restrictions.eq("s.id", id));
    cri.setProjection(Projections.projectionList().add(Projections.property("s.nome").as("nome"))
            .add(Projections.property("s.documento").as("documento")))
            .setResultTransformer(new AliasToBeanResultTransformer(classe));
    return (Servidor) cri.uniqueResult();
}

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

/**
 * <p>/*from   w ww . ja va 2 s. c  o m*/
 * <b>ATENO:</b> Este mtodo <b>altera permanentemente</b> o objeto {@link Criteria} recebido como parmetro. Qualquer {@link Projection} ou {@link ResultTransformer} configurado no criteria
 * <b>ser substitudo</b>.
 * </p>
 * <p>
 * Use o parmetro <code>distinct</code> para definir se o {@link ResultTransformer} deve ser
 * {@link Criteria.DISTINCT_ROOT_ENTITY} ou apenas {@link Criteria.ROOT_ENTITY}
 * e se deve ser usado <code>distinct count(*)</code> ou apenas <code>count(*)</code>.
 * </p>
 * @param criteria O Criteria a ser utilizado
 * @param pagination O objeto com a paginao desejada.
 * @param distinct Se a consulta deve usar <code>SQL DISTINCT</code>.
 * @return Um novo objeto de paginao referente  consulta atual.
 */
private static Pagination getTotalItemsForCriteria(Criteria criteria, Pagination pagination, boolean distinct) {

    Projection count = Projections.rowCount();
    if (distinct) {
        count = Projections.distinct(count);
    }
    int totalItems = (Integer) criteria.setProjection(count).uniqueResult();
    criteria.setProjection(null)
            .setResultTransformer(distinct ? Criteria.DISTINCT_ROOT_ENTITY : Criteria.ROOT_ENTITY);
    return new Pagination(pagination.getPageSize(), pagination.getCurrentPage(), totalItems,
            pagination.getOrdering());

}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

/**
 * {@inheritDoc}// w w w .  j  a  v  a2  s . c  o m
 */
@Override
public <ID extends Serializable, T extends Persistable<ID>, N extends Number> N countByCriteria(
        Class<T> entityClazz, Class<N> resultClazz, ResultTransformer resultTransformer,
        List<Criterion> criterions) {
    Criteria criteria = getSession().createCriteria(entityClazz);
    criteria.setProjection(Projections.rowCount());
    if (criterions != null) {
        for (Criterion c : criterions) {
            criteria.add(c);
        }
    }
    return (N) criteria.setResultTransformer(resultTransformer).uniqueResult();
}

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

License:Open Source License

/**
 *
 * @param card/*from w  ww  . j a va2 s . co  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);
}