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.Telefono; 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 TelProvedorDAO { public void agregaTelProvedor(Telefono telefono) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.save(telefono); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void borraTelProvedor(int idTelefono) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Telefono telefono = (Telefono) session.load(Telefono.class, new Integer(idTelefono)); session.delete(telefono); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void actualizaTelProvedor(Telefono telefono) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.update(telefono); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public List<Telefono> listaTelProvedores() { List<Telefono> reparaciones = new ArrayList<Telefono>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); reparaciones = session.createQuery("from Telefono").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return reparaciones; } public Telefono buscaTelProvedor(int idTelefono) { Telefono telefono = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Telefono where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", idTelefono); telefono = (Telefono) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return telefono; } }