org.springframework.orm.hibernate
Class LocalSessionFactoryBean


public class LocalSessionFactoryBean
implements DisposableBean, FactoryBean, InitializingBean

FactoryBean that creates a local Hibernate SessionFactory instance. Behaves like a SessionFactory instance when used as bean reference, e.g. for HibernateTemplate's "sessionFactory" property. Note that switching to JndiObjectFactoryBean is just a matter of configuration!

The typical usage will be to register this as singleton factory (for a certain underlying JDBC DataSource) in an application context, and give bean references to application services that need it.

Configuration settings can either be read from a Hibernate XML file, specified as "configLocation", or completely via this class. A typical local configuration consists of one or more "mappingResources", various "hibernateProperties" (not strictly necessary), and a "dataSource" that the SessionFactory should use. The latter can also be specified via Hibernate properties, but "dataSource" supports any Spring-configured DataSource, instead of relying on Hibernate's own connection providers.

This SessionFactory handling strategy is appropriate for most types of applications, from Hibernate-only single database apps to ones that need distributed transactions. Either HibernateTransactionManager or JtaTransactionManager can be used for transaction demarcation, the latter only being necessary for transactions that span multiple databases.

Registering a SessionFactory with JNDI is only advisable when using Hibernate's JCA Connector, i.e. when the application server cares for initialization. Else, portability is rather limited: Manual JNDI binding isn't supported by some application servers (e.g. Tomcat). Unfortunately, JCA has drawbacks too: Its setup is container-specific and can be tedious.

Note that the JCA Connector's sole major strength is its seamless cooperation with EJB containers and JTA services. If you do not use EJB and initiate your JTA transactions via Spring's JtaTransactionManager, you can get all benefits including distributed transactions and proper transactional JVM-level caching with local SessionFactory setup too - without any configuration hassle like container-specific setup.

Note: Spring's Hibernate support requires Hibernate 2.x (2.1 recommended).

Author:
Juergen Hoeller
Since: 05.05.2003
See Also: HibernateAccessor.setSessionFactory(SessionFactory), HibernateTransactionManager.setSessionFactory(SessionFactory), JndiObjectFactoryBean

Method Summary
 voidafterPropertiesSet()
          Initialize the SessionFactory for the given or the default location.
 voiddestroy()
          Close the SessionFactory on bean factory shutdown.
 static LobHandlergetConfigTimeLobHandler()
          Return the LobHandler for the currently configured Hibernate SessionFactory, to be used by Type implementations like ClobStringType.
 ObjectgetObject()
          Return the singleton SessionFactory.
 ClassgetObjectType()
           
 booleanisSingleton()
           
 voidsetConfigLocation(String configLocation)
          Set the location of the Hibernate XML config file as class path resource.
 voidsetDataSource(DataSource dataSource)
          Set the DataSource to be used by the SessionFactory.
 voidsetEntityInterceptor(Interceptor entityInterceptor)
          Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.
 voidsetHibernateProperties(Properties hibernateProperties)
          Set Hibernate properties, like "hibernate.dialect".
 voidsetJtaTransactionManager(TransactionManager jtaTransactionManager)
          Set the JTA TransactionManager to be used for Hibernate's TransactionManagerLookup.
 voidsetLobHandler(LobHandler lobHandler)
          Set the LobHandler to be used by the SessionFactory.
 voidsetMappingResourceJars(String[] mappingResourceJars)
          Set jar files in the class path that contain Hibernate mapping resources, like "example.hbm.jar".
 voidsetMappingResources(String[] mappingResources)
          Set Hibernate mapping resources to be found in the class path, like "example.hbm.xml" or "mypackage/example.hbm.xml".
 voidsetSchemaUpdate(boolean schemaUpdate)
          Set whether to execute a schema update after SessionFactory initialization.

Method Detail

afterPropertiesSet

public void afterPropertiesSet()
throws java.lang.IllegalArgumentException,
HibernateException
Initialize the SessionFactory for the given or the default location.
Throws:
IllegalArgumentException - in case of illegal property values
HibernateException - in case of Hibernate initialization errors

destroy

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

getConfigTimeLobHandler

