public class LocalSessionFactoryBuilder extends Configuration
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.
LocalSessionFactoryBean
ARTEFACT_PROCESSING_ORDER
Constructor and 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,
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.
|
Modifier and Type | Method and 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(AsyncTaskExecutor bootstrapExecutor)
Build the Hibernate
SessionFactory through background bootstrapping,
using the given executor for a parallel initialization phase
(e.g. |
LocalSessionFactoryBuilder |
scanPackages(String... packagesToScan)
Perform Spring-based scanning for entity classes, registering them
as annotated classes with this
Configuration . |
void |
setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)
Overridden to reliably pass a
CurrentTenantIdentifierResolver to the SessionFactory. |
LocalSessionFactoryBuilder |
setEntityTypeFilters(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(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider multiTenantConnectionProvider)
Set a
MultiTenantConnectionProvider to be passed on to the SessionFactory. |
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
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource)
dataSource
- the JDBC DataSource that the resulting Hibernate SessionFactory should be using
(may be null
)public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ClassLoader classLoader)
dataSource
- the JDBC DataSource that the resulting Hibernate SessionFactory should be using
(may be null
)classLoader
- the ClassLoader to load application classes frompublic LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader)
dataSource
- the JDBC DataSource that the resulting Hibernate SessionFactory should be using
(may be null
)resourceLoader
- the ResourceLoader to load application classes frompublic LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader, org.hibernate.boot.MetadataSources metadataSources)
dataSource
- the JDBC DataSource that the resulting Hibernate SessionFactory should be using
(may be null
)resourceLoader
- the ResourceLoader to load application classes frommetadataSources
- the Hibernate MetadataSources service to use (e.g. reusing an existing one)public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager)
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.
public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider multiTenantConnectionProvider)
MultiTenantConnectionProvider
to be passed on to the SessionFactory.AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER
public void setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)
CurrentTenantIdentifierResolver
to the SessionFactory.setCurrentTenantIdentifierResolver
in class Configuration
AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER
public LocalSessionFactoryBuilder setEntityTypeFilters(TypeFilter... entityTypeFilters)
Default is to search all specified packages for classes annotated with
@javax.persistence.Entity
, @javax.persistence.Embeddable
or @javax.persistence.MappedSuperclass
.
scanPackages(java.lang.String...)
public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses)
public LocalSessionFactoryBuilder addPackages(String... annotatedPackages)
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException
Configuration
.packagesToScan
- one or more Java package namesHibernateException
- if scanning fails for any reasonpublic SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor)
SessionFactory
through background bootstrapping,
using the given executor for a parallel initialization phase
(e.g. 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.
Configuration.buildSessionFactory()