org.springframework.orm.hibernate3
Class LocalSessionFactoryBean

java.lang.Object
  extended by org.springframework.orm.hibernate3.SessionFactoryBuilderSupport<SessionFactoryBuilder>
      extended by org.springframework.orm.hibernate3.SessionFactoryBuilder
          extended by org.springframework.orm.hibernate3.LocalSessionFactoryBean
All Implemented Interfaces:
Aware, BeanClassLoaderAware, DisposableBean, FactoryBean<org.hibernate.SessionFactory>, InitializingBean, PersistenceExceptionTranslator, SessionFactoryBeanOperations

public class LocalSessionFactoryBean
extends SessionFactoryBuilder
implements SessionFactoryBeanOperations

Subclass of SessionFactoryBuilder adhering to Spring's FactoryBean contract, making it suitable for use in XML configuration.

A typical LocalSessionFactoryBean bean definition:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="mappingLocations" value="classpath:com/foo/*.hbm.xml"/>
 </bean>

Implements the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor, for AOP-based translation of native Hibernate exceptions to Spring's DataAccessException hierarchy. Hence, the presence of an LocalSessionFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate Hibernate exceptions.

Since:
1.2
Author:
Juergen Hoeller, Chris Beams
See Also:
SessionFactoryBuilderSupport, SessionFactoryBeanOperations, AnnotationSessionFactoryBean

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate3.SessionFactoryBuilderSupport
logger
 
Constructor Summary
LocalSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Build and expose the SessionFactory.
 void destroy()
          Close the SessionFactory on bean factory shutdown.
 org.hibernate.SessionFactory getObject()
          Return the singleton SessionFactory.
 Class<? extends org.hibernate.SessionFactory> getObjectType()
          Return the SessionFactory class used.
 boolean isSingleton()
          Return true.
protected  org.hibernate.SessionFactory newSessionFactory(org.hibernate.cfg.Configuration config)
          Deprecated. as of Spring 3.1 in favor of SessionFactoryBuilderSupport.newSessionFactory() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().
protected  void postBuildSessionFactory()
          Allow cleaning up resources, thread locals, etc after building the SessionFactory.
protected  void postProcessConfiguration(org.hibernate.cfg.Configuration config)
          Deprecated. as of Spring 3.1 in favor of SessionFactoryBuilderSupport.postProcessConfiguration() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().
protected  void postProcessMappings(org.hibernate.cfg.Configuration config)
          Deprecated. as of Spring 3.1 in favor of SessionFactoryBuilderSupport.postProcessMappings() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().
protected  void preBuildSessionFactory()
          Allow additional population of the underlying Configuration instance.
 void setBeanClassLoader(ClassLoader classLoader)
          Exists for compatibility with BeanClassLoaderAware but simply delegates to setClassLoader.
 void setCacheProvider(org.hibernate.cache.CacheProvider cacheProvider)
          Deprecated. 
 void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
          Set the JDBC exception translator for the SessionFactory on this instance's underlying HibernateExceptionTranslator.
 void setPersistenceExceptionTranslator(HibernateExceptionTranslator hibernateExceptionTranslator)
          Customize the HibernateExceptionTranslator to be used when translating native HibernateException types to Spring's DataAccessException hierarchy.
 DataAccessException translateExceptionIfPossible(RuntimeException ex)
          Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
 org.hibernate.SessionFactory wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory rawSf)
          Wrap the given SessionFactory with a proxy, if demanded.
 
Methods inherited from class org.springframework.orm.hibernate3.SessionFactoryBuilder
getDefaultConfigurationClass
 
