Package org.springframework.orm.hibernate5

# Class LocalSessionFactoryBuilder

java.lang.Object
org.hibernate.cfg.Configuration
org.springframework.orm.hibernate5.LocalSessionFactoryBuilder

public class LocalSessionFactoryBuilder extends Configuration
A Spring-provided extension of the standard Hibernate `Configuration` class, adding `SpringSessionContext` as a default and providing convenient ways to specify a JDBC `DataSource` and an application class loader.

This is designed for programmatic use, for example, in `@Bean` factory methods; consider using `LocalSessionFactoryBean` for XML bean definition files. Typically combined with `HibernateTransactionManager` for declarative transactions against the `SessionFactory` and its JDBC `DataSource`.

Compatible with Hibernate ORM 5.5/5.6, as of Spring Framework 6.0. This Hibernate-specific factory builder can also be a convenient way to set up a JPA `EntityManagerFactory` since the Hibernate `SessionFactory` natively exposes the JPA `EntityManagerFactory` interface as well now.

This builder supports Hibernate `BeanContainer` integration, `MetadataSources` from custom `BootstrapServiceRegistryBuilder` setup, as well as other advanced Hibernate configuration options beyond the standard JPA bootstrap contract.

Since:
  4.2
Author:
  Juergen Hoeller
See Also:
  - `HibernateTransactionManager`
  - `LocalSessionFactoryBean`
  - `setBeanContainer(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)`
  - `LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources)`
  - `BootstrapServiceRegistryBuilder`

## Field Summary

### Fields inherited from class org.hibernate.cfg.Configuration

`ARTEFACT_PROCESSING_ORDER`

## Constructor Summary

Constructors
Constructor
Description
`LocalSessionFactoryBuilder(DataSource dataSource)`
Create a new LocalSessionFactoryBuilder for the given DataSource.
`LocalSessionFactoryBuilder(DataSource dataSource, ClassLoader classLoader)`
Create a new LocalSessionFactoryBuilder for the given DataSource.
`LocalSessionFactoryBuilder(DataSource dataSource, org.springframework.core.io.ResourceLoader resourceLoader)`
Create a new LocalSessionFactoryBuilder for the given DataSource.
`LocalSessionFactoryBuilder(DataSource dataSource, org.springframework.core.io.ResourceLoader resourceLoader, MetadataSources metadataSources)`
Create a new LocalSessionFactoryBuilder for the given DataSource.

## Method Summary

Modifier and Type
Method
Description
`LocalSessionFactoryBuilder`
`addAnnotatedClasses(Class<?>... annotatedClasses)`
Add the given annotated classes in a batch.
`LocalSessionFactoryBuilder`
`addPackages(String... annotatedPackages)`
Add the given annotated packages in a batch.
`SessionFactory`
`buildSessionFactory(org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor)`
Build the Hibernate `SessionFactory` through background bootstrapping, using the given executor for a parallel initialization phase (for example, a `SimpleAsyncTaskExecutor`).
`LocalSessionFactoryBuilder`
`scanPackages(String... packagesToScan)`
Perform Spring-based scanning for entity classes, registering them as annotated classes with this `Configuration`.
`LocalSessionFactoryBuilder`
`setBeanContainer(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)`
Set a Hibernate `BeanContainer` for the given Spring `ConfigurableListableBeanFactory`.
`LocalSessionFactoryBuilder`
`setCacheRegionFactory(RegionFactory cacheRegionFactory)`
Set the Hibernate `RegionFactory` to use for the SessionFactory.
`void`
`setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)`
Overridden to reliably pass a `CurrentTenantIdentifierResolver` to the SessionFactory.
`LocalSessionFactoryBuilder`
`setEntityTypeFilters(org.springframework.core.type.filter.TypeFilter... entityTypeFilters)`
Specify custom type filters for Spring-based scanning for entity classes.
`LocalSessionFactoryBuilder`
`setJtaTransactionManager(Object jtaTransactionManager)`
Set the Spring `JtaTransactionManager` or the JTA `TransactionManager` to be used with Hibernate, if any.
`LocalSessionFactoryBuilder`
`setMultiTenantConnectionProvider(MultiTenantConnectionProvider multiTenantConnectionProvider)`
Set a `MultiTenantConnectionProvider` to be passed on to the SessionFactory.

### Methods inherited from class org.hibernate.cfg.Configuration

