类 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 HibernateConfigurationclass, addingSpringSessionContextas a default and providing convenient ways to specify a JDBCDataSourceand an application class loader.This is designed for programmatic use, e.g. in
@Beanfactory methods; consider usingLocalSessionFactoryBeanfor XML bean definition files. Typically combined withHibernateTransactionManagerfor declarative transactions against theSessionFactoryand its JDBCDataSource.Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3/5.4, as of Spring 5.2. Set up with Hibernate 5.2+, this builder is also a convenient way to set up a JPA
EntityManagerFactorysince the HibernateSessionFactorynatively exposes the JPAEntityManagerFactoryinterface as well now.This builder supports Hibernate 5.3/5.4
BeanContainerintegration,MetadataSourcesfrom customBootstrapServiceRegistryBuildersetup, as well as other advanced Hibernate configuration options beyond the standard JPA bootstrap contract.- 从以下版本开始:
- 4.2
- 作者:
- Juergen Hoeller
- 另请参阅:
HibernateTransactionManager,LocalSessionFactoryBean,setBeanContainer(org.springframework.beans.factory.config.ConfigurableListableBeanFactory),LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources),BootstrapServiceRegistryBuilder
字段概要
从类继承的字段 org.hibernate.cfg.Configuration
ARTEFACT_PROCESSING_ORDER
构造器概要
构造器 构造器 说明 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, ResourceLoader resourceLoader)Create a new LocalSessionFactoryBuilder for the given DataSource.LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, org.hibernate.boot.MetadataSources metadataSources)Create a new LocalSessionFactoryBuilder for the given DataSource.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 LocalSessionFactoryBuilderaddAnnotatedClasses(Class<?>... annotatedClasses)Add the given annotated classes in a batch.LocalSessionFactoryBuilderaddPackages(String... annotatedPackages)Add the given annotated packages in a batch.SessionFactorybuildSessionFactory(AsyncTaskExecutor bootstrapExecutor)Build the HibernateSessionFactorythrough background bootstrapping, using the given executor for a parallel initialization phase (e.g. aSimpleAsyncTaskExecutor).LocalSessionFactoryBuilderscanPackages(String... packagesToScan)Perform Spring-based scanning for entity classes, registering them as annotated classes with thisConfiguration.LocalSessionFactoryBuildersetBeanContainer(ConfigurableListableBeanFactory beanFactory)Set a HibernateBeanContainerfor the given SpringConfigurableListableBeanFactory.LocalSessionFactoryBuildersetCacheRegionFactory(RegionFactory cacheRegionFactory)Set the HibernateRegionFactoryto use for the SessionFactory.voidsetCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)Overridden to reliably pass aCurrentTenantIdentifierResolverto the SessionFactory.LocalSessionFactoryBuildersetEntityTypeFilters(TypeFilter... entityTypeFilters)Specify custom type filters for Spring-based scanning for entity classes.LocalSessionFactoryBuildersetJtaTransactionManager(Object jtaTransactionManager)Set the SpringJtaTransactionManageror the JTATransactionManagerto be used with Hibernate, if any.LocalSessionFactoryBuildersetMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider multiTenantConnectionProvider)Set aMultiTenantConnectionProviderto be passed on to the SessionFactory.从类继承的方法 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, buildMappings, buildSessionFactory, buildSessionFactory, configure, configure, configure, configure, configure, getCurrentTenantIdentifierResolver, getEntityNotFoundDelegate, getEntityTuplizerFactory, getInterceptor, getNamedEntityGraphs, getNamedProcedureCallMap, getNamedQueries, getNamedSQLQueries, getProperties, getProperty, getSessionFactoryObserver, getSqlFunctions, getSqlResultSetMappings, getStandardServiceRegistryBuilder, mergeProperties, registerTypeContributor, registerTypeOverride, registerTypeOverride, registerTypeOverride, reset, setEntityNotFoundDelegate, setImplicitNamingStrategy, setInterceptor, setPhysicalNamingStrategy, setProperties, setProperty, setSessionFactoryObserver, setSharedCacheMode
构造器详细资料
LocalSessionFactoryBuilder
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource)
Create a new LocalSessionFactoryBuilder for the given DataSource.- 参数:
dataSource- the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may benull)
LocalSessionFactoryBuilder
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ClassLoader classLoader)
Create a new LocalSessionFactoryBuilder for the given DataSource.- 参数:
dataSource- the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may benull)classLoader- the ClassLoader to load application classes from
LocalSessionFactoryBuilder
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader)
Create a new LocalSessionFactoryBuilder for the given DataSource.- 参数:
dataSource- the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may benull)resourceLoader- the ResourceLoader to load application classes from
LocalSessionFactoryBuilder
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader, org.hibernate.boot.MetadataSources metadataSources)
Create a new LocalSessionFactoryBuilder for the given DataSource.- 参数:
dataSource- the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may benull)resourceLoader- the ResourceLoader to load application classes frommetadataSources- the Hibernate MetadataSources service to use (e.g. reusing an existing one)- 从以下版本开始:
- 4.3
方法详细资料
setJtaTransactionManager
public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager)
Set 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.
setBeanContainer
public LocalSessionFactoryBuilder setBeanContainer(ConfigurableListableBeanFactory beanFactory)
Set a HibernateBeanContainerfor the given SpringConfigurableListableBeanFactory.Note: Bean container integration requires Hibernate 5.3 or higher. It enables autowiring of Hibernate attribute converters and entity listeners.
- 从以下版本开始:
- 5.1
- 另请参阅:
SpringBeanContainer,AvailableSettings.BEAN_CONTAINER
setCacheRegionFactory
public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory)
Set 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.
- 从以下版本开始:
- 5.1
- 另请参阅:
AvailableSettings.CACHE_REGION_FACTORY
setMultiTenantConnectionProvider
public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider multiTenantConnectionProvider)
Set aMultiTenantConnectionProviderto be passed on to the SessionFactory.- 从以下版本开始:
- 4.3
- 另请参阅:
AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER
setCurrentTenantIdentifierResolver
public void setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)
Overridden to reliably pass aCurrentTenantIdentifierResolverto the SessionFactory.- 覆盖:
setCurrentTenantIdentifierResolver在类中Configuration- 从以下版本开始:
- 4.3.2
- 另请参阅:
AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER
setEntityTypeFilters
public LocalSessionFactoryBuilder setEntityTypeFilters(TypeFilter... entityTypeFilters)
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.Embeddableor@javax.persistence.MappedSuperclass.
addAnnotatedClasses
public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses)
Add the given annotated classes in a batch.
addPackages
public LocalSessionFactoryBuilder addPackages(String... annotatedPackages)
Add the given annotated packages in a batch.
scanPackages
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException
Perform Spring-based scanning for entity classes, registering them as annotated classes with thisConfiguration.- 参数:
packagesToScan- one or more Java package names- 抛出:
HibernateException- if scanning fails for any reason
buildSessionFactory
public SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor)
Build the HibernateSessionFactorythrough background bootstrapping, using the given executor for a parallel initialization phase (e.g. 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.- 从以下版本开始:
- 4.3
- 另请参阅:
Configuration.buildSessionFactory()