|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.sdsc.inca.depot.persistent.HibernateUtil
public class HibernateUtil
Basic Hibernate helper class, handles SessionFactory, Session and Transaction.
Uses a static initializer to read startup options and initialize Configuration and SessionFactory.
This class tries to figure out if either ThreadLocal handling of the Session and Transaction should be used, for resource local transactions or BMT, or if CMT with automatic Session handling is enabled.
To keep your DAOs free from any of this, just call HibernateUtil.getCurrentSession() in the constructor of each DAO., The recommended way to set resource local or BMT transaction boundaries is an interceptor, or a request filter.
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 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 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.
Constructor Summary | |
---|---|
HibernateUtil()
|
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 org.hibernate.Session |
getCurrentSession()
Retrieves the current Session local to the thread. |
static org.hibernate.Interceptor |
getInterceptor()
|
static org.hibernate.SessionFactory |
getSessionFactory()
Returns the global SessionFactory. |
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 |
reconnect(org.hibernate.Session session)
Reconnects a Hibernate Session to the current Thread. |
static void |
registerInterceptorAndRebuild(org.hibernate.Interceptor interceptor)
Register a Hibernate interceptor with the current SessionFactory. |
static void |
rollbackTransaction()
Rollback the database transaction. |
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 |
---|
public HibernateUtil()
Method Detail |
---|
public static org.hibernate.cfg.Configuration getConfiguration()
public static org.hibernate.SessionFactory getSessionFactory()
public static void shutdown()
The only other method that can be called on HibernateUtil after this one is rebuildSessionFactory(Configuration).
public static void rebuildSessionFactory()
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.
public static void rebuildSessionFactory(org.hibernate.cfg.Configuration cfg)
HibernateUtil does not configure() the given Configuration object, it directly calls buildSessionFactory(). This method also closes the old SessionFactory before, if still open.
cfg
- public static org.hibernate.Session getCurrentSession()
public static void closeSession()
Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions.
public static void beginTransaction()
Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions. In both cases, it will either start a new transaction or join the existing ThreadLocal or JTA transaction.
public static void commitTransaction()
Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions. It will commit the ThreadLocal or BMT/JTA transaction.
public static void rollbackTransaction()
Is a no-op (with warning) if called in a CMT environment. Should be used in non-managed environments with resource local transactions, or with EJBs and bean-managed transactions. It will rollback the resource local or BMT/JTA transaction.
public static void reconnect(org.hibernate.Session session)
Unsupported in a CMT environment.
session
- The Hibernate Session to be reconnected.public static org.hibernate.Session disconnectSession()
public static void registerInterceptorAndRebuild(org.hibernate.Interceptor interceptor)
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.
public static org.hibernate.Interceptor getInterceptor()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |