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.TelCliente; 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 TelClienteDAO { public void agregaTelCliente(TelCliente 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(); TelCliente telefono = (TelCliente) session.load(TelCliente.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 actualizaTelCliente(TelCliente 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<TelCliente> listaTelProvedores() { List<TelCliente> reparaciones = new ArrayList<TelCliente>(); 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 TelCliente buscaTelProvedor(int idTelefono) { TelCliente 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 = (TelCliente) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return telefono; } }