Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

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

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<ProfessorFuncionario> listarIdNome() {
    Criteria criteria = getSession().createCriteria(ProfessorFuncionario.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("idprofessorFuncionario"), "idprofessorFuncionario");
    projList.add(Projections.property("nome"), "nome");
    criteria.add(Restrictions.eq("ativo", "s"));
    criteria.setProjection(projList);/*from w ww.  j a  v a 2  s .  c o  m*/
    criteria.setResultTransformer(Transformers.aliasToBean(ProfessorFuncionario.class));
    List<ProfessorFuncionario> results = criteria.list();

    return results;
}

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

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<Roteiro> listRoteiroRange(int primeiro, int ultimo) {
    Criteria criteria = getSession().createCriteria(Roteiro.class);
    ProjectionList projList = Projections.projectionList();

    criteria.setFirstResult(primeiro);//from w w  w  .  j  a va  2s.  co  m
    criteria.setMaxResults(ultimo);
    projList.add(Projections.property("idroteiro"), "idroteiro");
    projList.add(Projections.property("nome"), "nome");

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

    criteria.addOrder(Order.asc("nome"));
    List<Roteiro> result = criteria.list();

    return result;
}

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

License:Creative Commons License

@Override
@SuppressWarnings("unchecked")
public List<Roteiro> listarAnoEstudoLazy(int anoEstudo) {
    Criteria criteria = getSession().createCriteria(Roteiro.class);
    ProjectionList projList = Projections.projectionList();

    criteria.createAlias("anoEstudo", "anoEstudo");
    criteria.add(Restrictions.eq("anoEstudo.idanoEstudo", anoEstudo));
    criteria.add(Restrictions.eqOrIsNull("ativo", 1));
    projList.add(Projections.property("idroteiro"), "idroteiro");
    projList.add(Projections.property("nome"), "nome");
    criteria.setProjection(projList).setCacheable(true);
    criteria.setResultTransformer(Transformers.aliasToBean(Roteiro.class));
    List<Roteiro> result = criteria.list();

    return result;

}

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

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<Tutoria> listarDadosPertinentes() {

    Criteria criteria = getSession().createCriteria(Tutoria.class);

    ProjectionList projList = Projections.projectionList();

    projList.add(Projections.property("idtutoria"), "idtutoria");
    projList.add(Projections.property("tutoria"), "tutoria");

    criteria.setProjection(projList).setCacheable(true);

    criteria.setResultTransformer(Transformers.aliasToBean(Tutoria.class));

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

    return results;
}

From source file:br.com.pfood.dao.imp.GenericDAOImp.java

@Override
public <T extends Object> List<T> getAllLimit(Class<T> classe, Order order, Integer inicio, Integer fim) {
    Criteria c = session.createCriteria(classe);
    if (inicio != null) {
        c.setFirstResult(inicio);/*from  w  ww.  j  a  v  a2s. c  o  m*/
    }
    if (fim != null) {
        c.setMaxResults(fim);
    }
    if (order != null) {
        c.addOrder(order);
    }

    c.setResultTransformer(c.DISTINCT_ROOT_ENTITY);

    return (List<T>) c.list();
}

From source file:br.com.pfood.dao.imp.GenericDAOImp.java

/**
 * No  obrigatrio passar o ORDER, caso receba null vai buscar pelo padro
 * do banco de dados/*from w  w  w  .  j  a v a 2  s.c o  m*/
 *
 * @param <T>
 * @param obj
 * @param order
 * @return List<T> podendo ser null
 */
