org.nakedobjects.plugins.hibernate.objectstore.util
Class HibernateUtil

java.lang.Object
  extended by org.nakedobjects.plugins.hibernate.objectstore.util.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

Field Summary
static java.lang.String MAPPING_DIR
           
 
Constructor Summary
HibernateUtil()
           
 
Method Summary
static void commitTransaction()
          Commits the transaction on the current session if one is active.
static void ensureMapped(NakedObjectSpecification specification)
           
static org.hibernate.cfg.Configuration getConfiguration()
          Returns the original Hibernate configuration.
static org.hibernate.Session getCurrentSession()
          Returns the current Session, and starts a new transaction if one is not active
static java.lang.String getRequiredClasses()
           
static org.hibernate.SessionFactory getSessionFactory()
          Returns the global SessionFactory.
static boolean hasInitRun()
           
static boolean init()
           
static void initialiseSessionFactory()
           
static boolean inTransaction()
           
static boolean isDatabaseKeyword(java.lang.String potentialKeyword)
           
static void rebuildSessionFactory()
          Rebuild the SessionFactory with the given Hibernate Configuration.
static void rollbackTransaction()
          Rolls back the transaction on the current session if one is active.
static void shutdown()
          Closes the current SessionFactory and releases all resources except the configuration.
static void startTransaction()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAPPING_DIR

public static final java.lang.String MAPPING_DIR
See Also:
Constant Field Values
Constructor Detail

HibernateUtil

public HibernateUtil()
Method Detail

initialiseSessionFactory

public static void initialiseSessionFactory()

getConfiguration

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

Returns:
Configuration

getSessionFactory

public static org.hibernate.SessionFactory getSessionFactory()
Returns the global SessionFactory.

Returns:
SessionFactory

getCurrentSession

public static org.hibernate.Session getCurrentSession()
Returns the current Session, and starts a new transaction if one is not active

Returns:
Session

startTransaction

public static void startTransaction()

inTransaction

public static boolean inTransaction()

commitTransaction

public static void commitTransaction()
Commits the transaction on the current session if one is active. Also auto closes the current session.


rollbackTransaction

public static void rollbackTransaction()
Rolls back the transaction on the current session if one is active. Also auto closes the current session.


rebuildSessionFactory

public static void rebuildSessionFactory()
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 -

getRequiredClasses

public static java.lang.String getRequiredClasses()

hasInitRun

public static boolean hasInitRun()

init

public static boolean init()

shutdown

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


isDatabaseKeyword

public static boolean isDatabaseKeyword(java.lang.String potentialKeyword)

ensureMapped

public static void ensureMapped(NakedObjectSpecification specification)


Copyright © 2001-2009 Naked Objects Group Ltd.. All Rights Reserved.