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.Profesor; import org.hibernate.HibernateError; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author Mariana */ public class ProfesorDAO { public int create(Profesor p) throws Exception { Session session = NewHibernateUtil.getSessionFactory().openSession(); Transaction tx = session.getTransaction(); try { tx.begin(); session.persist(p); //session.save(a); tx.commit(); session.close(); return 1; } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); return 0; } } public void update(Profesor p) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); try { tx.begin(); session.saveOrUpdate(p); 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(Profesor p) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); try { tx.begin(); session.delete(p); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } } public Profesor load(Profesor p) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); try { tx.begin(); p = (Profesor) session.get(Profesor.class, p.getRFC()); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } return p; } 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 profesor"); 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 Profesor login(String Bol, String cont) throws Exception { Session session = NewHibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.getTransaction(); Profesor prof = null; try { tx.begin(); Query q = session .createSQLQuery("select * from profesor where profesor.RFC=:RFC and profesor.contrasena =:cont") .addEntity(Profesor.class).setParameter("cont", cont).setParameter("RFC", Bol); prof = (Profesor) q.uniqueResult(); tx.commit(); } catch (HibernateError he) { if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Se ha cerrado la transaccion"); } return prof; } }