Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package daoImp; import dao.ArquivoEmpresaDao; import dao.EmpresaDao; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import modelo.EmpEmpresa; import org.hibernate.Session; import org.hibernate.Transaction; import org.primefaces.context.RequestContext; import util.HibernateUtil; /** * * @author Chris Allen */ public class EmpresaDaoImp implements EmpresaDao { private String msg; @Override public void guardar(EmpEmpresa empresa) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.getTransaction().begin(); session.save(empresa); session.getTransaction().commit(); msg = "Empresa guardada com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); RequestContext.getCurrentInstance().execute("PF('dlgnovo').hide()"); } catch (Exception e) { msg = "Erro ao guardar empresa!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); System.out.println("Erro ao Incluir o Item: " + e.getMessage()); session.getTransaction().rollback(); RequestContext.getCurrentInstance().execute("PF('dlgnovo').show()"); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } @Override public void apagar(EmpEmpresa empresa) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.getTransaction().begin(); session.delete(empresa); session.getTransaction().commit(); msg = "Empresa apagada com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { msg = "Erro ao apagado empresa!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); System.out.println("Erro ao Incluir o Item: " + e.getMessage()); session.getTransaction().rollback(); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } @Override public void alterar(EmpEmpresa empresa) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { Transaction tx = session.beginTransaction(); session.merge(empresa); tx.commit(); msg = "Empresa alterada com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); RequestContext.getCurrentInstance().execute("PF('dlgEditar').hide()"); } catch (Exception e) { msg = "Erro ao alterar a empresa!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); System.out.println("Erro ao Incluir o Item: " + e.getMessage()); session.getTransaction().rollback(); RequestContext.getCurrentInstance().execute("PF('dlgEditar').hide()"); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } @Override public void alterarActulizacao(EmpEmpresa empresa) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { Transaction tx = session.beginTransaction(); session.merge(empresa); tx.commit(); } catch (Exception e) { System.out.println("Erro ao Incluir o Item: " + e.getMessage()); session.getTransaction().rollback(); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } @Override public EmpEmpresa buscaPorId(int id) { Session session = HibernateUtil.getSessionFactory().openSession(); return (EmpEmpresa) session.load(EmpEmpresa.class, id); } @Override public List<EmpEmpresa> buscarTodos() { Session session = HibernateUtil.getSessionFactory().openSession(); try { return session.createQuery("from EmpEmpresa").list(); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); session.beginTransaction().rollback(); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } return null; } @Override public List<EmpEmpresa> selectItems() { Session session = HibernateUtil.getSessionFactory().openSession(); return session.createQuery("from EmpEmpresa").list(); } }