jhomenet.persistence
Class HibernateUtil

java.lang.Object
  extended by jhomenet.persistence.HibernateUtil

public class HibernateUtil
extends java.lang.Object

Basic Hibernate helper class for Hibernate configuration and startup.

Uses a static initializer to read startup options and initialize Configuration and SessionFactory.

This class also tries to figure out if JNDI binding of the SessionFactory is used, otherwise it falls back to a global static variable (Singleton). If you use this helper class to obtain a SessionFactory in your code, you are shielded from these deployment differences.

Another advantage of this class is access to the Configuration object that was used to build the current SessionFactory. You can access mapping metadata programmatically with this API, and even change it and rebuild the SessionFactory.

If you want to assign a global interceptor, set its fully qualified class name with the system (or hibernate.properties/hibernate.cfg.xml) property hibernate.util.interceptor_class. It will be loaded and instantiated on static initialization of HibernateUtil; it has to have a no-argument constructor. You can call HibernateUtil.getInterceptor() if you need to provide settings before using the interceptor.

Note: This class supports annotations by default, hence needs JDK 5.0 and the Hibernate Annotations library on the classpath. Change the single commented line in the source to make it compile and run on older JDKs with XML mapping files only.

Note: This class supports only one data store. Support for several SessionFactory instances can be easily added (through a static Map, for example). You could then lookup a SessionFactory by its name.

Author:
christian@hibernate.org

Constructor Summary
HibernateUtil()
           
 
Method Summary
static Configuration getConfiguration()
          Returns the original Hibernate configuration.
static Interceptor getInterceptor()
           
static SessionFactory getSessionFactory()
          Returns the global SessionFactory.
static void rebuildSessionFactory()
          Rebuild the SessionFactory with the static Configuration.
static void rebuildSessionFactory(Configuration cfg)
          Rebuild the SessionFactory with the given Hibernate Configuration.
static SessionFactory registerInterceptorAndRebuild(Interceptor interceptor)
          Register a Hibernate interceptor with the current SessionFactory.
static void shutdown()
          Closes the current SessionFactory and releases all resources.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateUtil

public HibernateUtil()
Method Detail

getConfiguration

public static Configuration getConfiguration()
Returns the original Hibernate configuration.

Returns:
Configuration

getSessionFactory

public static SessionFactory getSessionFactory()
Returns the global SessionFactory.

Returns:
SessionFactory

shutdown

public static void shutdown()
Closes the current SessionFactory and releases all resources.

The only other method that can be called on HibernateUtil after this one is rebuildSessionFactory(Configuration).


rebuildSessionFactory

public static void rebuildSessionFactory()
Rebuild the SessionFactory with the static Configuration.

This method also closes the old SessionFactory before, if still open. Note that this method should only be used with static SessionFactory management, not with JNDI or any other external registry.


rebuildSessionFactory

public static void rebuildSessionFactory(Configuration cfg)
Rebuild the SessionFactory with the given Hibernate Configuration.

HibernateUtil does not configure() the given Configuration object, it directly calls buildSessionFactory(). This method also closes the old SessionFactory before, if still open.

Parameters:
cfg -

registerInterceptorAndRebuild

public static SessionFactory registerInterceptorAndRebuild(Interceptor interceptor)
Register a Hibernate interceptor with the current SessionFactory.

Every Session opened is opened with this interceptor after registration. Has no effect if the current Session of the thread is already open, effective on next close()/getCurrentSession().

Attention: This method effectively restarts Hibernate. If you need an interceptor active on static startup of HibernateUtil, set the hibernateutil.interceptor system property to its fully qualified class name.


getInterceptor

public static Interceptor getInterceptor()


Copyright © 2004-2005 David Irwin. All Rights Reserved.