org.springframework.orm.hibernate
Class SessionFactoryUtils

java.lang.Object
  extended byorg.springframework.orm.hibernate.SessionFactoryUtils

public abstract class SessionFactoryUtils
extends java.lang.Object

Helper class featuring methods for Hibernate session handling, allowing for reuse of Hibernate Session instances within transactions.

Used internally by HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager. Can also be used directly in application code, e.g. in combination with HibernateInterceptor.

Note: Spring's Hibernate support requires Hibernate 2.1 (as of Spring 1.0).

Since:
02.05.2003
Author:
Juergen Hoeller
See Also:
HibernateTemplate, HibernateInterceptor, HibernateTransactionManager, JtaTransactionManager

Constructor Summary
SessionFactoryUtils()
           
 
Method Summary
static void applyTransactionTimeout(net.sf.hibernate.Criteria criteria, net.sf.hibernate.SessionFactory sessionFactory)
          Apply the current transaction timeout, if any, to the given Hibernate Criteria object.
static void applyTransactionTimeout(net.sf.hibernate.Query query, net.sf.hibernate.SessionFactory sessionFactory)
          Apply the current transaction timeout, if any, to the given Hibernate Query object.
static void closeSessionIfNecessary(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory)
          Close the given Session, created via the given factory, if it isn't bound to the thread.
static DataAccessException convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory, boolean allowCreate)
          Get a Hibernate Session for the given factory.
static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory, net.sf.hibernate.Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator)
          Get a Hibernate Session for the given factory.
static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory, net.sf.hibernate.Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator, boolean allowSynchronization)
          Get a Hibernate Session for the given factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionFactoryUtils

public SessionFactoryUtils()
Method Detail

getSession

public static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory,
                                                  boolean allowCreate)
                                           throws DataAccessResourceFailureException,
                                                  java.lang.IllegalStateException
Get a Hibernate Session for the given factory. Is aware of a respective Session bound to the current thread, for example when using HibernateTransactionManager. Will create a new Session else, if allowCreate is true.

This is the getSession method used by typical data access code, in combination with closeSessionIfNecessary called when done with the Session. Note that HibernateTemplate allows to write data access code without caring about such resource handling.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
allowCreate - if a new Session should be created if no thread-bound found
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
java.lang.IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
closeSessionIfNecessary(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory), HibernateTemplate

getSession

public static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory,
                                                  net.sf.hibernate.Interceptor entityInterceptor,
                                                  SQLExceptionTranslator jdbcExceptionTranslator)
Get a Hibernate Session for the given factory. Is aware of a respective Session bound to the current thread, for example when using HibernateTransactionManager. Will always create a new Session else.

Supports synchronization with JTA transactions via TransactionSynchronizationManager, to allow for proper transactional handling of the JVM-level cache.

Supports setting a Session-level Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Such an interceptor can also be set at the SessionFactory level, i.e. on LocalSessionFactoryBean.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
entityInterceptor - Hibernate entity interceptor, or null if none
jdbcExceptionTranslator - SQLExcepionTranslator to use for flushing the Session on transaction synchronization (can be null; only used when actually registering a transaction synchronization)
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
See Also:
LocalSessionFactoryBean.setEntityInterceptor(net.sf.hibernate.Interceptor), TransactionSynchronizationManager

getSession

public static net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory,
                                                  net.sf.hibernate.Interceptor entityInterceptor,
                                                  SQLExceptionTranslator jdbcExceptionTranslator,
                                                  boolean allowSynchronization)
                                           throws DataAccessResourceFailureException
Get a Hibernate Session for the given factory. Is aware of a respective Session bound to the current thread, for example when using HibernateTransactionManager. Will always create a new Session else.

Supports synchronization with JTA transactions via TransactionSynchronizationManager, to allow for proper transactional handling of the JVM-level cache.

Supports setting a Session-level Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Such an interceptor can also be set at the SessionFactory level, i.e. on LocalSessionFactoryBean.

Parameters:
sessionFactory - Hibernate SessionFactory to create the session with
entityInterceptor - Hibernate entity interceptor, or null if none
jdbcExceptionTranslator - SQLExcepionTranslator to use for flushing the Session on transaction synchronization (can be null; only used when actually registering a transaction synchronization)
allowSynchronization - if a new Hibernate Session is supposed to be registered with transaction synchronization (if synchronization is active). This will always be true for typical data access code.
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
See Also:
getSession(SessionFactory, Interceptor, SQLExceptionTranslator), LocalSessionFactoryBean.setEntityInterceptor(net.sf.hibernate.Interceptor), TransactionSynchronizationManager

applyTransactionTimeout

public static void applyTransactionTimeout(net.sf.hibernate.Query query,
                                           net.sf.hibernate.SessionFactory sessionFactory)
Apply the current transaction timeout, if any, to the given Hibernate Query object.

Parameters:
query - the Hibernate Query object
sessionFactory - Hibernate SessionFactory that the Query was created for

applyTransactionTimeout

public static void applyTransactionTimeout(net.sf.hibernate.Criteria criteria,
                                           net.sf.hibernate.SessionFactory sessionFactory)
Apply the current transaction timeout, if any, to the given Hibernate Criteria object.

Parameters:
criteria - the Hibernate Criteria object
sessionFactory - Hibernate SessionFactory that the Criteria was created for

convertHibernateAccessException

public static DataAccessException convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy. Note that it is advisable to handle JDBCException specifically by using an SQLExceptionTranslator for the underlying SQLException.

Parameters:
ex - HibernateException that occured
Returns:
the corresponding DataAccessException instance
See Also:
HibernateAccessor.convertHibernateAccessException(net.sf.hibernate.HibernateException), HibernateAccessor.convertJdbcAccessException(java.sql.SQLException), HibernateTemplate.execute(org.springframework.orm.hibernate.HibernateCallback)

closeSessionIfNecessary

public static void closeSessionIfNecessary(net.sf.hibernate.Session session,
                                           net.sf.hibernate.SessionFactory sessionFactory)
                                    throws CleanupFailureDataAccessException
Close the given Session, created via the given factory, if it isn't bound to the thread.

Parameters:
session - Session to close
sessionFactory - Hibernate SessionFactory that the Session was created with
Throws:
DataAccessResourceFailureException - if the Session couldn't be closed
CleanupFailureDataAccessException


Copyright (C) 2003-2004 The Spring Framework Project.