public class LocalSessionFactoryBean extends HibernateExceptionTranslator implements org.springframework.beans.factory.FactoryBean<SessionFactory>, org.springframework.context.ResourceLoaderAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
FactoryBean
that creates a 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 data access objects via dependency injection.
NOTE: This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher.
It is similar in role to the same-named class in the orm.hibernate3
package.
However, in practice, it is closer to AnnotationSessionFactoryBean
since
its core purpose is to bootstrap a SessionFactory
from annotation scanning.
NOTE: To set up Hibernate 4 for Spring-driven JTA transactions, make
sure to either specify the "jtaTransactionManager"
bean property or to set the "hibernate.transaction.factory_class" property to
CMTTransactionFactory
.
Otherwise, Hibernate's smart flushing mechanism won't work properly.
setDataSource(javax.sql.DataSource)
,
setPackagesToScan(java.lang.String...)
Constructor and Description |
---|
LocalSessionFactoryBean() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected SessionFactory |
buildSessionFactory(LocalSessionFactoryBuilder sfb)
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.
|
void |
destroy() |
Configuration |
getConfiguration()
Return the Hibernate Configuration object used to build the SessionFactory.
|
Properties |
getHibernateProperties()
Return the Hibernate properties, if any.
|
SessionFactory |
getObject() |
Class<?> |
getObjectType() |
boolean |
isSingleton() |
void |
setAnnotatedClasses(Class<?>[] annotatedClasses)
Specify annotated entity classes to register with this Hibernate SessionFactory.
|
void |
setAnnotatedPackages(String[] annotatedPackages)
Specify the names of annotated packages, for which package-level
annotation metadata will be read.
|
void |
setCacheableMappingLocations(org.springframework.core.io.Resource[] cacheableMappingLocations)
Set locations of cacheable Hibernate mapping files, for example as web app
resource "/WEB-INF/mapping/example.hbm.xml".
|
void |
setConfigLocation(org.springframework.core.io.Resource configLocation)
Set the location of a single Hibernate XML config file, for example as
classpath resource "classpath:hibernate.cfg.xml".
|
void |
setConfigLocations(org.springframework.core.io.Resource[] configLocations)
Set the locations of multiple Hibernate XML config files, for example as
classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".
|
void |
setDataSource(DataSource dataSource)
Set the DataSource to be used by the SessionFactory.
|
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.
|
void |
setHibernateProperties(Properties hibernateProperties)
Set Hibernate properties, such as "hibernate.dialect".
|
void |
setJtaTransactionManager(Object jtaTransactionManager)
Set the Spring
JtaTransactionManager
or the JTA TransactionManager to be used with Hibernate,
if any. |
void |
setMappingDirectoryLocations(org.springframework.core.io.Resource[] mappingDirectoryLocations)
Set locations of directories that contain Hibernate mapping resources,
like "WEB-INF/mappings".
|
void |
setMappingJarLocations(org.springframework.core.io.Resource[] mappingJarLocations)
Set locations of jar files that contain Hibernate mapping resources,
like "WEB-INF/lib/example.hbm.jar".
|
void |
setMappingLocations(org.springframework.core.io.Resource[] mappingLocations)
Set locations of Hibernate mapping files, for example as classpath
resource "classpath:example.hbm.xml".
|
void |
setMappingResources(String[] mappingResources)
Set Hibernate mapping resources to be found in the class path,
like "example.hbm.xml" or "mypackage/example.hbm.xml".
|
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.
|
void |
setPackagesToScan(String... packagesToScan)
Specify packages to search for autodetection of your entity classes in the
classpath.
|
void |
setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader) |
convertHibernateAccessException, translateExceptionIfPossible
public void setDataSource(DataSource dataSource)
If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.
public void setConfigLocation(org.springframework.core.io.Resource configLocation)
Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.
public void setConfigLocations(org.springframework.core.io.Resource[] configLocations)
Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.
public void setMappingResources(String[] mappingResources)
Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.
public void setMappingLocations(org.springframework.core.io.Resource[] mappingLocations)
Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.
public void setCacheableMappingLocations(org.springframework.core.io.Resource[] cacheableMappingLocations)
Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.
public void setMappingJarLocations(org.springframework.core.io.Resource[] mappingJarLocations)
Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.
Configuration.addJar(java.io.File)
public void setMappingDirectoryLocations(org.springframework.core.io.Resource[] mappingDirectoryLocations)
Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.
public void setEntityInterceptor(Interceptor entityInterceptor)
public void setNamingStrategy(NamingStrategy namingStrategy)
public void setHibernateProperties(Properties hibernateProperties)
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.
setDataSource(javax.sql.DataSource)
public Properties getHibernateProperties()
public void setAnnotatedClasses(Class<?>[] annotatedClasses)
org.hibernate.cfg.Configuration#addAnnotatedClass(Class)
public void setAnnotatedPackages(String[] annotatedPackages)
org.hibernate.cfg.Configuration#addPackage(String)
public void setPackagesToScan(String... packagesToScan)
org.springframework.context.annotation.ClassPathBeanDefinitionScanner
).public void setJtaTransactionManager(Object jtaTransactionManager)
JtaTransactionManager
or the JTA TransactionManager
to be used with Hibernate,
if any.public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
setResourceLoader
in interface org.springframework.context.ResourceLoaderAware
public void afterPropertiesSet() throws IOException
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
IOException
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb)
The default implementation invokes LocalSessionFactoryBuilder's buildSessionFactory. A custom implementation could prepare the instance in a specific way (e.g. applying a custom ServiceRegistry) or use a custom SessionFactoryImpl subclass.
sfb
- LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBeanLocalSessionFactoryBuilder.buildSessionFactory()
public final Configuration getConfiguration()
IllegalStateException
- if the Configuration object has not been initialized yetpublic SessionFactory getObject()
getObject
in interface org.springframework.beans.factory.FactoryBean<SessionFactory>
public Class<?> getObjectType()
getObjectType
in interface org.springframework.beans.factory.FactoryBean<SessionFactory>
public boolean isSingleton()
isSingleton
in interface org.springframework.beans.factory.FactoryBean<SessionFactory>
public void destroy()
destroy
in interface org.springframework.beans.factory.DisposableBean