org.springframework.orm.hibernate3.annotation
Class AnnotationSessionFactoryBean

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

public class AnnotationSessionFactoryBean
extends AnnotationSessionFactoryBuilder
implements SessionFactoryBeanOperations, ResourceLoaderAware

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

A typical AnnotationSessionFactoryBean bean definition:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="annotatedClasses">
     <list>
       <value>test.package.Foo</value>
       <value>test.package.Bar</value>
     </list>
   </property>
 </bean>
Or when using classpath scanning for autodetection of entity classes:
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="packagesToScan" value="test.package"/>
 </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 AnnotationSessionFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate Hibernate exceptions.

Since:
1.2.2
Author:
Juergen Hoeller, Chris Beams
See Also:
SessionFactoryBuilderSupport, AnnotationSessionFactoryBuilder

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate3.SessionFactoryBuilderSupport
logger
 
Constructor Summary
AnnotationSessionFactoryBean()
           
 
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 postProcessAnnotationConfiguration(org.hibernate.cfg.AnnotationConfiguration config)
          Deprecated. as of Spring 3.1 in favor of AnnotationSessionFactoryBuilder.postProcessAnnotationConfiguration() which can access the internal AnnotationConfiguration instance via SessionFactoryBuilderSupport.getConfiguration().
protected  void postProcessConfiguration(org.hibernate.cfg.Configuration config)
          Deprecated. as of Spring 3.1 in favor of AnnotationSessionFactoryBuilder.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 AnnotationSessionFactoryBuilder.postProcessMappings() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().
protected  void preBuildSessionFactory()
          Allow additional population of the underlying Configuration instance.
protected  void scanPackages(org.hibernate.cfg.AnnotationConfiguration config)
          Deprecated. as of Spring 3.1 in favor of AnnotationSessionFactoryBuilder.scanPackages() which can access the internal AnnotationConfiguration instance via SessionFactoryBuilderSupport.getConfiguration().
 void setBeanClassLoader(ClassLoader beanClassLoader)
          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.
 void setResourceLoader(ResourceLoader resourceLoader)
          Set the ResourceLoader that this object runs in.
 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.annotation.AnnotationSessionFactoryBuilder
getDefaultConfigurationClass, postProcessAnnotationConfiguration, postProcessConfiguration, postProcessMappings, scanPackages, setAnnotatedClasses, setAnnotatedPackages, setEntityTypeFilters, setPackagesToScan, setResourcePatternResolver
 
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, 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

AnnotationSessionFactoryBean

public AnnotationSessionFactoryBean()
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 void preBuildSessionFactory()
Description copied from class: SessionFactoryBuilderSupport
Allow additional population of the underlying Configuration instance. Called during SessionFactoryBuilderSupport.doBuildSessionFactory().

Overrides:
preBuildSessionFactory in class SessionFactoryBuilderSupport<AnnotationSessionFactoryBuilder>

postBuildSessionFactory

protected 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<AnnotationSessionFactoryBuilder>

setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
Description copied from interface: ResourceLoaderAware
Set the ResourceLoader that this object runs in.

This might be a ResourcePatternResolver, which can be checked through instanceof ResourcePatternResolver. See also the ResourcePatternUtils.getResourcePatternResolver method.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

Specified by:
setResourceLoader in interface ResourceLoaderAware
Parameters:
resourceLoader - ResourceLoader object to be used by this object
See Also:
ResourcePatternResolver, ResourcePatternUtils.getResourcePatternResolver(org.springframework.core.io.ResourceLoader)

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()
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)
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 beanClassLoader)
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:
beanClassLoader - 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<AnnotationSessionFactoryBuilder>
Parameters:
rawSf - the raw SessionFactory as built by SessionFactoryBuilderSupport.buildSessionFactory()
Returns:
the SessionFactory reference to expose
See Also:
SessionFactoryBuilderSupport.buildSessionFactory()

scanPackages

@Deprecated
protected void scanPackages(org.hibernate.cfg.AnnotationConfiguration config)
Deprecated. as of Spring 3.1 in favor of AnnotationSessionFactoryBuilder.scanPackages() which can access the internal AnnotationConfiguration instance via SessionFactoryBuilderSupport.getConfiguration().


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 AnnotationSessionFactoryBuilder.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 AnnotationSessionFactoryBuilder.postProcessConfiguration() which can access the internal Configuration instance via SessionFactoryBuilderSupport.getConfiguration().

Throws:
org.hibernate.HibernateException

postProcessAnnotationConfiguration

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

Throws:
org.hibernate.HibernateException