Class LocalSessionFactoryBuilder
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:
- 
Constructor SummaryConstructorsConstructorDescriptionLocalSessionFactoryBuilder(@Nullable DataSource dataSource) Create a new LocalSessionFactoryBuilder for the given DataSource.LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ClassLoader classLoader) Create a new LocalSessionFactoryBuilder for the given DataSource.LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader) Create a new LocalSessionFactoryBuilder for the given DataSource.LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) Create a new LocalSessionFactoryBuilder for the given DataSource.
- 
Method SummaryModifier and TypeMethodDescriptionaddPackages(String... annotatedPackages) Add the given annotated packages in a batch.buildSessionFactory(AsyncTaskExecutor bootstrapExecutor) Build the HibernateSessionFactorythrough background bootstrapping, using the given executor for a parallel initialization phase (for example, aSimpleAsyncTaskExecutor).scanPackages(String... packagesToScan) Perform Spring-based scanning for entity classes, registering them as annotated classes with thisConfiguration.setBeanContainer(ConfigurableListableBeanFactory beanFactory) Set a HibernateBeanContainerfor the given SpringConfigurableListableBeanFactory.setCacheRegionFactory(RegionFactory cacheRegionFactory) Set the HibernateRegionFactoryto use for the SessionFactory.setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver<Object> currentTenantIdentifierResolver) Overridden to reliably pass aCurrentTenantIdentifierResolverto the SessionFactory.setEntityTypeFilters(TypeFilter... entityTypeFilters) Specify custom type filters for Spring-based scanning for entity classes.setJtaTransactionManager(Object jtaTransactionManager) Set the SpringJtaTransactionManageror the JTATransactionManagerto be used with Hibernate, if any.setMultiTenantConnectionProvider(MultiTenantConnectionProvider<?> multiTenantConnectionProvider) Set aMultiTenantConnectionProviderto be passed on to the SessionFactory.Methods inherited from class org.hibernate.cfg.ConfigurationaddAnnotatedClass, addAnnotatedClasses, addAttributeConverter, addAttributeConverter, addAttributeConverter, addAttributeConverter, addAttributeConverter, addAuxiliaryDatabaseObject, addCacheableFile, addCacheableFile, addCacheableFileStrictly, addClass, addDirectory, addEntityNameResolver, addFile, addFile, addInputStream, addJar, addPackage, addProperties, addResource, addSqlFunction, addURL, addXmlMapping, buildSessionFactory, buildSessionFactory, configure, configure, configure, configure, getColumnOrderingStrategy, getCurrentTenantIdentifierResolver, getCustomEntityDirtinessStrategy, getEntityNotFoundDelegate, getImplicitNamingStrategy, getInterceptor, getNamedEntityGraphs, getNamedProcedureCallMap, getNamedQueries, getNamedSQLQueries, getPhysicalNamingStrategy, getProperties, getProperty, getSessionFactoryObserver, getSqlResultSetMappings, getStandardServiceRegistryBuilder, getStatementInspector, getXmlMappingBinderAccess, mergeProperties, registerFunctionContributor, registerTypeContributor, registerTypeOverride, registerTypeOverride, setColumnOrderingStrategy, setCredentials, setCustomEntityDirtinessStrategy, setDatasource, setEntityNotFoundDelegate, setImplicitNamingStrategy, setInterceptor, setJdbcUrl, setPhysicalNamingStrategy, setProperties, setProperty, setProperty, setProperty, setProperty, setProperty, setSchemaExportAction, setSessionFactoryObserver, setSharedCacheMode, setStatementInspector, setTransactionType, showSql
- 
Constructor Details- 
LocalSessionFactoryBuilderCreate a new LocalSessionFactoryBuilder for the given DataSource.- Parameters:
- dataSource- the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be- null)
 
- 
LocalSessionFactoryBuilderCreate 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
 
- 
LocalSessionFactoryBuilderCreate 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
 
- 
LocalSessionFactoryBuilderpublic LocalSessionFactoryBuilder(@Nullable DataSource dataSource, 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- 
setJtaTransactionManagerSet the SpringJtaTransactionManageror the JTATransactionManagerto 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 JtaTransactionManagerneeds to contain a JTATransactionManagerreference to be usable here, except for the WebSphere case where we'll automatically setWebSphereExtendedJtaPlatformaccordingly.Note: If this is set, the Hibernate settings should not contain a JTA platform setting to avoid meaningless double configuration. 
- 
setBeanContainerSet a HibernateBeanContainerfor the given SpringConfigurableListableBeanFactory.This enables autowiring of Hibernate attribute converters and entity listeners. - Since:
- 5.1
- See Also:
 
- 
setCacheRegionFactorySet the HibernateRegionFactoryto use for the SessionFactory. Allows for using a Spring-managedRegionFactoryinstance.Note: If this is set, the Hibernate settings should not define a cache provider to avoid meaningless double configuration. - Since:
- 5.1
- See Also:
 
- 
setMultiTenantConnectionProviderpublic LocalSessionFactoryBuilder setMultiTenantConnectionProvider(MultiTenantConnectionProvider<?> multiTenantConnectionProvider) Set aMultiTenantConnectionProviderto be passed on to the SessionFactory.- Since:
- 4.3
- See Also:
 
- 
setCurrentTenantIdentifierResolverpublic LocalSessionFactoryBuilder setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver<Object> currentTenantIdentifierResolver) Overridden to reliably pass aCurrentTenantIdentifierResolverto the SessionFactory.- Overrides:
- setCurrentTenantIdentifierResolverin class- Configuration
- Since:
- 4.3.2
- See Also:
 
- 
setEntityTypeFiltersSpecify 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.Embeddableor@jakarta.persistence.MappedSuperclass.- See Also:
 
- 
addPackagesAdd the given annotated packages in a batch.- Overrides:
- addPackagesin class- Configuration
- See Also:
 
- 
scanPackagesPerform Spring-based scanning for entity classes, registering them as annotated classes with thisConfiguration.- Parameters:
- packagesToScan- one or more Java package names
- Throws:
- HibernateException- if scanning fails for any reason
 
- 
buildSessionFactoryBuild the HibernateSessionFactorythrough background bootstrapping, using the given executor for a parallel initialization phase (for example, aSimpleAsyncTaskExecutor).SessionFactoryinitialization will then switch into background bootstrap mode, with aSessionFactoryproxy immediately returned for injection purposes instead of waiting for Hibernate's bootstrapping to complete. However, note that the first actual call to aSessionFactorymethod will then block until Hibernate's bootstrapping completed, if not ready by then. For maximum benefit, make sure to avoid earlySessionFactorycalls in init methods of related beans, even for metadata introspection purposes.- Since:
- 4.3
- See Also:
 
 
-