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 model.daos; import controler.NewHibernateUtil; import java.util.Collection; import java.util.List; import model.Alumno; import org.hibernate.HibernateError; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author Mariana */ public class AlumnoDAO { public void create(Alumno a) throws Exception { Session session = NewHibernateUtil.getSessionFactory().openSession(); Transaction tx = session.getTransaction(); try { tx.begin(); session.persist(a); //session.save(a); tx.commit(); session.close(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } } public void update(Alumno a) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); try { tx.begin(); session.saveOrUpdate(a); tx.commit(); session.close(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } } public void delete(Alumno a) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); try { tx.begin(); session.delete(a); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } } public Alumno load(Alumno a) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); try { tx.begin(); a = (Alumno) session.get(Alumno.class, a.getBoleta()); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } return a; } public Collection loadAll() throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); List l = null; try { tx.begin(); Query q = session.createQuery("from alumno"); l = q.list(); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } return l; } public Alumno login(String Bol, String cont) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); Alumno Alum = null; try { tx.begin(); Query q = session .createSQLQuery("select * from alumno where alumno.Boleta=:Bol and alumno.contrasena =:cont") .addEntity(Alumno.class).setParameter("cont", cont).setParameter("Bol", Bol); Alum = (Alumno) q.uniqueResult(); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } return Alum; } }