`add, addAnnotatedClass, addAttributeConverter, addAttributeConverter, addAttributeConverter, addAttributeConverter, addAttributeConverter, addAuxiliaryDatabaseObject, addCacheableFile, addCacheableFile, addCacheableFileStrictly, addClass, addDirectory, addDocument, addFile, addFile, addInputStream, addJar, addPackage, addProperties, addResource, addResource, addSqlFunction, addURL, addXML, addXmlMapping, buildMappings, buildSessionFactory, buildSessionFactory, configure, configure, configure, configure, configure, getCurrentTenantIdentifierResolver, getEntityNotFoundDelegate, getEntityTuplizerFactory, getInterceptor, getNamedEntityGraphs, getNamedProcedureCallMap, getNamedQueries, getNamedSQLQueries, getProperties, getProperty, getSessionFactoryObserver, getSqlFunctions, getSqlResultSetMappings, getStandardServiceRegistryBuilder, getXmlMappingBinderAccess, mergeProperties, registerTypeContributor, registerTypeOverride, registerTypeOverride, registerTypeOverride, reset, setEntityNotFoundDelegate, setImplicitNamingStrategy, setInterceptor, setPhysicalNamingStrategy, setProperties, setProperty, setSessionFactoryObserver, setSharedCacheMode`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### LocalSessionFactoryBuilder

public LocalSessionFactoryBuilder(@Nullable DataSource dataSource)
Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters:
  `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)

### LocalSessionFactoryBuilder

public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ClassLoader classLoader)
Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters:
  `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)
  `classLoader` - the ClassLoader to load application classes from

### LocalSessionFactoryBuilder

public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, org.springframework.core.io.ResourceLoader resourceLoader)
Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters:
  `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)
  `resourceLoader` - the ResourceLoader to load application classes from

### LocalSessionFactoryBuilder

public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, org.springframework.core.io.ResourceLoader resourceLoader, MetadataSources metadataSources)
Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters:
  `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)
  `resourceLoader` - the ResourceLoader to load application classes from
  `metadataSources` - the Hibernate MetadataSources service to use (for example, reusing an existing one)
Since:
  4.3

## Method Details

### setJtaTransactionManager

public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager)
Set the Spring `JtaTransactionManager` or the JTA `TransactionManager` to be used with Hibernate, if any. Allows for using a Spring-managed transaction manager for Hibernate 5's session and cache synchronization, with the "hibernate.transaction.jta.platform" automatically set to it.

A passed-in Spring `JtaTransactionManager` needs to contain a JTA `TransactionManager` reference to be usable here, except for the WebSphere case where we'll automatically set `WebSphereExtendedJtaPlatform` accordingly.

Note: If this is set, the Hibernate settings should not contain a JTA platform setting to avoid meaningless double configuration.

### setBeanContainer

public LocalSessionFactoryBuilder setBeanContainer(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Set a Hibernate `BeanContainer` for the given Spring `ConfigurableListableBeanFactory`.

This enables autowiring of Hibernate attribute converters and entity listeners.

Since:
  5.1
See Also:
  - `SpringBeanContainer`
  - `AvailableSettings.BEAN_CONTAINER`

### setCacheRegionFactory

public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory)
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.

Since:
  5.1
See Also:
  - `AvailableSettings.CACHE_REGION_FACTORY`

### setMultiTenantConnectionProvider

public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(MultiTenantConnectionProvider multiTenantConnectionProvider)
Set a `MultiTenantConnectionProvider` to be passed on to the SessionFactory.

Since:
  4.3
See Also:
  - `AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER`

### setCurrentTenantIdentifierResolver

public void setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)
Overridden to reliably pass a `CurrentTenantIdentifierResolver` to the SessionFactory.

Overrides:
  `setCurrentTenantIdentifierResolver` in class `Configuration`
Since:
  4.3.2
See Also:
  - `AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER`

### setEntityTypeFilters

public LocalSessionFactoryBuilder setEntityTypeFilters(org.springframework.core.type.filter.TypeFilter... entityTypeFilters)
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`.

See Also:
  - `scanPackages(java.lang.String...)`

### addAnnotatedClasses

public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses)
Add the given annotated classes in a batch.

See Also:
  - `Configuration.addAnnotatedClass(java.lang.Class)`
  - `scanPackages(java.lang.String...)`

### addPackages

public LocalSessionFactoryBuilder addPackages(String... annotatedPackages)
Add the given annotated packages in a batch.

See Also:
  - `Configuration.addPackage(java.lang.String)`
  - `scanPackages(java.lang.String...)`

### scanPackages

public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException
Perform Spring-based scanning for entity classes, registering them as annotated classes with this `Configuration`.

Parameters:
  `packagesToScan` - one or more Java package names
Throws:
  `HibernateException` - if scanning fails for any reason

### buildSessionFactory

public SessionFactory buildSessionFactory(org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor)
Build the Hibernate `SessionFactory` through background bootstrapping, using the given executor for a parallel initialization phase (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.

Since:
  4.3
See Also:
  - `Configuration.buildSessionFactory()`