public class LocalSessionFactoryBean extends HibernateExceptionTranslator implements FactoryBean, ResourceLoaderAware, BeanFactoryAware, InitializingBean, SmartInitializingSingleton, 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 ORM 5.5/5.6, as of Spring Framework 6.0.
This Hibernate-specific LocalSessionFactoryBean can be an immediate alternative
to LocalContainerEntityManagerFactoryBean for
common JPA purposes: 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.
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
afterPropertiesSet() |
|
public void |
afterSingletonsInstantiated() |
|
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. |
|
public void |
destroy() |
|
public final Configuration |
getConfiguration()Return the Hibernate Configuration object used to build the SessionFactory. |
|
public Properties |
getHibernateProperties()Return the Hibernate properties, if any. |
|
public MetadataSources |
getMetadataSources()Determine the Hibernate MetadataSources to use. |
|
public SessionFactory |
getObject() |
|
public Class<?> |
getObjectType() |
|
public ResourceLoader |
getResourceLoader()Determine the Spring ResourceLoader to use for Hibernate metadata. |
|
public boolean |
isSingleton() |
|
public void |
setAnnotatedClasses(Class<?> annotatedClasses)Specify annotated entity classes to register with this Hibernate SessionFactory. |
|
public void |
setAnnotatedPackages(String annotatedPackages)Specify the names of annotated packages, for which package-level annotation metadata will be read. |
|
public void |
setBeanFactory(BeanFactory beanFactory)Accept the containing BeanFactory, registering corresponding Hibernate BeanContainer integration for it if possible. |
|
public void |
setBootstrapExecutor(AsyncTaskExecutor bootstrapExecutor)Specify an asynchronous executor for background bootstrapping, for example, a SimpleAsyncTaskExecutor. |
|
public void |
setCacheRegionFactory(RegionFactory cacheRegionFactory)Set the Hibernate RegionFactory to use for the SessionFactory. |
|
public void |
setCacheableMappingLocations(Resource cacheableMappingLocations)Set locations of cacheable Hibernate mapping files, for example as web app resource "/WEB-INF/mapping/example.hbm.xml". |
|
public void |
setConfigLocation(Resource configLocation)Set the location of a single Hibernate XML config file, for example as classpath resource "classpath:hibernate.cfg.xml". |
|
public void |
setConfigLocations(Resource configLocations)Set the locations of multiple Hibernate XML config files, for example as classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml". |
|
public void |
setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)Set a CurrentTenantIdentifierResolver to be passed on to the SessionFactory. |
|
public void |
setDataSource(javax.sql.DataSource dataSource)Set the DataSource to be used by the SessionFactory. |
|
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. |
|
public void |
setEntityTypeFilters(TypeFilter entityTypeFilters)Specify custom type filters for Spring-based scanning for entity classes. |
|
public void |
setHibernateIntegrators(Integrator hibernateIntegrators)Specify one or more Hibernate Integrator implementations to apply. |
|
public void |
setHibernateProperties(Properties hibernateProperties)Set Hibernate properties, such as "hibernate.dialect". |
|
public void |
setImplicitNamingStrategy(ImplicitNamingStrategy implicitNamingStrategy)Set a Hibernate 5 ImplicitNamingStrategy for the SessionFactory. |
|
public void |
setJtaTransactionManager(Object jtaTransactionManager)Set the Spring JtaTransactionManager or the JTA TransactionManager to be used with Hibernate, if any. |
|
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 |
setMetadataSources(MetadataSources metadataSources)Specify a Hibernate MetadataSources service to use (for example, reusing an existing one), potentially populated with a custom Hibernate bootstrap ServiceRegistry as well. |
|
public void |
setMultiTenantConnectionProvider(MultiTenantConnectionProvider multiTenantConnectionProvider)Set a MultiTenantConnectionProvider to be passed on to the SessionFactory. |
|
public void |
setPackagesToScan(String packagesToScan)Specify packages to search for autodetection of your entity classes in the classpath. |
|
public void |
setPhysicalNamingStrategy(PhysicalNamingStrategy physicalNamingStrategy)Set a Hibernate 5 PhysicalNamingStrategy for the SessionFactory. |
|
public void |
setResourceLoader(ResourceLoader resourceLoader)Specify a Spring ResourceLoader to use for Hibernate metadata. |
| Methods inherited from class | Name |
|---|---|
class HibernateExceptionTranslator |
convertHibernateAccessException, setJdbcExceptionTranslator, translateExceptionIfPossible |
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 LocalSessionFactoryBuilder's buildSessionFactory. A custom implementation could prepare the instance in a specific way (for example, applying a custom ServiceRegistry) or use a custom SessionFactoryImpl subclass.
sfb - a LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBeanReturn the Hibernate Configuration object used to build the SessionFactory. Allows for access to configuration metadata stored there (rarely needed).
Return the Hibernate properties, if any. Mainly available for configuration through property paths that specify individual keys.
Determine the Hibernate 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)Determine the Spring ResourceLoader to use for Hibernate metadata.
null)Specify annotated entity classes to register with this Hibernate SessionFactory.
Specify the names of annotated packages, for which package-level annotation metadata will be read.
Accept the containing BeanFactory, registering corresponding Hibernate BeanContainer integration for it if possible. This requires a Spring ConfigurableListableBeanFactory.
Specify an asynchronous executor for background bootstrapping, for example, a 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.
As of 6.2, Hibernate initialization is enforced before context refresh completion, waiting for asynchronous bootstrapping to complete by then.
Set the Hibernate 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.
Set locations of cacheable Hibernate mapping files, for example as web app resource "/WEB-INF/mapping/example.hbm.xml". Supports any resource location via Spring's resource abstraction, as long as the resource can be resolved in the file system.
Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.
Set the location of a single 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.
Set the locations of multiple Hibernate XML config files, for example as classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".
Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.
Set a CurrentTenantIdentifierResolver to be passed on to the SessionFactory.
Set the DataSource to be used by the SessionFactory. If set, this will override corresponding settings in Hibernate properties.
If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.
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.
Specify custom type filters for Spring-based scanning for entity classes.
Default is to search all specified packages for classes annotated with
@jakarta.persistence.Entity, @jakarta.persistence.Embeddable
or @jakarta.persistence.MappedSuperclass.
Specify one or more Hibernate Integrator implementations to apply.
This will only be applied for an internally built MetadataSources instance. setMetadataSources effectively overrides such settings, with integrators to be applied to the externally built MetadataSources.
Set Hibernate properties, such as "hibernate.dialect".
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.
Set a Hibernate 5 ImplicitNamingStrategy for the SessionFactory.
Set the Spring JtaTransactionManager
or the JTA TransactionManager to be used with Hibernate,
if any. Implicitly sets up JtaPlatform.
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.
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.
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.
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.
Specify a Hibernate MetadataSources service to use (for example, reusing an existing one), potentially populated with a custom Hibernate bootstrap ServiceRegistry as well.
Set a MultiTenantConnectionProvider to be passed on to the SessionFactory.
Specify packages to search for autodetection of your entity classes in the classpath. This is analogous to Spring's component-scan feature (ClassPathBeanDefinitionScanner).
Set a Hibernate 5 PhysicalNamingStrategy for the SessionFactory.
Specify a Spring ResourceLoader to use for Hibernate metadata.
resourceLoader - the ResourceLoader to use (never null)