/** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html }. */ publicclass HibernateSessionFactory {
/** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ privatestaticString CONFIG_FILE_LOCATION ="/hibernate.cfg.xml"; privatestaticfinalThreadLocalthreadLocal=newThreadLocal(); privatestaticConfigurationconfiguration=newConfiguration(); privatestaticorg.hibernate.SessionFactory sessionFactory; privatestaticString configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory(){ }
/** * Returns the ThreadLocal Session instance. Lazy initialize * the <code>SessionFactory</code> if needed. * * @return Session * @throws HibernateException */ publicstatic Session getSession()throws HibernateException { Session session =(Session)threadLocal.get();