public class LocalSessionFactoryBean extends HibernateExceptionTranslator implements org.springframework.beans.factory.FactoryBean<SessionFactory>, org.springframework.context.ResourceLoaderAware, org.springframework.beans.factory.BeanFactoryAware, 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 data access objects via dependency injection.
Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
Set up with Hibernate 5.3, LocalSessionFactoryBean
is an immediate alternative
to LocalContainerEntityManagerFactoryBean
for common
JPA purposes: In particular with Hibernate 5.3, the Hibernate SessionFactory
will natively expose the JPA EntityManagerFactory
interface as well, and
Hibernate BeanContainer
integration will be registered out of the box.
In combination with HibernateTransactionManager
, this naturally allows for
mixing JPA access code with native Hibernate access code within the same transaction.
setDataSource(javax.sql.DataSource)
,
setPackagesToScan(java.lang.String...)
,
HibernateTransactionManager
,
LocalSessionFactoryBuilder
,
LocalContainerEntityManagerFactoryBean
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.
|
java.util.Properties |
getHibernateProperties()
Return the Hibernate properties, if any.
|
org.hibernate.boot.MetadataSources |
getMetadataSources()
Determine the Hibernate
MetadataSources to use. |
SessionFactory |
getObject() |
java.lang.Class<?> |
getObjectType() |
org.springframework.core.io.ResourceLoader |
getResourceLoader()
Determine the Spring
ResourceLoader to use for Hibernate metadata. |
boolean |
isSingleton() |
void |
setAnnotatedClasses(java.lang.Class<?>... annotatedClasses)
Specify annotated entity classes to register with this Hibernate SessionFactory.
|
void |
setAnnotatedPackages(java.lang.String... annotatedPackages)
Specify the names of annotated packages, for which package-level
annotation metadata will be read.
|
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
Accept the containing
BeanFactory , registering corresponding Hibernate
BeanContainer integration for
it if possible. |
void |
setBootstrapExecutor(org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor)
Specify an asynchronous executor for background bootstrapping,
e.g.
|
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 |
setCacheRegionFactory(RegionFactory cacheRegionFactory)
Set the Hibernate
RegionFactory to use for the SessionFactory. |
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 |
setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)
Set a
CurrentTenantIdentifierResolver to be passed on to the SessionFactory. |
void |
setDataSource(javax.sql.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 |
setEntityTypeFilters(org.springframework.core.type.filter.TypeFilter... entityTypeFilters)
Specify custom type filters for Spring-based scanning for entity classes.
|
void |
setHibernateIntegrators(Integrator... hibernateIntegrators)
Specify one or more Hibernate
Integrator implementations to apply. |
void |
setHibernateProperties(java.util.Properties hibernateProperties)
Set Hibernate properties, such as "hibernate.dialect".
|
void |
setImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy implicitNamingStrategy)
Set a Hibernate 5
ImplicitNamingStrategy for the SessionFactory. |
void |
setJtaTransactionManager(java.lang.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(java.lang.String... mappingResources)
Set Hibernate mapping resources to be found in the class path,
like "example.hbm.xml" or "mypackage/example.hbm.xml".
|
void |
setMetadataSources(org.hibernate.boot.MetadataSources metadataSources)
Specify a Hibernate
MetadataSources service to use (e.g. |
void |
setMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider multiTenantConnectionProvider)
Set a
MultiTenantConnectionProvider to be passed on to the SessionFactory. |
void |
setPackagesToScan(java.lang.String... packagesToScan)
Specify packages to search for autodetection of your entity classes in the
classpath.
|
void |
setPhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy physicalNamingStrategy)
Set a Hibernate 5
PhysicalNamingStrategy for the SessionFactory. |
void |
setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
Specify a Spring
ResourceLoader to use for Hibernate metadata. |
convertHibernateAccessException, setJdbcExceptionTranslator, translateExceptionIfPossible
public void setDataSource(javax.sql.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.
Configuration.configure(java.net.URL)
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.
Configuration.configure(java.net.URL)
public void setMappingResources(java.lang.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.
Configuration.addCacheableFile(File)
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(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.
Configuration.addDirectory(File)
public void setEntityInterceptor(Interceptor entityInterceptor)
public void setImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy implicitNamingStrategy)
ImplicitNamingStrategy
for the SessionFactory.public void setPhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy physicalNamingStrategy)
PhysicalNamingStrategy
for the SessionFactory.public void setJtaTransactionManager(java.lang.Object jtaTransactionManager)
JtaTransactionManager
or the JTA TransactionManager
to be used with Hibernate,
if any. Implicitly sets up JtaPlatform
.public void setCacheRegionFactory(RegionFactory cacheRegionFactory)
RegionFactory
to use for the SessionFactory.
Allows for using a Spring-managed RegionFactory
instance.
Note: If this is set, the Hibernate settings should not define a cache provider to avoid meaningless double configuration.
public void setMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider multiTenantConnectionProvider)
MultiTenantConnectionProvider
to be passed on to the SessionFactory.public void setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)
CurrentTenantIdentifierResolver
to be passed on to the SessionFactory.public void setHibernateProperties(java.util.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 java.util.Properties getHibernateProperties()
public void setEntityTypeFilters(org.springframework.core.type.filter.TypeFilter... entityTypeFilters)
Default is to search all specified packages for classes annotated with
@javax.persistence.Entity
, @javax.persistence.Embeddable
or @javax.persistence.MappedSuperclass
.
setPackagesToScan(java.lang.String...)
public void setAnnotatedClasses(java.lang.Class<?>... annotatedClasses)
Configuration.addAnnotatedClass(Class)
public void setAnnotatedPackages(java.lang.String... annotatedPackages)
Configuration.addPackage(String)
public void setPackagesToScan(java.lang.String... packagesToScan)
org.springframework.context.annotation.ClassPathBeanDefinitionScanner
).public void setBootstrapExecutor(org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor)
SimpleAsyncTaskExecutor
.
SessionFactory
initialization will then switch into background
bootstrap mode, with a SessionFactory
proxy immediately returned for
injection purposes instead of waiting for Hibernate's bootstrapping to complete.
However, note that the first actual call to a SessionFactory
method will
then block until Hibernate's bootstrapping completed, if not ready by then.
For maximum benefit, make sure to avoid early SessionFactory
calls
in init methods of related beans, even for metadata introspection purposes.
LocalSessionFactoryBuilder.buildSessionFactory(AsyncTaskExecutor)
public void setHibernateIntegrators(Integrator... hibernateIntegrators)
Integrator
implementations to apply.
This will only be applied for an internally built MetadataSources
instance. setMetadataSources(org.hibernate.boot.MetadataSources)
effectively overrides such settings,
with integrators to be applied to the externally built MetadataSources
.
setMetadataSources(org.hibernate.boot.MetadataSources)
,
BootstrapServiceRegistryBuilder.applyIntegrator(org.hibernate.integrator.spi.Integrator)
public void setMetadataSources(org.hibernate.boot.MetadataSources metadataSources)
MetadataSources
service to use (e.g. reusing an
existing one), potentially populated with a custom Hibernate bootstrap
ServiceRegistry
as well.MetadataSources.MetadataSources(ServiceRegistry)
,
BootstrapServiceRegistryBuilder.build()
public org.hibernate.boot.MetadataSources getMetadataSources()
MetadataSources
to use.
Can also be externally called to initialize and pre-populate a MetadataSources
instance which is then going to be used for SessionFactory
building.
null
)LocalSessionFactoryBuilder.LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources)
public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
ResourceLoader
to use for Hibernate metadata.setResourceLoader
in interface org.springframework.context.ResourceLoaderAware
resourceLoader
- the ResourceLoader to use (never null
)public org.springframework.core.io.ResourceLoader getResourceLoader()
ResourceLoader
to use for Hibernate metadata.null
)public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
BeanFactory
, registering corresponding Hibernate
BeanContainer
integration for
it if possible. This requires a Spring ConfigurableListableBeanFactory
and Hibernate 5.3 or higher on the classpath.setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
SpringBeanContainer
,
LocalSessionFactoryBuilder.setBeanContainer(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
public void afterPropertiesSet() throws java.io.IOException
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.io.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
- a LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBeanLocalSessionFactoryBuilder.buildSessionFactory(org.springframework.core.task.AsyncTaskExecutor)
public final Configuration getConfiguration()
java.lang.IllegalStateException
- if the Configuration object has not been initialized yet@Nullable public SessionFactory getObject()
getObject
in interface org.springframework.beans.factory.FactoryBean<SessionFactory>
public java.lang.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