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.UtenteDao; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import modelo.UteUtente; import org.hibernate.Session; import org.hibernate.Transaction; import org.primefaces.context.RequestContext; import util.HibernateUtil; /** * * @author Chris Allen */ public class UtenteDaoImp implements UtenteDao { private String msg; UteUtente utente = new UteUtente(); @Override public void guardar(UteUtente utente) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.getTransaction().begin(); session.save(utente); session.getTransaction().commit(); msg = "Utente 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 utente!"; 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(UteUtente utente) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.getTransaction().begin(); session.delete(utente); session.getTransaction().commit(); msg = "Utente apagada com sucesso!"; FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); FacesContext.getCurrentInstance().addMessage(null, message); } catch (Exception e) { msg = "Erro ao apagado utente!"; 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(UteUtente utente) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { Transaction tx = session.beginTransaction(); session.merge(utente); tx.commit(); msg = "Utente 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 utente!"; 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').show()"); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } @Override public void alterarDeAddArquivo(UteUtente utente) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { Transaction tx = session.beginTransaction(); session.merge(utente); tx.commit(); } catch (Exception e) { System.out.println("Erro ao Incluir o Item: " + e.getMessage()); session.getTransaction().rollback(); RequestContext.getCurrentInstance().execute("PF('dlgEditar').show()"); } finally { try { session.isOpen(); session.close(); System.out.println("Sesso fechada"); } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } @Override public void alterarActualizar(UteUtente utente) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try { Transaction tx = session.beginTransaction(); session.merge(utente); 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 UteUtente buscaPorId(int id) { Session session = HibernateUtil.getSessionFactory().openSession(); return (UteUtente) session.load(UteUtente.class, id); } @Override public List<UteUtente> buscarTodos() { Session session = HibernateUtil.getSessionFactory().openSession(); try { return session.createQuery("from UteUtente").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<UteUtente> selectItems() { Session session = HibernateUtil.getSessionFactory().openSession(); return session.createQuery("from UteUtente").list(); } }