The Spring Framework

org.springframework.orm.hibernate3
Class AbstractSessionFactoryBean

java.lang.Object
  extended by org.springframework.orm.hibernate3.AbstractSessionFactoryBean
All Implemented Interfaces:
DisposableBean, FactoryBean, InitializingBean, PersistenceExceptionTranslator
Direct Known Subclasses:
LocalSessionFactoryBean

public abstract class AbstractSessionFactoryBean
extends Object
implements FactoryBean, InitializingBean, DisposableBean, PersistenceExceptionTranslator

Abstract FactoryBean that exposes a Hibernate SessionFactory. Supports building a transaction-aware SessionFactory proxy that exposes a Spring-managed transaction Session as "current Session".

This class also implements the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of a Hibernate SessionFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate Hibernate exceptions.

This class mainly serves as common base class for LocalSessionFactoryBean. For details on typical SessionFactory setup, see the LocalSessionFactoryBean javadoc.

Since:
2.0
Author:
Juergen Hoeller
See Also:
setExposeTransactionAwareSessionFactory(boolean), SessionFactory.getCurrentSession(), PersistenceExceptionTranslationPostProcessor

Field Summary
protected  Log logger
          Logger available to subclasses
 
Constructor Summary
AbstractSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Build and expose the SessionFactory.
protected abstract  SessionFactory buildSessionFactory()
          Build the underlying Hibernate SessionFactory.
protected  DataAccessException convertHibernateAccessException(HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
 void destroy()
          Close the SessionFactory on bean factory shutdown.
 Object getObject()
          Return the singleton SessionFactory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
protected  SessionFactory getSessionFactory()
          Return the exposed SessionFactory.
protected  SessionFactory getTransactionAwareSessionFactoryProxy(SessionFactory target)
          Wrap the given SessionFactory with a proxy that delegates every method call to it but delegates getCurrentSession calls to SessionFactoryUtils, for participating in Spring-managed transactions.
protected  boolean isExposeTransactionAwareSessionFactory()
          Return whether to expose a transaction-aware proxy for the SessionFactory.
 boolean isSingleton()
          Is the bean managed by this factory a singleton or a prototype?
 void setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory)
          Set whether to expose a transaction-aware proxy for the SessionFactory, returning the Session that's associated with the current Spring-managed transaction on getCurrentSession(), if any.
 void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
          Set the JDBC exception translator for the SessionFactory, exposed via the PersistenceExceptionTranslator interface.
 DataAccessException translateExceptionIfPossible(RuntimeException ex)
          Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
protected  SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf)
          Wrap the given SessionFactory with a transaction-aware proxy, if demanded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Logger available to subclasses

Constructor Detail

AbstractSessionFactoryBean

public AbstractSessionFactoryBean()
Method Detail

setExposeTransactionAwareSessionFactory

public void setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory)
Set whether to expose a transaction-aware proxy for the SessionFactory, returning the Session that's associated with the current Spring-managed transaction on getCurrentSession(), if any.

Default is "true", letting data access code work with the plain Hibernate SessionFactory and its getCurrentSession() method, while still being able to participate in current Spring-managed transactions: with any transaction management strategy, either local or JTA / EJB CMT, and any transaction synchronization mechanism, either Spring or JTA. Furthermore, getCurrentSession() will also seamlessly work with a request-scoped Session managed by OpenSessionInViewFilter/Interceptor.

Turn this flag off to expose the plain Hibernate SessionFactory with Hibernate's default getCurrentSession() behavior, where Hibernate 3.0.x only supports plain JTA synchronization. On Hibernate 3.1+, such a plain SessionFactory will by default have a SpringSessionContext registered to nevertheless provide Spring-managed Sessions; this can be overridden through the corresponding Hibernate property "hibernate.current_session_context_class".

See Also:
SessionFactory.getCurrentSession(), JtaTransactionManager, HibernateTransactionManager, OpenSessionInViewFilter, OpenSessionInViewInterceptor, SpringSessionContext

isExposeTransactionAwareSessionFactory

protected boolean isExposeTransactionAwareSessionFactory()
Return whether to expose a transaction-aware proxy for the SessionFactory.


setJdbcExceptionTranslator

public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
Set the JDBC exception translator for the SessionFactory, exposed via the PersistenceExceptionTranslator interface.

Applied to any SQLException root cause of a Hibernate JDBCException, overriding Hibernate's default SQLException translation (which is based on Hibernate's Dialect for a specific target database).

Parameters:
jdbcExceptionTranslator - the exception translator
See Also:
SQLException, JDBCException, SQLErrorCodeSQLExceptionTranslator, SQLStateSQLExceptionTranslator, PersistenceExceptionTranslator

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Build and expose the SessionFactory.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.
See Also:
buildSessionFactory(), wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory)

wrapSessionFactoryIfNecessary

protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf)
Wrap the given SessionFactory with a transaction-aware proxy, if demanded.

Parameters:
rawSf - the raw SessionFactory as built by buildSessionFactory()
Returns:
the SessionFactory reference to expose
See Also:
buildSessionFactory(), getTransactionAwareSessionFactoryProxy(org.hibernate.SessionFactory)

getTransactionAwareSessionFactoryProxy

protected SessionFactory getTransactionAwareSessionFactoryProxy(SessionFactory target)
Wrap the given SessionFactory with a proxy that delegates every method call to it but delegates getCurrentSession calls to SessionFactoryUtils, for participating in Spring-managed transactions.

Parameters:
target - the original SessionFactory to wrap
Returns:
the wrapped SessionFactory
See Also:
SessionFactory.getCurrentSession(), SessionFactoryUtils.doGetSession(org.hibernate.SessionFactory, boolean)

getSessionFactory

protected final SessionFactory getSessionFactory()
Return the exposed SessionFactory.

Throws:
IllegalStateException - if the SessionFactory has not been initialized yet

destroy

public void destroy()
             throws HibernateException
Close the SessionFactory on bean factory shutdown.

Specified by:
destroy in interface DisposableBean
Throws:
HibernateException

getObject

public Object getObject()
Return the singleton SessionFactory.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (can be null)
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance.

This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object (a reference that can be cached)?

NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton
See Also:
FactoryBean.getObject()

translateExceptionIfPossible

public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.

Converts the exception if it is a HibernateException; else returns null to indicate an unknown exception.

Specified by:
translateExceptionIfPossible in interface PersistenceExceptionTranslator
Parameters:
ex - a RuntimeException thrown
Returns:
the corresponding DataAccessException (or null if the exception could not be translated, as in this case it may result from user code rather than an actual persistence problem)
See Also:
PersistenceExceptionTranslationPostProcessor, convertHibernateAccessException(org.hibernate.HibernateException)

convertHibernateAccessException

protected DataAccessException convertHibernateAccessException(HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

Will automatically apply a specified SQLExceptionTranslator to a Hibernate JDBCException, else rely on Hibernate's default translation.

Parameters:
ex - HibernateException that occured
Returns:
a corresponding DataAccessException
See Also:
SessionFactoryUtils.convertHibernateAccessException(org.hibernate.HibernateException), setJdbcExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)

buildSessionFactory

protected abstract SessionFactory buildSessionFactory()
                                               throws Exception
Build the underlying Hibernate SessionFactory.

Returns:
the raw SessionFactory (potentially to be wrapped with a transaction-aware proxy before it is exposed to the application)
Throws:
Exception - in case of initialization failure

The Spring Framework

Copyright © 2002-2006 The Spring Framework.