Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

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

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:br.com.magmadoctor.modelo.profissional.ProfissionalDAOHibernate.java

@Override
public Integer retornaUltimoCodigo() {
    int ultimo = 0;
    Criteria criteria = this.session.createCriteria(Profissional.class).setProjection(Projections.max("id"));
    if (criteria.uniqueResult() == null) {
        return ultimo;
    } else {/*from www  .  j av a2  s. co  m*/
        ultimo = (Integer) criteria.uniqueResult();
    }
    return ultimo;
}

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

@Override
public Cliente getPorCpf(String cpf) throws DAOException {
    Cliente c = null;/*from   w  w  w.ja va2  s  .  c  o  m*/
    try {
        manager = this.getEntityManager();
        Criteria criteria = this.getCriteria();
        criteria.add(Restrictions.eq("cpf", cpf));
        c = (Cliente) criteria.uniqueResult();
    } catch (Exception e) {
        manager.getTransaction().rollback();
        e.printStackTrace();
        throw new DAOException(e.getMessage());
    } finally {
        manager.close();
    }
    return c;
}

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

@Override
public Funcionario getByCpf(String cpf) throws DAOException {
    manager = getEntityManager();// ww w.  j a  v a 2s . com
    Funcionario funcionario = null;
    try {
        Criteria criteria = getCriteria();
        criteria.add(Restrictions.eq("cpf", cpf));
        funcionario = (Funcionario) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
        throw new DAOException(e.getMessage());
    } finally {
        manager.close();
    }
    return funcionario;
}

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

@Override
public T getById(Long id) throws DAOException {
    T c = null;//from w  w  w  .j  a  v a  2s.c o m
    manager = getEntityManager();
    try {
        Session session = (Session) manager.getDelegate();
        Criteria criteria = session.createCriteria(clazz);
        criteria.add(Restrictions.eq("id", id));
        c = (T) criteria.uniqueResult();
        session.close();
    } catch (Exception e) {
        manager.getTransaction().rollback();
        // Mudar para arquivos de Propriedades
        throw new DAOException("Erro ao buscar objeto no banco de dados.");
    }
    return c;
}

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

@Override
public double aputadoMes(Calendar dataInicial, Calendar dataFinal) throws DAOException {
    manager = getEntityManager();/* w ww.j a  v a  2  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.PlanoEstudoDAOImpl.java

License:Creative Commons License

@SuppressWarnings("unchecked")
public List<PlanoEstudo> utimoPlanoEstudos(int idAluno) {

    Criteria criteria = getSession().createCriteria(PlanoEstudo.class);
    criteria.createAlias("aluno", "aluno");

    Criteria criteria2 = getSession().createCriteria(PlanoEstudo.class);
    criteria2.createAlias("aluno", "aluno");

    criteria2.add(Restrictions.eq("aluno.idAluno", idAluno));
    criteria2.setProjection(Projections.max("dataInicio"));

    criteria.add(Restrictions.eq("aluno.idAluno", idAluno));
    criteria.add(Restrictions.eq("dataInicio", criteria2.uniqueResult()));

    List<PlanoEstudo> result = criteria.list();
    return result;
}

From source file:br.com.nfsconsultoria.nfsuporte.dao.GenericDAO.java

public Entidade buscar(Long codigo) {
    Session sessao = HibernateUtil.getFabricaDeSessoes().openSession();

    try {// w ww  . j a v a  2s .c  o m
        Criteria consulta = sessao.createCriteria(classe);
        consulta.add(Restrictions.idEq(codigo));
        Entidade resultado = (Entidade) consulta.uniqueResult();
        return resultado;
    } catch (RuntimeException erro) {
        throw erro;
    } finally {
        sessao.close();
    }
}

From source file:br.com.nfsconsultoria.radiologic.dao.GenericDAO.java

public Entidade buscarCodigo(Long codigo) {
    Session sessao = HBUtil.getSessionFactory().openSession();

    try {//ww  w. j  av  a  2s  .c o m
        Criteria consulta = sessao.createCriteria(classe);
        consulta.add(Restrictions.idEq(codigo));
        Entidade resultado = (Entidade) consulta.uniqueResult();
        return resultado;
    } catch (RuntimeException ex) {
        throw ex;
    } finally {
        sessao.close();
    }
}

From source file:br.com.nfsconsultoria.radiologic.dao.GenericDAO.java

public Entidade buscarNome(String nome) {
    Session sessao = HBUtil.getSessionFactory().openSession();

    try {/*  w ww  .  j a v  a 2s .c o m*/
        Criteria consulta = sessao.createCriteria(classe);
        consulta.add(Restrictions.eq("nome", nome));
        Entidade resultado = (Entidade) consulta.uniqueResult();
        return resultado;
    } catch (RuntimeException ex) {
        throw ex;
    } finally {
        sessao.close();
    }
}

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 2  s .co m
        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();
}