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.proyectohibernate.dao; import org.hibernate.HibernateException; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Hibernate Utility class with a convenient method to get Session Factory * object. * * @author rafa */ //public class HibernateUtil { ////http://stackoverflow.com/questions/20872943/configure-hibernate-with-embedded-tomcat-7-programmatically // //https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/wicket/src/main/java/hellowicket/HibernateUtil.java ////http://www.coderanch.com/t/590399/ORM/databases/Comment-hibernate-util-class // //http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/ // http://yourwebdevcenter.webhop.org/files/HibernateUtil.java // private static final SessionFactory sessionFactory; // // static { // try { // // Create the SessionFactory from standard (hibernate.cfg.xml) // // config file. // sessionFactory = new AnnotationConfiguration().configure().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; // } //} import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = getSessionFactory(); public static SessionFactory getSessionFactory() { try { return new Configuration().configure().buildSessionFactory(); } catch (HibernateException ex) { System.err.println("HibernateUtil - Error: " + ex); throw new ExceptionInInitializerError(ex); } } /** * Returns the original Hibernate configuration. * * @return Configuration */ public static Configuration getConfiguration() { return new Configuration(); } // private static final SessionFactory sessionFactory; // // static { // try { // // Create the SessionFactory from standard (hibernate.cfg.xml) // // config file. // sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); // } catch (HibernateException ex) { // // Log the exception. // System.err.println("Initial SessionFactory creation failed." + ex); // throw new ExceptionInInitializerError(ex); // } // } // // public static SessionFactory getSessionFactory() { // return sessionFactory; // } // public static void shutDown(){ // //closes caches and connections // getSessionFactory().close(); // } }