Methods inherited from class org.springframework.orm.hibernate3.SessionFactoryBuilderSupport
afterSessionFactoryCreation, beforeSessionFactoryDestruction, buildSessionFactory, createDatabaseSchema, doBuildSessionFactory, doWithConfiguration, dropDatabaseSchema, executeSchemaScript, executeSchemaStatement, getBeanClassLoader, getCacheRegionFactory, getConfigTimeDataSource, getConfigTimeLobHandler, getConfigTimeTransactionManager, getConfiguration, getDataSource, getHibernateProperties, getSessionFactory, isExposeTransactionAwareSessionFactory, isUseTransactionAwareDataSource, newConfiguration, newSessionFactory, postProcessConfiguration, postProcessMappings, setCacheableMappingLocations, setCacheRegionFactory, setClassLoader, setCollectionCacheStrategies, setConfigLocation, setConfigLocations, setConfigurationClass, setDataSource, setEntityCacheStrategies, setEntityInterceptor, setEventListeners, setExposeTransactionAwareSessionFactory, setFilterDefinitions, setHibernateProperties, setJtaTransactionManager, setLobHandler, setMappingDirectoryLocations, setMappingJarLocations, setMappingLocations, setMappingResources, setNamingStrategy, setSchemaUpdate, setTypeDefinitions, setUseTransactionAwareDataSource, updateDatabaseSchema, validateDatabaseSchema
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalSessionFactoryBean

public LocalSessionFactoryBean()
Method Detail

setCacheProvider

@Deprecated
public void setCacheProvider(org.hibernate.cache.CacheProvider cacheProvider)
Deprecated. 

Description copied from interface: SessionFactoryBeanOperations
Deprecated. as of Spring 3.0 in favor of setCacheRegionFactory following Hibernate 3.3's deprecation of the CacheProvider SPI.

Set the Hibernate CacheProvider to use for the SessionFactory. Allows for using a Spring-managed CacheProvider instance.

Note: If this is set, the Hibernate settings should not define a cache provider to avoid meaningless double configuration. of the CacheProvider SPI in favor of RegionFactory SPI.

Specified by:
setCacheProvider in interface SessionFactoryBeanOperations

preBuildSessionFactory

protected final void preBuildSessionFactory()
Description copied from class: SessionFactoryBuilderSupport
Allow additional population of the underlying Configuration instance. Called during SessionFactoryBuilderSupport.doBuildSessionFactory().

Overrides:
preBuildSessionFactory in class SessionFactoryBuilderSupport<SessionFactoryBuilder>

postBuildSessionFactory

protected final void postBuildSessionFactory()
Description copied from class: SessionFactoryBuilderSupport
Allow cleaning up resources, thread locals, etc after building the SessionFactory. Called during the finally block of SessionFactoryBuilderSupport.doBuildSessionFactory().

Overrides:
postBuildSessionFactory in class SessionFactoryBuilderSupport<SessionFactoryBuilder>

destroy

public void destroy()
             throws org.hibernate.HibernateException
Description copied from interface: SessionFactoryBeanOperations
Close the SessionFactory on bean factory shutdown.

Specified by:
destroy in interface DisposableBean
Specified by:
destroy in interface SessionFactoryBeanOperations
Throws:
org.hibernate.HibernateException

getObject

public org.hibernate.SessionFactory getObject()
                                       throws Exception
Description copied from interface: SessionFactoryBeanOperations
Return the singleton SessionFactory.

Specified by:
getObject in interface FactoryBean<org.hibernate.SessionFactory>
Specified by:
getObject in interface SessionFactoryBeanOperations
Returns:
an instance of the bean (can be null)
Throws:
Exception - in case of creation errors
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class<? extends org.hibernate.SessionFactory> getObjectType()
Description copied from interface: SessionFactoryBeanOperations
Return the SessionFactory class used.

Specified by:
getObjectType in interface FactoryBean<org.hibernate.SessionFactory>
Specified by:
getObjectType in interface SessionFactoryBeanOperations
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)

setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)
Description copied from interface: SessionFactoryBeanOperations
Exists for compatibility with BeanClassLoaderAware but simply delegates to setClassLoader.

