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.Cliente; 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 ClienteDAO { public void agregaCliente(Cliente cliente) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.save(cliente); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void borraCliente(int idCliente) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Cliente cliente = (Cliente) session.load(Cliente.class, new Integer(idCliente)); session.delete(cliente); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void actualizaCliente(Cliente cliente) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.update(cliente); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public List<Cliente> listaClientes() { List<Cliente> users = new ArrayList<Cliente>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); users = session.createQuery("from Cliente").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return users; } public Cliente buscaCliente(int idCliente) { Cliente cliente = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Cliente where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", idCliente); cliente = (Cliente) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return cliente; } }