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