| java.lang.Object org.springframework.orm.hibernate.LocalSessionFactoryBean
LocalSessionFactoryBean | public class LocalSessionFactoryBean implements FactoryBean,InitializingBean,DisposableBean(Code) | | org.springframework.beans.factory.FactoryBean that creates a
Hibernate
net.sf.hibernate.SessionFactory . This is the usual way to
set up a shared Hibernate SessionFactory in a Spring application context;
the SessionFactory can then be passed to Hibernate-based DAOs via
dependency injection.
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
org.springframework.transaction.jta.JtaTransactionManager can be
used for transaction demarcation, with the latter only necessary for
transactions which span multiple databases.
Note: Spring's Hibernate support in this package requires Hibernate 2.1.
Dedicated Hibernate3 support can be found in a separate package:
org.springframework.orm.hibernate3 .
author: Juergen Hoeller since: 05.05.2003 See Also: HibernateTemplate.setSessionFactory See Also: HibernateTransactionManager.setSessionFactory |
Field Summary | |
final protected Log | logger |
Method Summary | |
public void | afterPropertiesSet() Initialize the SessionFactory for the given or the default location. | public void | createDatabaseSchema() Execute schema creation script, determined by the Configuration object
used for creating the SessionFactory. | public void | destroy() Close the SessionFactory on bean factory shutdown. | public void | dropDatabaseSchema() Execute schema drop script, determined by the Configuration object
used for creating the SessionFactory. | protected void | executeSchemaScript(Connection con, String[] sql) Execute the given schema script on the given JDBC Connection.
Note that the default implementation will log unsuccessful statements
and continue to execute. | protected void | executeSchemaStatement(Statement stmt, String sql) Execute the given schema SQL on the given JDBC Statement.
Note that the default implementation will log unsuccessful statements
and continue to execute. | public static DataSource | getConfigTimeDataSource() Return the DataSource for the currently configured Hibernate SessionFactory,
to be used by LocalDataSourceConnectionProvoder.
This instance will be set before initialization of the corresponding
SessionFactory, and reset immediately afterwards. | public static LobHandler | getConfigTimeLobHandler() Return the LobHandler for the currently configured Hibernate SessionFactory,
to be used by UserType implementations like ClobStringType.
This instance will be set before initialization of the corresponding
SessionFactory, and reset immediately afterwards. | public static TransactionManager | getConfigTimeTransactionManager() Return the JTA TransactionManager for the currently configured Hibernate
SessionFactory, to be used by LocalTransactionManagerLookup.
This instance will be set before initialization of the corresponding
SessionFactory, and reset immediately afterwards. | public Configuration | getConfiguration() Return the Configuration object used to build the SessionFactory. | public Properties | getHibernateProperties() Return the Hibernate properties, if any. | public Object | getObject() Return the singleton SessionFactory. | public Class | getObjectType() | public boolean | isSingleton() | protected Configuration | newConfiguration() Subclasses can override this method to perform custom initialization
of the Configuration instance used for SessionFactory creation. | protected SessionFactory | newSessionFactory(Configuration config) Subclasses can override this method to perform custom initialization
of the SessionFactory instance, creating it via the given Configuration
object that got prepared by this LocalSessionFactoryBean. | protected void | postProcessConfiguration(Configuration config) To be implemented by subclasses that want to to perform custom
post-processing of the Configuration object after this FactoryBean
performed its default initialization. | public void | setConfigLocation(Resource configLocation) Set the location of the Hibernate XML config file, for example as
classpath resource "classpath:hibernate.cfg.xml". | 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.
If using HibernateTransactionManager as transaction strategy, consider
proxying your target DataSource with a LazyConnectionDataSourceProxy.
This defers fetching of an actual JDBC Connection until the first JDBC
Statement gets executed, even within JDBC transactions (as performed by
HibernateTransactionManager). | 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. | public void | setHibernateProperties(Properties hibernateProperties) Set Hibernate properties, such as "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. | public void | setJtaTransactionManager(TransactionManager jtaTransactionManager) Set the JTA TransactionManager to be used for Hibernate's
TransactionManagerLookup. | public void | setLobHandler(LobHandler lobHandler) Set the LobHandler to be used by the SessionFactory. | public void | setMappingDirectoryLocations(Resource[] mappingDirectoryLocations) Set locations of directories that contain Hibernate mapping resources,
like "WEB-INF/mappings". | public void | setMappingJarLocations(Resource[] mappingJarLocations) Set locations of jar files that contain Hibernate mapping resources,
like "WEB-INF/lib/example.hbm.jar". | public void | setMappingLocations(Resource[] mappingLocations) Set locations of Hibernate mapping files, for example as classpath
resource "classpath:example.hbm.xml". | 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". | public void | setNamingStrategy(NamingStrategy namingStrategy) Set a Hibernate NamingStrategy for the SessionFactory, determining the
physical column and table names given the info in the mapping document. | public void | setSchemaUpdate(boolean schemaUpdate) Set whether to execute a schema update after SessionFactory initialization. | public void | setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource) Set whether to use a transaction-aware DataSource for the SessionFactory,
i.e. | public void | updateDatabaseSchema() Execute schema update script, determined by the Configuration object
used for creating the SessionFactory. |
logger | final protected Log logger(Code) | | |
afterPropertiesSet | public void afterPropertiesSet() throws IllegalArgumentException, HibernateException, IOException(Code) | | Initialize the SessionFactory for the given or the default location.
throws: IllegalArgumentException - in case of illegal property values throws: HibernateException - in case of Hibernate initialization errors |
createDatabaseSchema | public void createDatabaseSchema() throws DataAccessException(Code) | | Execute schema creation script, determined by the Configuration object
used for creating the SessionFactory. A replacement for Hibernate's
SchemaExport class, to be invoked on application setup.
Fetch the LocalSessionFactoryBean itself rather than the exposed
SessionFactory to be able to invoke this method, e.g. via
LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory"); .
Uses the SessionFactory that this bean generates for accessing a JDBC
connection to perform the script.
throws: DataAccessException - in case of script execution errors See Also: net.sf.hibernate.cfg.Configuration.generateSchemaCreationScript See Also: net.sf.hibernate.tool.hbm2ddl.SchemaExport.create |
destroy | public void destroy() throws HibernateException(Code) | | Close the SessionFactory on bean factory shutdown.
|
dropDatabaseSchema | public void dropDatabaseSchema() throws DataAccessException(Code) | | Execute schema drop script, determined by the Configuration object
used for creating the SessionFactory. A replacement for Hibernate's
SchemaExport class, to be invoked on application setup.
Fetch the LocalSessionFactoryBean itself rather than the exposed
SessionFactory to be able to invoke this method, e.g. via
LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory"); .
Uses the SessionFactory that this bean generates for accessing a JDBC
connection to perform the script.
throws: DataAccessException - in case of script execution errors See Also: net.sf.hibernate.cfg.Configuration.generateDropSchemaScript See Also: net.sf.hibernate.tool.hbm2ddl.SchemaExport.drop |
executeSchemaScript | protected void executeSchemaScript(Connection con, String[] sql) throws SQLException(Code) | | Execute the given schema script on the given JDBC Connection.
Note that the default implementation will log unsuccessful statements
and continue to execute. Override the executeSchemaStatement
method to treat failures differently.
Parameters: con - the JDBC Connection to execute the script on Parameters: sql - the SQL statements to execute throws: SQLException - if thrown by JDBC methods See Also: LocalSessionFactoryBean.executeSchemaStatement |
executeSchemaStatement | protected void executeSchemaStatement(Statement stmt, String sql) throws SQLException(Code) | | Execute the given schema SQL on the given JDBC Statement.
Note that the default implementation will log unsuccessful statements
and continue to execute. Override this method to treat failures differently.
Parameters: stmt - the JDBC Statement to execute the SQL on Parameters: sql - the SQL statement to execute throws: SQLException - if thrown by JDBC methods (and considered fatal) |
getConfigTimeDataSource | public static DataSource getConfigTimeDataSource()(Code) | | Return the DataSource for the currently configured Hibernate SessionFactory,
to be used by LocalDataSourceConnectionProvoder.
This instance will be set before initialization of the corresponding
SessionFactory, and reset immediately afterwards. It is thus only available
during configuration.
See Also: LocalSessionFactoryBean.setDataSource See Also: LocalDataSourceConnectionProvider |
getConfigTimeTransactionManager | public static TransactionManager getConfigTimeTransactionManager()(Code) | | Return the JTA TransactionManager for the currently configured Hibernate
SessionFactory, to be used by LocalTransactionManagerLookup.
This instance will be set before initialization of the corresponding
SessionFactory, and reset immediately afterwards. It is thus only available
during configuration.
See Also: LocalSessionFactoryBean.setJtaTransactionManager See Also: LocalTransactionManagerLookup |
getConfiguration | public Configuration getConfiguration()(Code) | | Return the Configuration object used to build the SessionFactory.
Allows access to configuration metadata stored there (rarely needed).
|
getHibernateProperties | public Properties getHibernateProperties()(Code) | | Return the Hibernate properties, if any. Mainly available for
configuration through property paths that specify individual keys.
|
getObject | public Object getObject()(Code) | | Return the singleton SessionFactory.
|
isSingleton | public boolean isSingleton()(Code) | | |
newConfiguration | protected Configuration newConfiguration() throws HibernateException(Code) | | Subclasses can override this method to perform custom initialization
of the Configuration instance used for SessionFactory creation.
The properties of this LocalSessionFactoryBean will be applied to
the Configuration object that gets returned here.
The default implementation creates a new Configuration instance.
A custom implementation could prepare the instance in a specific way,
or use a custom Configuration subclass.
the Configuration instance throws: HibernateException - in case of Hibernate initialization errors See Also: net.sf.hibernate.cfg.Configuration.Configuration |
newSessionFactory | protected SessionFactory newSessionFactory(Configuration config) throws HibernateException(Code) | | Subclasses can override this method to perform custom initialization
of the SessionFactory instance, creating it via the given Configuration
object that got prepared by this LocalSessionFactoryBean.
The default implementation invokes Configuration's buildSessionFactory.
A custom implementation could prepare the instance in a specific way,
or use a custom SessionFactoryImpl subclass.
Parameters: config - Configuration prepared by this LocalSessionFactoryBean the SessionFactory instance throws: HibernateException - in case of Hibernate initialization errors See Also: net.sf.hibernate.cfg.Configuration.buildSessionFactory |
postProcessConfiguration | protected void postProcessConfiguration(Configuration config) throws HibernateException(Code) | | To be implemented by subclasses that want to to perform custom
post-processing of the Configuration object after this FactoryBean
performed its default initialization.
Parameters: config - the current Configuration object throws: HibernateException - in case of Hibernate initialization errors |
setConfigLocation | public void setConfigLocation(Resource configLocation)(Code) | | Set the location of the Hibernate XML config file, for example as
classpath resource "classpath:hibernate.cfg.xml".
Note: Can be omitted when all necessary properties and mapping
resources are specified locally via this bean.
See Also: net.sf.hibernate.cfg.Configuration.configure(java.net.URL) |
setDataSource | public void setDataSource(DataSource dataSource)(Code) | | 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.
If using HibernateTransactionManager as transaction strategy, consider
proxying your target DataSource with a LazyConnectionDataSourceProxy.
This defers fetching of an actual JDBC Connection until the first JDBC
Statement gets executed, even within JDBC transactions (as performed by
HibernateTransactionManager). Such lazy fetching is particularly beneficial
for read-only operations, in particular if the chances of resolving the
result in the second-level cache are high.
As JTA and transactional JNDI DataSources already provide lazy enlistment
of JDBC Connections, LazyConnectionDataSourceProxy does not add value with
JTA (i.e. Spring's JtaTransactionManager) as transaction strategy.
See Also: LocalSessionFactoryBean.setUseTransactionAwareDataSource See Also: LocalDataSourceConnectionProvider See Also: HibernateTransactionManager See Also: org.springframework.transaction.jta.JtaTransactionManager See Also: org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy |
setEntityInterceptor | public void setEntityInterceptor(Interceptor entityInterceptor)(Code) | | 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: HibernateTemplate.setEntityInterceptor See Also: HibernateInterceptor.setEntityInterceptor See Also: HibernateTransactionManager.setEntityInterceptor See Also: net.sf.hibernate.cfg.Configuration.setInterceptor |
setHibernateProperties | public void setHibernateProperties(Properties hibernateProperties)(Code) | | Set Hibernate properties, such as "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: LocalSessionFactoryBean.setDataSource |
setJtaTransactionManager | public void setJtaTransactionManager(TransactionManager jtaTransactionManager)(Code) | | 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 |
setMappingDirectoryLocations | public void setMappingDirectoryLocations(Resource[] mappingDirectoryLocations)(Code) | | Set locations of directories that contain Hibernate mapping resources,
like "WEB-INF/mappings".
Can be used to add to mappings from a Hibernate XML config file,
or to specify all mappings locally.
See Also: net.sf.hibernate.cfg.Configuration.addDirectory(java.io.File) |
setMappingJarLocations | public void setMappingJarLocations(Resource[] mappingJarLocations)(Code) | | Set locations of jar files that contain Hibernate mapping resources,
like "WEB-INF/lib/example.hbm.jar".
Can be used to add to mappings from a Hibernate XML config file,
or to specify all mappings locally.
See Also: net.sf.hibernate.cfg.Configuration.addJar(java.io.File) |
setMappingLocations | public void setMappingLocations(Resource[] mappingLocations)(Code) | | Set locations of Hibernate mapping files, for example as classpath
resource "classpath:example.hbm.xml". Supports any resource location
via Spring's resource abstraction, for example relative paths like
"WEB-INF/mappings/example.hbm.xml" when running in an application context.
Can be used to add to mappings from a Hibernate XML config file,
or to specify all mappings locally.
See Also: net.sf.hibernate.cfg.Configuration.addInputStream |
setMappingResources | public void setMappingResources(String[] mappingResources)(Code) | | Set Hibernate mapping resources to be found in the class path,
like "example.hbm.xml" or "mypackage/example.hbm.xml".
Analogous to mapping entries in a Hibernate XML config file.
Alternative to the more generic setMappingLocations method.
Can be used to add to mappings from a Hibernate XML config file,
or to specify all mappings locally.
See Also: LocalSessionFactoryBean.setMappingLocations See Also: net.sf.hibernate.cfg.Configuration.addResource |
setNamingStrategy | public void setNamingStrategy(NamingStrategy namingStrategy)(Code) | | Set a Hibernate NamingStrategy for the SessionFactory, determining the
physical column and table names given the info in the mapping document.
See Also: net.sf.hibernate.cfg.Configuration.setNamingStrategy |
setSchemaUpdate | public void setSchemaUpdate(boolean schemaUpdate)(Code) | | 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 See Also: net.sf.hibernate.tool.hbm2ddl.SchemaUpdate |
setUseTransactionAwareDataSource | public void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource)(Code) | | Set whether to use a transaction-aware DataSource for the SessionFactory,
i.e. whether to automatically wrap the passed-in DataSource with Spring's
TransactionAwareDataSourceProxy.
Default is "false": LocalSessionFactoryBean is usually used with Spring's
HibernateTransactionManager or JtaTransactionManager, both of which work nicely
on a plain JDBC DataSource. Hibernate Sessions and their JDBC Connections are
fully managed by the Hibernate/JTA transaction infrastructure in such a scenario.
If you switch this flag to "true", Spring's Hibernate access will be able to
participate in JDBC-based transactions managed outside of Hibernate
(for example, by Spring's DataSourceTransactionManager). This can be convenient
if you need a different local transaction strategy for another O/R mapping tool,
for example, but still want Hibernate access to join into those transactions.
A further benefit of this option is that plain Sessions opened directly
via the SessionFactory, outside of Spring's Hibernate support, will still
participate in active Spring-managed transactions.
As a further effect, using a transaction-aware DataSource will apply
remaining transaction timeouts to all created JDBC Statements. This means
that all operations performed by the SessionFactory will automatically
participate in Spring-managed transaction timeouts, not just queries.
This adds value even for HibernateTransactionManager.
WARNING: Be aware of side effects when using a transaction-aware
DataSource in combination with OpenSessionInViewFilter/Interceptor.
This combination is only properly supported with HibernateTransactionManager
transactions. PROPAGATION_SUPPORTS with HibernateTransactionManager and
JtaTransactionManager in general are only supported on Hibernate3, which
introduces (optional) aggressive release of Connections.
See Also: LocalSessionFactoryBean.setDataSource See Also: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy See Also: org.springframework.jdbc.datasource.DataSourceTransactionManager See Also: org.springframework.orm.hibernate.support.OpenSessionInViewFilter See Also: org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor See Also: HibernateTransactionManager See Also: org.springframework.transaction.jta.JtaTransactionManager See Also: org.springframework.orm.hibernate3.LocalSessionFactoryBean.setUseTransactionAwareDataSource |
updateDatabaseSchema | public void updateDatabaseSchema() throws DataAccessException(Code) | | Execute schema update script, determined by the Configuration object
used for creating the SessionFactory. A replacement for Hibernate's
SchemaUpdate class, for automatically executing schema update scripts
on application startup. Can also be invoked manually.
Fetch the LocalSessionFactoryBean itself rather than the exposed
SessionFactory to be able to invoke this method, e.g. via
LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ctx.getBean("&mySessionFactory"); .
Uses the SessionFactory that this bean generates for accessing a JDBC
connection to perform the script.
throws: DataAccessException - in case of script execution errors See Also: LocalSessionFactoryBean.setSchemaUpdate See Also: net.sf.hibernate.cfg.Configuration.generateSchemaUpdateScript See Also: net.sf.hibernate.tool.hbm2ddl.SchemaUpdate |
|
|