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 br.com.livraria.dao; import br.com.livraria.domain.Item; import br.com.livraria.util.HibernateUtil; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author Paulinho */ public class ItemDAO { public void salvar(Item item) { Session sessao = HibernateUtil.getSessionFactory().openSession(); Transaction transacao = null; try { transacao = sessao.beginTransaction(); sessao.save(item); transacao.commit(); } catch (RuntimeException ex) { if (transacao != null) { transacao.rollback(); } throw ex; } finally { sessao.close(); } } public List<Item> listar() { Session sessao = HibernateUtil.getSessionFactory().openSession(); List<Item> itens = null; try { Query consulta = sessao.getNamedQuery("Item.listar"); itens = consulta.list(); } catch (RuntimeException ex) { throw ex; } finally { sessao.close(); } return itens; } public Item buscarPorCodigo(Long codigo) { Session sessao = HibernateUtil.getSessionFactory().openSession(); Item item = null; try { Query consulta = sessao.getNamedQuery("Item.buscarPorCodigo"); consulta.setLong("codigo", codigo); item = (Item) consulta.uniqueResult(); } catch (RuntimeException ex) { throw ex; } finally { sessao.close(); } return item; } public void excluir(Item item) { Session sessao = HibernateUtil.getSessionFactory().openSession(); Transaction transacao = null; try { transacao = sessao.beginTransaction(); sessao.delete(item); transacao.commit(); } catch (RuntimeException ex) { if (transacao != null) { transacao.rollback(); } throw ex; } finally { sessao.close(); } } public void editar(Item item) { Session sessao = HibernateUtil.getSessionFactory().openSession(); Transaction transacao = null; try { transacao = sessao.beginTransaction(); sessao.update(item); transacao.commit(); } catch (RuntimeException ex) { if (transacao != null) { transacao.rollback(); } throw ex; } finally { sessao.close(); } } }