com.melloware.jukes.db
Class HibernateUtil

java.lang.Object
  extended by com.melloware.jukes.db.HibernateUtil

public final 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. Copyright (c) 1999-2007 Melloware, Inc.

Author:
Emil A. Lefkof III , christian@hibernate.org

Method Summary
static void beginTransaction()
          Start a new database transaction.
static void closeSession()
          Closes the Session local to the thread.
static void commitTransaction()
          Commit the database transaction.
static org.hibernate.Session disconnectSession()
          Disconnect and return Session from current Thread.
static org.hibernate.cfg.Configuration getConfiguration()
          Returns the original Hibernate configuration.
static java.lang.String getRemoteUrl()
          Gets the remoteUrl.
static org.hibernate.Session getSession()
          Retrieves the current Session local to the thread.
static org.hibernate.SessionFactory getSessionFactory()
          Returns the SessionFactory used for this static class.
static void initialize()
          Static initializer to startup Hibernate.
static boolean isCompact()
          Gets the compact.
static boolean isHSQLDialect()
          Gets the isHSQLDialect.
static void rebuildSessionFactory()
          Rebuild the SessionFactory with the static Configuration.
static void rebuildSessionFactory(org.hibernate.cfg.Configuration cfg)
          Rebuild the SessionFactory with the given Hibernate Configuration.
static void registerInterceptor(org.hibernate.Interceptor interceptor)
          Register a Hibernate interceptor with the current thread.
static void rollbackTransaction()
          Commit the database transaction.
static void setCompact(boolean aCompact)
          Sets the compact.
static void setRemoteUrl(java.lang.String aRemoteUrl)
          Sets the remoteUrl.
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
 

Method Detail

getConfiguration

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

Returns:
Configuration

getRemoteUrl

public static java.lang.String getRemoteUrl()
Gets the remoteUrl.

Returns:
Returns the remoteUrl.

getSession

public static org.hibernate.Session getSession()
                                        throws InfrastructureException
Retrieves the current Session local to the thread.

If no Session is open, opens a new Session for the running thread.

Returns:
Session
Throws:
InfrastructureException

getSessionFactory

public static org.hibernate.SessionFactory getSessionFactory()
Returns the SessionFactory used for this static class.

Returns:
SessionFactory

setCompact

public static void setCompact(boolean aCompact)
Sets the compact.

Parameters:
aCompact - The compact to set.

setRemoteUrl

public static void setRemoteUrl(java.lang.String aRemoteUrl)
Sets the remoteUrl.

Parameters:
aRemoteUrl - The remoteUrl to set.

isCompact

public static boolean isCompact()
Gets the compact.

Returns:
Returns the compact.

isHSQLDialect

public static boolean isHSQLDialect()
Gets the isHSQLDialect.

Returns:
Returns the isHSQLDialect.

beginTransaction

public static void beginTransaction()
                             throws InfrastructureException
Start a new database transaction.

Throws:
InfrastructureException

closeSession

public static void closeSession()
                         throws InfrastructureException
Closes the Session local to the thread.

Throws:
InfrastructureException

commitTransaction

public static void commitTransaction()
                              throws InfrastructureException
Commit the database transaction.

Throws:
InfrastructureException

disconnectSession

public static org.hibernate.Session disconnectSession()
                                               throws InfrastructureException
Disconnect and return Session from current Thread.

Returns:
Session the disconnected Session
Throws:
InfrastructureException

initialize

public static void initialize()
Static initializer to startup Hibernate.


rebuildSessionFactory

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

Throws:
InfrastructureException

rebuildSessionFactory

public static void rebuildSessionFactory(org.hibernate.cfg.Configuration cfg)
                                  throws InfrastructureException
Rebuild the SessionFactory with the given Hibernate Configuration.

Parameters:
cfg -
Throws:
InfrastructureException

registerInterceptor

public static void registerInterceptor(org.hibernate.Interceptor interceptor)
Register a Hibernate interceptor with the current thread.

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()/getSession().


rollbackTransaction

public static void rollbackTransaction()
                                throws InfrastructureException
Commit the database transaction.

Throws:
InfrastructureException

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).



Copyright © 1999-2008 Melloware Inc. All Rights Reserved.