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.StatusDao; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import modelo.StaStatus; import org.hibernate.Session; import org.hibernate.Transaction; import util.HibernateUtil; /** * * @author Chris Allen */ public class StatusDaoImp implements StatusDao { private String msg; @Override public void guardar(StaStatus status) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.getTransaction().begin(); session.save(status); session.getTransaction().commit(); msg = "Status guardado com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { msg = "Erro ao guardar status!"; 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 apagar(StaStatus status) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.getTransaction().begin(); session.delete(status); session.getTransaction().commit(); msg = "Status apagado com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { msg = "Erro ao apagar status!"; 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(StaStatus status) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { Transaction tx = session.beginTransaction(); session.merge(status); tx.commit(); msg = "Status alterado com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { msg = "Erro ao alterar status!"; 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 StaStatus buscaPorId(int id) { Session session = HibernateUtil.getSessionFactory().openSession(); return (StaStatus) session.load(StaStatus.class, id); } @Override public List<StaStatus> buscarTodos() { Session session = HibernateUtil.getSessionFactory().openSession(); try { return session.createQuery("from StaStatus").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<StaStatus> selectItems() { Session session = HibernateUtil.getSessionFactory().openSession(); return session.createQuery("from StaStatus").list(); } }