Specified by:
setBeanClassLoader in interface BeanClassLoaderAware
Specified by:
setBeanClassLoader in interface SessionFactoryBeanOperations
Parameters:
classLoader - the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via ClassUtils.getDefaultClassLoader()

isSingleton

public boolean isSingleton()
Description copied from interface: SessionFactoryBeanOperations
Return true.

Specified by:
isSingleton in interface FactoryBean<org.hibernate.SessionFactory>
Specified by:
isSingleton in interface SessionFactoryBeanOperations
Returns:
whether the exposed object is a singleton
See Also:
FactoryBean.getObject(), SmartFactoryBean.isPrototype()

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Description copied from interface: SessionFactoryBeanOperations
Build and expose the SessionFactory.

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

translateExceptionIfPossible

public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Description copied from interface: SessionFactoryBeanOperations
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
Specified by:
translateExceptionIfPossible in interface SessionFactoryBeanOperations
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

setJdbcExceptionTranslator

public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
Description copied from interface: SessionFactoryBeanOperations
Set the JDBC exception translator for the SessionFactory on this instance's underlying HibernateExceptionTranslator.

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

Specified by:
setJdbcExceptionTranslator in interface SessionFactoryBeanOperations
Parameters:
jdbcExceptionTranslator - the exception translator
See Also:
SessionFactoryBeanOperations.setPersistenceExceptionTranslator(HibernateExceptionTranslator), HibernateExceptionTranslator.setJdbcExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator), SQLException, JDBCException, SQLErrorCodeSQLExceptionTranslator, SQLStateSQLExceptionTranslator, PersistenceExceptionTranslator

setPersistenceExceptionTranslator

public void setPersistenceExceptionTranslator(HibernateExceptionTranslator hibernateExceptionTranslator)
Description copied from interface: SessionFactoryBeanOperations
Customize the HibernateExceptionTranslator to be used when translating native HibernateException types to Spring's DataAccessException hierarchy.

Specified by:
setPersistenceExceptionTranslator in interface SessionFactoryBeanOperations

wrapSessionFactoryIfNecessary

public org.hibernate.SessionFactory wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory rawSf)
Description copied from class: SessionFactoryBuilderSupport
Wrap the given SessionFactory with a proxy, if demanded.

The default implementation wraps the given SessionFactory as a Spring DisposableBean proxy in order to call SessionFactory.close() on ApplicationContext shutdown.

Subclasses may override this to implement transaction awareness through a SessionFactory proxy for example, or even to avoid creation of the DisposableBean proxy altogether.

Specified by:
wrapSessionFactoryIfNecessary in interface SessionFactoryBeanOperations
Overrides:
wrapSessionFactoryIfNecessary in class SessionFactoryBuilderSupport<SessionFactoryBuilder>
Parameters:
rawSf - the raw SessionFactory as built by SessionFactoryBuilderSupport.buildSessionFactory()
Returns:
the SessionFactory reference to expose
See Also:
SessionFactoryBuilderSupport.buildSessionFactory()

newSessionFactory

@Deprecated
protected org.hibernate.SessionFactory newSessionFactory(org.hibernate.cfg.Configuration config)
                                                  throws org.hibernate.HibernateException
Deprecated. as of Spring 3.1 in favor of SessionFactoryBuilderSupport.newSessionFactory() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().

Throws:
org.hibernate.HibernateException

postProcessMappings

@Deprecated
protected void postProcessMappings(org.hibernate.cfg.Configuration config)
                            throws org.hibernate.HibernateException
Deprecated. as of Spring 3.1 in favor of SessionFactoryBuilderSupport.postProcessMappings() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().

Throws:
org.hibernate.HibernateException

postProcessConfiguration

@Deprecated
protected void postProcessConfiguration(org.hibernate.cfg.Configuration config)
                                 throws org.hibernate.HibernateException
Deprecated. as of Spring 3.1 in favor of SessionFactoryBuilderSupport.postProcessConfiguration() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().

Throws:
org.hibernate.HibernateException