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 com.hibernate.dao; import com.hibernate.entidades.Direccion; import com.hibernate.init.HibernateUtil; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author bruno */ public class DireccionDAO { public void agregaProvedor(Direccion direccion) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.save(direccion); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void borraProvedor(int idDireccion) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Direccion direccion = (Direccion) session.load(Direccion.class, new Integer(idDireccion)); session.delete(direccion); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void actualizaDireccion(Direccion direccion) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.update(direccion); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public List<Direccion> listaProvedores() { List<Direccion> direcciones = new ArrayList<Direccion>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); direcciones = session.createQuery("from Direccion").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return direcciones; } public Direccion buscaPedido(int idDireccion) { Direccion direccion = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Direccion where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", idDireccion); direccion = (Direccion) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return direccion; } }