Top Description Inners Fields Constructors Methods
org.springframework.orm.hibernate5

public Class LocalSessionFactoryBuilder

extends Configuration
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.io.FileNotFoundException, .IOException, java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.Collections, .Set, .TreeSet, java.util.concurrent.Callable, .ExecutionException, .Future, javax.sql.DataSource, jakarta.persistence.AttributeConverter, .Converter, .Embeddable, .Entity, .MappedSuperclass, jakarta.transaction.TransactionManager, org.hibernate.HibernateException, .MappingException, .SessionFactory, org.hibernate.boot.MetadataSources, org.hibernate.boot.registry.BootstrapServiceRegistryBuilder, org.hibernate.cache.spi.RegionFactory, org.hibernate.cfg.AvailableSettings, .Configuration, org.hibernate.context.spi.CurrentTenantIdentifierResolver, org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider, org.hibernate.engine.spi.SessionFactoryImplementor, org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.core.InfrastructureProxy, .SpringProperties, org.springframework.core.io.Resource, .ResourceLoader, org.springframework.core.io.support.PathMatchingResourcePatternResolver, .ResourcePatternResolver, .ResourcePatternUtils, org.springframework.core.task.AsyncTaskExecutor, org.springframework.core.type.classreading.CachingMetadataReaderFactory, .ClassFormatException, .MetadataReader, .MetadataReaderFactory, org.springframework.core.type.filter.AnnotationTypeFilter, .TypeFilter, org.springframework.lang.Nullable, org.springframework.transaction.jta.JtaTransactionManager, org.springframework.util.Assert, .ClassUtils

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, e.g. 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.

Author
Juergen Hoeller
Since
4.2
See Also
HibernateTransactionManager, LocalSessionFactoryBean, setBeanContainer, LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources), BootstrapServiceRegistryBuilder

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
LocalSessionFactoryBuilder.BootstrapSessionFactoryInvocationHandler

Proxy invocation handler for background bootstrapping, only enforcing a fully initialized target SessionFactory when actually needed.

Field Summary

Modifier and TypeField and Description
private static final TypeFilter
private static final TypeFilter[]
private TypeFilter[]
private static final String
private static final String
private static final String
private final ResourcePatternResolver
private static final boolean
Inherited from org.hibernate.cfg.Configuration:
ARTEFACT_PROCESSING_ORDER

Constructor Summary

AccessConstructor and Description
public
LocalSessionFactoryBuilder(DataSource
the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)
dataSource
)

Create a new LocalSessionFactoryBuilder for the given DataSource.

public
LocalSessionFactoryBuilder(DataSource
the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)
dataSource
,
ClassLoader
the ClassLoader to load application classes from
classLoader
)

Create a new LocalSessionFactoryBuilder for the given DataSource.

public
LocalSessionFactoryBuilder(DataSource
the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)
dataSource
,
ResourceLoader
the ResourceLoader to load application classes from
resourceLoader
)

Create a new LocalSessionFactoryBuilder for the given DataSource.

public
LocalSessionFactoryBuilder(DataSource
the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)
dataSource
,
ResourceLoader
the ResourceLoader to load application classes from
resourceLoader
,
MetadataSources
the Hibernate MetadataSources service to use (e.g. reusing an existing one)
metadataSources
)

Create a new LocalSessionFactoryBuilder for the given DataSource.

Method Summary

Modifier and TypeMethod and Description
public LocalSessionFactoryBuilder
addAnnotatedClasses(Class<?>... annotatedClasses)

Add the given annotated classes in a batch.

public LocalSessionFactoryBuilder
addPackages(String... annotatedPackages)

Add the given annotated packages in a batch.

public SessionFactory
buildSessionFactory(AsyncTaskExecutor bootstrapExecutor)

Deprecated as a consequence of SessionFactory being deprecated.
Build the Hibernate SessionFactory through background bootstrapping, using the given executor for a parallel initialization phase (e.g. a org.springframework.core.task.SimpleAsyncTaskExecutor).
private boolean
matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory)

Check whether any of the configured entity type filters matches the current class descriptor contained in the metadata reader.

public LocalSessionFactoryBuilder
scanPackages(String...
one or more Java package names
packagesToScan
)

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

public LocalSessionFactoryBuilder
public LocalSessionFactoryBuilder
setCacheRegionFactory(RegionFactory cacheRegionFactory)

Set the Hibernate RegionFactory to use for the SessionFactory.

public void
public LocalSessionFactoryBuilder
setEntityTypeFilters(TypeFilter... entityTypeFilters)

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

public LocalSessionFactoryBuilder
setJtaTransactionManager(Object jtaTransactionManager)

Set the Spring JtaTransactionManager or the JTA TransactionManager to be used with Hibernate, if any.

public LocalSessionFactoryBuilder
setMultiTenantConnectionProvider(MultiTenantConnectionProvider multiTenantConnectionProvider)

Set a MultiTenantConnectionProvider to be passed on to the SessionFactory.

