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.Servicio; 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 ServicioDAO { public void agregaServicio(Servicio servicio) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.save(servicio); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void borraServicio(int idServicio) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Servicio reparacion = (Servicio) session.load(Servicio.class, new Integer(idServicio)); session.delete(reparacion); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void actualizaServicio(Servicio reparacion) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.update(reparacion); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public List<Servicio> listaServicios() { List<Servicio> reparaciones = new ArrayList<Servicio>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); reparaciones = session.createQuery("from Servicio").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return reparaciones; } public Servicio buscaServicio(int idServicio) { Servicio reparacion = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Reparacion where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", idServicio); reparacion = (Servicio) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return reparacion; } }