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.prototipo.dao.UsuarioDao.java

public Usuario validarLogin2(Usuario u) {
    if (sessao.isConnected()) {
        sessao.close();//from w w w  .j  a  v  a 2s  .  com
    }
    sessao = HibernateUtil.getSessionFactory().openSession();
    Criteria cri = sessao.createCriteria(Usuario.class);
    Criterion _login = Restrictions.eq("login", u.getLogin());
    cri.add(_login);
    Criterion _senha = Restrictions.eq("senha", u.getSenha());
    cri.add(_senha);
    return (Usuario) cri.uniqueResult();
}

From source file:br.com.railsos.os.dao.GenericDAO.java

/**
 * Mtodo Buscar Busca pelo id a variavel
 *
 * @author Rafael/*  w w  w.ja v  a 2 s. co m*/
 */
@SuppressWarnings("unchecked")
public Entidade buscar(int id) {
    Session sessao = HibernateUtil.getSessionFactory().openSession();
    try {
        Criteria consulta = sessao.createCriteria(classe);
        consulta.add(Restrictions.idEq(id));
        Entidade resultado = (Entidade) consulta.uniqueResult();
        return resultado;
    } catch (RuntimeException erro) {
        throw erro;
    } finally {
        sessao.close();
    }
}

From source file:br.com.rhmanager.daoImpl.AGBancariaDAOImpl.java

@Override
public AGBancaria getAGBByName(String nome) {
    Session session = null;/*  w  w  w . ja v a  2 s  .co  m*/
    try {
        session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(AGBancaria.class);
        criteria.add(Restrictions.eq("nomeAGBancaria", nome));
        return (AGBancaria) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
        return null;
    } finally {
        if (!session.isConnected()) {
            session.close();
        }
    }
}

From source file:br.com.rhmanager.daoImpl.FuncionarioDAOImpl.java

@Override
public Funcionario getFuncionarioByCpf(String cpf) {
    Session session = null;/*from w  w  w  .ja v  a 2s . c o  m*/
    try {
        session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(Funcionario.class);
        criteria.add(Restrictions.eq("cpf", cpf));
        return (Funcionario) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }
}

From source file:br.com.rhmanager.daoImpl.FuncionarioDAOImpl.java

@Override
public Funcionario getFuncionarioById(Long id) {
    Session session = null;//from  w w  w .j ava2s. c  om
    try {
        session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(Funcionario.class);
        criteria.add(Restrictions.eq("idFuncionario", id));
        return (Funcionario) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }
}

From source file:br.com.rhmanager.daoImpl.UsuarioDAOImpl.java

@Override
public Usuario verificarLogin(String email, String senha) {
    Session session = null;//from  w ww .  jav  a 2 s .com
    try {
        session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(Usuario.class);
        criteria.add(Restrictions.eq("email", email)).add(Restrictions.eq("password", senha));
        return (Usuario) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (!session.isConnected()) {
            session.close();
        }
    }
}

From source file:br.com.senai.Controle.FuncionarioDAO.java

public void CalculoHoraExtra(Long HoraDia, int id) {

    sessao = HibernateUtil.getSessionFactory().openSession();
    sessao.beginTransaction();//  w  w  w.  j ava2 s  .  c o  m
    //Cria criterios ou filtros ou condies de seleo.
    Criteria criteria = sessao.createCriteria(Funcionario.class);
    criteria.add(Restrictions.eq("id", id));

    Funcionario funcionario = (Funcionario) criteria.uniqueResult();

    long HoraExtra = HoraDia - 540;

    if (HoraExtra > 0) {

        funcionario.setHrAcumulada(funcionario.getHrAcumulada() + HoraExtra);

    } else if (HoraExtra < 0) {

        funcionario.setHrAcumulada(funcionario.getHrAcumulada() + HoraExtra);

    } else {

        funcionario.setHrAcumulada(funcionario.getHrAcumulada());

    }

    alterar(funcionario);
}

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