net.sf.click.extras.hibernate
Class SessionContext

java.lang.Object
  extended bynet.sf.click.extras.hibernate.SessionContext

public class SessionContext
extends Object

Provides a thread local Hibernate Session context class. The Hibernate configuration should be defined in class path file:

 /hibernate.cfg.xml 
Or alternatively by using System properties.

The Hibernate initialization code used by SessionContext is:

 Configuration configuration = createConfiguration();
 configuration.setProperties(System.getProperties());
 configuration.configure();
 SessionFactory sessionFactory = configuration.buildSessionFactory(); 
To support the SessionContext class configure a SessionFilter in your web application.

Author:
Malcolm Edgar
See Also:
SessionFilter, HibernateForm

Constructor Summary
SessionContext()
           
 
Method Summary
static void close()
          Close the Session held by the current Thread.
 org.hibernate.cfg.Configuration createConfiguration()
          Creates and returns a new Configuration instance.
static org.hibernate.Session getSession()
          Get the Session for the current Thread, creating one if necessary.
static org.hibernate.SessionFactory getSessionFactory()
          Return the Hibernate SesssionFactory.
static boolean hasSession()
          Return true if a session is open.
 void initConfiguration(org.hibernate.cfg.Configuration configuration)
          Initialize the configuration instance.
 void onInit(ServletContext servletContext)
          Initializes the SessionContext instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionContext

public SessionContext()
Method Detail

onInit

public void onInit(ServletContext servletContext)
Initializes the SessionContext instance.

This includes creating a new Hibernate Configuration and building the SessionFactory.

This method first creates a new Configuration by invoking createConfiguration() and then initializes the configuration by invoking initConfiguration(org.hibernate.cfg.Configuration).

Parameters:
servletContext - the servlet context

createConfiguration

public org.hibernate.cfg.Configuration createConfiguration()
Creates and returns a new Configuration instance.

Note: as annotations have become popular the last couple of years, this method will try and detect if Hibernate's AnnotationConfiguration is available on the classpath. If it is a new AnnotationConfiguration instance is created otherwise a Configuration instance is created.

Returns:
new Hibernate Configuration instance.

initConfiguration

public void initConfiguration(org.hibernate.cfg.Configuration configuration)
Initialize the configuration instance.

You can override this method and manually setup the configuration:

 public Configuration createConfiguration() {
     configuration.setProperties(System.getProperties());
     configuration.configure();
 }

Parameters:
configuration - the configuration to initialize

getSession

public static org.hibernate.Session getSession()
                                        throws org.hibernate.HibernateException
Get the Session for the current Thread, creating one if necessary.

Returns:
the Session fro the current Thread.
Throws:
org.hibernate.HibernateException - if an error occurs opening the session

close

public static void close()
                  throws org.hibernate.HibernateException
Close the Session held by the current Thread. The close session will also be removed from the ThreadLocal variable.

Throws:
org.hibernate.HibernateException - if an error occurs closing the session

hasSession

public static boolean hasSession()
Return true if a session is open.

Returns:
true if a session is currently open.

getSessionFactory

public static org.hibernate.SessionFactory getSessionFactory()
Return the Hibernate SesssionFactory.

Returns:
the Hibernate SessionFactory