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 edu.temple.tutrucks; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; /** * Hibernate Utility class with a convenient method to get Session Factory * object. * * @author nickdellosa * @version %PROJECT_VERSION% */ public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); /** * Builds Hibernate's Session Factory. Required by Hibernate * @return the session factory for Hibernate */ private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); return configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed."); System.err.println(ex.getMessage()); ex.printStackTrace(); throw new ExceptionInInitializerError(ex); } } /** * Returns the current Hibernate session factory. Required by Hibernate * @return the current Hibernate session factory */ public static SessionFactory getSessionFactory() { return sessionFactory; } }