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 cr.ac.una.prograiv.moviestar.utils; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; import org.hibernate.Transaction; /** * Hibernate Utility class with a convenient method to get Session Factory * object. * * @author Mery Ziga */ public class HibernateUtil { private static final SessionFactory sessionFactory; private Session sesion; private Transaction transac; static { try { // Create the SessionFactory from standard (hibernate.cfg.xml) // config file. //sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory(); } catch (Throwable ex) { // Log the exception. System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public void iniciaOperacion() throws HibernateException { sesion = HibernateUtil.getSessionFactory().openSession(); transac = sesion.beginTransaction(); } public void manejaExcepcion(HibernateException he) throws HibernateException { transac.rollback(); throw new HibernateException("Ocurri un error en la capa de " + "acceso a datos", he); } public Session getSesion() { return sesion; } public void setSesion(Session sesion) { this.sesion = sesion; } public Transaction getTransac() { return transac; } public void setTransac(Transaction transac) { this.transac = transac; } }