public static LobHandler getConfigTimeLobHandler()
Return the LobHandler for the currently configured Hibernate SessionFactory, to be used by Type implementations like ClobStringType.

This instance will be set before initialization of the corresponding SessionFactory, and reset immediately afterwards. It is thus only available in constructors of UserType implementations.

See Also:
setLobHandler(org.springframework.jdbc.support.lob.LobHandler), ClobStringType, net.sf.hibernate.type.Type

getObject

public Object getObject()
Return the singleton SessionFactory.

getObjectType

public Class getObjectType()

isSingleton

public boolean isSingleton()

setConfigLocation

public void setConfigLocation(String configLocation)
Set the location of the Hibernate XML config file as class path resource. A typical value is "/hibernate.cfg.xml", in the case of web applications normally to be found in WEB-INF/classes.

Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.


setDataSource

public void setDataSource(DataSource dataSource)
Set the DataSource to be used by the SessionFactory. If set, this will override corresponding settings in Hibernate properties.

Note: If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.

See Also:
LocalDataSourceConnectionProvider

setEntityInterceptor

public void setEntityInterceptor(Interceptor entityInterceptor)
Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Will get applied to any new Session created by this factory.

Such an interceptor can either be set at the SessionFactory level, i.e. on LocalSessionFactoryBean, or at the Session level, i.e. on HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager. It's preferable to set it on LocalSessionFactoryBean or HibernateTransactionManager to avoid repeated configuration and guarantee consistent behavior in transactions.

See Also:
HibernateAccessor.setEntityInterceptor(Interceptor), HibernateAccessor.setEntityInterceptor(Interceptor), HibernateTransactionManager.setEntityInterceptor(Interceptor)

setHibernateProperties

public void setHibernateProperties(Properties hibernateProperties)
Set Hibernate properties, like "hibernate.dialect".

Can be used to override values in a Hibernate XML config file, or to specify all necessary properties locally.

Note: Do not specify a transaction provider here when using Spring-driven transactions. It is also advisable to omit connection provider settings and use a Spring-set DataSource instead.

See Also:
setDataSource(javax.sql.DataSource)

setJtaTransactionManager

public void setJtaTransactionManager(TransactionManager jtaTransactionManager)
Set the JTA TransactionManager to be used for Hibernate's TransactionManagerLookup. If set, this will override corresponding settings in Hibernate properties. Allows to use a Spring-managed JTA TransactionManager for Hibernate's cache synchronization.

Note: If this is set, the Hibernate settings should not define a transaction manager lookup to avoid meaningless double configuration.

See Also:
LocalTransactionManagerLookup

setLobHandler

public void setLobHandler(LobHandler lobHandler)
Set the LobHandler to be used by the SessionFactory. Will be exposed at config time for Type implementations.
See Also:
getConfigTimeLobHandler(), ClobStringType, net.sf.hibernate.type.Type

setMappingResourceJars

public void setMappingResourceJars(String[] mappingResourceJars)
Set jar files in the class path that contain Hibernate mapping resources, like "example.hbm.jar".

Can be used to override values from a Hibernate XML config file, or to specify all mappings locally.


setMappingResources

public void setMappingResources(String[] mappingResources)
Set Hibernate mapping resources to be found in the class path, like "example.hbm.xml" or "mypackage/example.hbm.xml".

Can be used to override values from a Hibernate XML config file, or to specify all mappings locally.


setSchemaUpdate

public void setSchemaUpdate(boolean schemaUpdate)
Set whether to execute a schema update after SessionFactory initialization.

For details on how to make schema update scripts work, see the Hibernate documentation, as this class leverages the same schema update script support in net.sf.hibernate.cfg.Configuration as Hibernate's own SchemaUpdate tool.

See Also:
net.sf.hibernate.cfg.Configuration#generateSchemaUpdateScript, net.sf.hibernate.tool.hbm2ddl.SchemaUpdate

Association Links

to Class java.lang.ThreadLocal

to Class java.lang.String

to Class java.lang.String

to Class java.lang.String

to Class java.util.Properties

to Class javax.sql.DataSource

to Class javax.transaction.TransactionManager

to Class org.springframework.jdbc.support.lob.LobHandler