spring-framework / org.springframework.orm.hibernate5 / LocalSessionFactoryBuilder

LocalSessionFactoryBuilder

open class LocalSessionFactoryBuilder : Configuration

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

This is designed for programmatic use, e.g. in @Bean factory methods. Consider using LocalSessionFactoryBean for XML bean definition files.

Compatible with Hibernate 5.0/5.1 as well as 5.2, as of Spring 4.3.

Author
Juergen Hoeller

Since
4.2

See Also
LocalSessionFactoryBean

Constructors

<init>

LocalSessionFactoryBuilder(dataSource: DataSource)
LocalSessionFactoryBuilder(dataSource: DataSource, classLoader: ClassLoader)
LocalSessionFactoryBuilder(dataSource: DataSource, resourceLoader: ResourceLoader)
LocalSessionFactoryBuilder(dataSource: DataSource, resourceLoader: ResourceLoader, metadataSources: MetadataSources)

Create a new LocalSessionFactoryBuilder for the given DataSource.

Functions

addAnnotatedClasses

open fun addAnnotatedClasses(vararg annotatedClasses: Class<*>): LocalSessionFactoryBuilder

Add the given annotated classes in a batch.

addPackages

open fun addPackages(vararg annotatedPackages: String): LocalSessionFactoryBuilder

Add the given annotated packages in a batch.

buildSessionFactory

open fun buildSessionFactory(bootstrapExecutor: AsyncTaskExecutor): SessionFactory

Build the Hibernate SessionFactory through background bootstrapping, using the given executor for a parallel initialization phase (e.g. a org.springframework.core.task.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.

scanPackages

open fun scanPackages(vararg packagesToScan: String): LocalSessionFactoryBuilder

Perform Spring-based scanning for entity classes, registering them as annotated classes with this Configuration.

setCurrentTenantIdentifierResolver

open fun setCurrentTenantIdentifierResolver(currentTenantIdentifierResolver: CurrentTenantIdentifierResolver): Unit

Overridden to reliably pass a CurrentTenantIdentifierResolver to the SessionFactory.

setEntityTypeFilters

open fun setEntityTypeFilters(vararg entityTypeFilters: TypeFilter): LocalSessionFactoryBuilder

Specify custom type filters for Spring-based scanning for entity classes.

Default is to search all specified packages for classes annotated with @javax.persistence.Entity, @javax.persistence.Embeddable or @javax.persistence.MappedSuperclass.

setJtaTransactionManager

open fun setJtaTransactionManager(jtaTransactionManager: Any): LocalSessionFactoryBuilder

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.

setMultiTenantConnectionProvider

open fun setMultiTenantConnectionProvider(multiTenantConnectionProvider: MultiTenantConnectionProvider): LocalSessionFactoryBuilder

Set a MultiTenantConnectionProvider to be passed on to the SessionFactory.