@Override
public <T extends Object> List<T> getPorAtributosIguais(T obj, Order order) {
    Criteria c = session.createCriteria(obj.getClass());

    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field f : fields) {
        f.setAccessible(true);
        if ((f.isAnnotationPresent(Column.class) || f.isAnnotationPresent(ManyToOne.class)
                || f.isAnnotationPresent(OneToOne.class)) && !f.getType().isPrimitive()) {
            try {
                Object valor = f.get(obj);
                if (valor != null) {
                    c.add(Restrictions.eq(f.getName(), valor));
                }
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    c.setResultTransformer(c.DISTINCT_ROOT_ENTITY);
    if (order != null) {
        c.addOrder(order);
    }
    return (ArrayList<T>) c.list();
}

From source file:br.com.pfood.dao.imp.GenericDAOImp.java

public <T extends Object> T getPorAtributosUnicos(T obj) throws Exception {
    Criteria c = session.createCriteria(obj.getClass());
    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field f : fields) {
        f.setAccessible(true);/*w  ww .  j  a v  a 2s.com*/
        if ((f.isAnnotationPresent(Column.class) || f.isAnnotationPresent(ManyToOne.class)
                || f.isAnnotationPresent(OneToOne.class)) && !f.getType().isPrimitive()) {
            try {
                Object valor = f.get(obj);
                if (valor != null) {
                    c.add(Restrictions.eq(f.getName(), valor));
                }
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    c.setResultTransformer(c.DISTINCT_ROOT_ENTITY);
    if (!c.list().isEmpty() && c.list().size() > 1) {
        throw new Exception("Mais de um resultado encontrado para o objeto");
    }
    if (c.list().isEmpty()) {
        return null;
    }
    return (T) c.uniqueResult();
}

From source file:br.com.pfood.dao.imp.GenericDAOImp.java

/**
 * Metodo criado inicialmente para busca de item_tabela_preco. Voce deve
 * preencher o objeto com os atributos a ser buscado. Vai executar uma busca
 * apartir de todos os atributos COM VALOR, onde on valor for igual ao
 * informado ou entao esta null no banco. A busca  apenas pelos atributos
 * que possuem valor, nao em todo os atributos do objeto.
 *
 * @param <T>//from  w w  w .  j a v a 2 s.  c  o m
 * @param obj
 * @param order
 * @return
 * @throws Exception
 */
@Override
public <T> List<T> getPorAtributosIguaisOuNull(T obj, Order order) throws Exception {
    Criteria c = session.createCriteria(obj.getClass());

    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field f : fields) {
        f.setAccessible(true);
        if ((f.isAnnotationPresent(Column.class) || f.isAnnotationPresent(ManyToOne.class)
                || f.isAnnotationPresent(OneToOne.class)) && !f.getType().isPrimitive()) {
            try {
                Object valor = f.get(obj);
                if (valor != null) {
                    c.add(Restrictions.or(Restrictions.eq(f.getName(), valor),
                            Restrictions.isNull(f.getName())));
                }
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    c.setResultTransformer(c.DISTINCT_ROOT_ENTITY);
    if (order != null) {
        c.addOrder(order);
    }
    return (ArrayList<T>) c.list();
}

From source file:br.com.pfood.dao.imp.GenericDAOImp.java

@Override
public int buscaProximoCodigo(Class classe) throws Exception {

    Field[] fields = classe.getDeclaredFields();
    Criteria c = getSession().createCriteria(classe);
    for (Field f : fields) {
        f.setAccessible(true);/*from ww  w .ja  v  a 2 s . c  o m*/
        if (f.isAnnotationPresent(Id.class)) {
            c.setProjection(Projections.max(f.getName()));
            break;
        }
    }
    c.setResultTransformer(c.DISTINCT_ROOT_ENTITY);
    Integer maximo = (Integer) c.uniqueResult();

    if (maximo == null || maximo == 0) {
        return 1;
    } else {
        return maximo + 1;
    }
}

From source file:br.com.reindex.suri.framework.dao.DaoSupport.java

License:Open Source License

/**
 * Metodo Responsavel por recuperar todos os objeto de uma tabela da base de dados de forma ordenada
 * // ww  w. j  ava  2 s .c o  m
 * @param sort asc - ascending or desc - descending
 * @param propertyName property to be ordered
 * @return todos os objetos da classe
 */
public List<T> retrieveAll(SearchSort sort, String... propertiesName) {
    Criteria criteria = getSession().createCriteria(tipo);
    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();
}