Inherited from org.hibernate.cfg.Configuration:
addaddAnnotatedClassaddAttributeConverteraddAttributeConverteraddAttributeConverteraddAttributeConverteraddAttributeConverteraddAuxiliaryDatabaseObjectaddCacheableFileaddCacheableFileaddCacheableFileStrictlyaddClassaddDirectoryaddDocumentaddFileaddFileaddInputStreamaddJaraddPackageaddPropertiesaddResourceaddResourceaddSqlFunctionaddURLaddXMLaddXmlMappingbuildMappingsbuildSessionFactorybuildSessionFactoryconfigureconfigureconfigureconfigureconfiguregetCurrentTenantIdentifierResolvergetEntityNotFoundDelegategetEntityTuplizerFactorygetInterceptorgetNamedEntityGraphsgetNamedProcedureCallMapgetNamedQueriesgetNamedSQLQueriesgetPropertiesgetPropertygetSessionFactoryObservergetSqlFunctionsgetSqlResultSetMappingsgetStandardServiceRegistryBuildergetXmlMappingBinderAccessmergePropertiesregisterTypeContributorregisterTypeOverrideregisterTypeOverrideregisterTypeOverrideresetsetEntityNotFoundDelegatesetImplicitNamingStrategysetInterceptorsetPhysicalNamingStrategysetPropertiessetPropertysetSessionFactoryObserversetSharedCacheMode

Field Detail

CONVERTER_TYPE_FILTERback to summary
private static final TypeFilter CONVERTER_TYPE_FILTER
DEFAULT_ENTITY_TYPE_FILTERSback to summary
private static final TypeFilter[] DEFAULT_ENTITY_TYPE_FILTERS
entityTypeFiltersback to summary
private TypeFilter[] entityTypeFilters
Annotations
@Nullable
IGNORE_CLASSFORMAT_PROPERTY_NAMEback to summary
private static final String IGNORE_CLASSFORMAT_PROPERTY_NAME
PACKAGE_INFO_SUFFIXback to summary
private static final String PACKAGE_INFO_SUFFIX
RESOURCE_PATTERNback to summary
private static final String RESOURCE_PATTERN
resourcePatternResolverback to summary
private final ResourcePatternResolver resourcePatternResolver
shouldIgnoreClassFormatExceptionback to summary
private static final boolean shouldIgnoreClassFormatException

Constructor Detail

LocalSessionFactoryBuilderback to summary
public LocalSessionFactoryBuilder(DataSource dataSource)

Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters
dataSource:DataSource

the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)

LocalSessionFactoryBuilderback to summary
public LocalSessionFactoryBuilder(DataSource dataSource, ClassLoader classLoader)

Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters
dataSource:DataSource

the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)

classLoader:ClassLoader

the ClassLoader to load application classes from

LocalSessionFactoryBuilderback to summary
public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader)

Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters
dataSource:DataSource

the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)

resourceLoader:ResourceLoader

the ResourceLoader to load application classes from

LocalSessionFactoryBuilderback to summary
public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources)

Create a new LocalSessionFactoryBuilder for the given DataSource.

Parameters
dataSource:DataSource

the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be null)

resourceLoader:ResourceLoader

the ResourceLoader to load application classes from

metadataSources:MetadataSources

the Hibernate MetadataSources service to use (e.g. reusing an existing one)

Since
4.3

Method Detail

addAnnotatedClassesback to summary
public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses)

Add the given annotated classes in a batch.

See Also
addAnnotatedClass, scanPackages
addPackagesback to summary
public LocalSessionFactoryBuilder addPackages(String... annotatedPackages)

Add the given annotated packages in a batch.

See Also
addPackage, scanPackages
buildSessionFactoryback to summary
public SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor)

Deprecated

as a consequence of SessionFactory being deprecated. See corresponding docs for further information.

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.

Since
4.3
See Also
buildSessionFactory()
matchesEntityTypeFilterback to summary
private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException

Check whether any of the configured entity type filters matches the current class descriptor contained in the metadata reader.

scanPackagesback to summary
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException

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

Parameters
packagesToScan:String[]

one or more Java package names

Annotations
@SuppressWarnings:unchecked
Exceptions
HibernateException:
if scanning fails for any reason
setBeanContainerback to summary
public LocalSessionFactoryBuilder setBeanContainer(ConfigurableListableBeanFactory beanFactory)

Set a Hibernate org.hibernate.resource.beans.container.spi.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
setCacheRegionFactoryback to summary
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
setCurrentTenantIdentifierResolverback to summary
public void setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver currentTenantIdentifierResolver)

Overrides org.hibernate.cfg.Configuration.setCurrentTenantIdentifierResolver.

Overridden to reliably pass a CurrentTenantIdentifierResolver to the SessionFactory.

Annotations
@Override
Since
4.3.2
See Also
AvailableSettings#MULTI_TENANT_IDENTIFIER_RESOLVER
setEntityTypeFiltersback to summary
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 @jakarta.persistence.Entity, @jakarta.persistence.Embeddable or @jakarta.persistence.MappedSuperclass.

See Also
scanPackages
setJtaTransactionManagerback to summary
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.

setMultiTenantConnectionProviderback to summary
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
org.springframework.orm.hibernate5 back to summary

private Class LocalSessionFactoryBuilder.BootstrapSessionFactoryInvocationHandler

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • org.springframework.orm.hibernate5.LocalSessionFactoryBuilder.BootstrapSessionFactoryInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Proxy invocation handler for background bootstrapping, only enforcing a fully initialized target SessionFactory when actually needed.
Since
4.3

Field Summary

Modifier and TypeField and Description
private final Future<SessionFactory>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private SessionFactory
getSessionFactory()

Deprecated as a consequence of SessionFactory being deprecated.
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

sessionFactoryFutureback to summary
private final Future<SessionFactory> sessionFactoryFuture

Constructor Detail

BootstrapSessionFactoryInvocationHandlerback to summary
public BootstrapSessionFactoryInvocationHandler(AsyncTaskExecutor bootstrapExecutor)

Method Detail

getSessionFactoryback to summary
private SessionFactory getSessionFactory()

Deprecated

as a consequence of SessionFactory being deprecated. See corresponding docs for further information.

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.