Beispiel #1
0
  @Override
  public Object getAsObject(FacesContext facesContext, UIComponent component, String string) {
    if (string == null || string.isEmpty()) return null;

    try {
      session = HibernateUtil.getSessionFactory().openSession();
      String[] values = string.split("-");
      return session.get(Class.forName(values[0]), Long.valueOf(values[1]));
    } catch (Exception e) {
      logger.error("Erro ao converter String em entidade", e);
      return null;
    } finally {
      session.close();
    }
  }
  public void excluir(Linha lin) throws Exception, SQLException {
    Session sessao = null;
    try {
      sessao = dao.HibernateUtil.getSessionFactory().openSession();
      sessao.beginTransaction();

      sessao.delete(lin);

      sessao.getTransaction().commit();
    } catch (HibernateException he) {
      sessao.getTransaction().rollback();
    } finally {
      if (sessao != null) {
        sessao.close();
      }
    }
  }
  public List listarLinhas() throws Exception, SQLException {
    Session sessao = null;
    List<Linha> lista = null;
    try {
      sessao = dao.HibernateUtil.getSessionFactory().openSession();
      sessao.beginTransaction();

      // Usando HQL
      Query consulta = sessao.createQuery("from Linha");
      lista = consulta.list();

      sessao.getTransaction().commit();

    } catch (HibernateException he) {
      sessao.getTransaction().rollback();
    } finally {
      if (sessao != null) {
        sessao.close();
      }
      return lista;
    }
  }
  public List pesquisar(String pesqNome) throws Exception, SQLException {
    Session sessao = null;
    List lista = null;
    try {
      sessao = dao.HibernateUtil.getSessionFactory().openSession();
      sessao.beginTransaction();

      // Usando HQL
      Query consulta =
          sessao.createQuery("from Linha lin where lin.nome LIKE '%" + pesqNome + "%' ");
      lista = consulta.list();

      sessao.getTransaction().commit();

    } catch (HibernateException he) {
      sessao.getTransaction().rollback();
    } finally {
      if (sessao != null) {
        sessao.close();
      }
      return lista;
    }
  }