Explicit support for Hibernate Integrators on LocalSessionFactoryBean
Issue: SPR-16828
This commit is contained in:
parent
b0ece0e967
commit
b6d95567e8
|
|
@ -30,6 +30,8 @@ import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
|||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
|
||||
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
|
@ -44,15 +46,13 @@ import org.springframework.core.io.support.ResourcePatternUtils;
|
|||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.core.type.filter.TypeFilter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} that creates a Hibernate
|
||||
* {@link SessionFactory}. This is the usual way to set up a shared
|
||||
* Hibernate SessionFactory in a Spring application context; the SessionFactory can
|
||||
* then be passed to Hibernate-based data access objects via dependency injection.
|
||||
* {@link FactoryBean} that creates a Hibernate {@link SessionFactory}. This is the usual
|
||||
* way to set up a shared Hibernate SessionFactory in a Spring application context; the
|
||||
* SessionFactory can then be passed to data access objects via dependency injection.
|
||||
*
|
||||
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2, as of Spring 4.3.
|
||||
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.2
|
||||
|
|
@ -120,6 +120,9 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||
@Nullable
|
||||
private AsyncTaskExecutor bootstrapExecutor;
|
||||
|
||||
@Nullable
|
||||
private Integrator[] hibernateIntegrators;
|
||||
|
||||
private boolean metadataSourcesAccessed = false;
|
||||
|
||||
@Nullable
|
||||
|
|
@ -358,14 +361,28 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||
this.bootstrapExecutor = bootstrapExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify one or more Hibernate {@link Integrator} implementations to apply.
|
||||
* <p>This will only be applied for an internally built {@link MetadataSources}
|
||||
* instance. {@link #setMetadataSources} effectively overrides such settings,
|
||||
* with integrators to be applied to the externally built {@link MetadataSources}.
|
||||
* @since 5.1
|
||||
* @see #setMetadataSources
|
||||
* @see BootstrapServiceRegistryBuilder#applyIntegrator
|
||||
*/
|
||||
public void setHibernateIntegrators(Integrator... hibernateIntegrators) {
|
||||
this.hibernateIntegrators = hibernateIntegrators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a Hibernate {@link MetadataSources} service to use (e.g. reusing an
|
||||
* existing one), potentially populated with a custom Hibernate bootstrap
|
||||
* {@link org.hibernate.service.ServiceRegistry} as well.
|
||||
* @since 4.3
|
||||
* @see MetadataSources#MetadataSources(ServiceRegistry)
|
||||
* @see BootstrapServiceRegistryBuilder#build()
|
||||
*/
|
||||
public void setMetadataSources(MetadataSources metadataSources) {
|
||||
Assert.notNull(metadataSources, "MetadataSources must not be null");
|
||||
this.metadataSourcesAccessed = true;
|
||||
this.metadataSources = metadataSources;
|
||||
}
|
||||
|
|
@ -385,6 +402,11 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||
if (this.resourcePatternResolver != null) {
|
||||
builder = builder.applyClassLoader(this.resourcePatternResolver.getClassLoader());
|
||||
}
|
||||
if (this.hibernateIntegrators != null) {
|
||||
for (Integrator integrator : this.hibernateIntegrators) {
|
||||
builder = builder.applyIntegrator(integrator);
|
||||
}
|
||||
}
|
||||
this.metadataSources = new MetadataSources(builder.build());
|
||||
}
|
||||
return this.metadataSources;
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ import org.springframework.util.ClassUtils;
|
|||
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
|
||||
* Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
|
||||
*
|
||||
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2, as of Spring 4.3.
|
||||
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.2
|
||||
|
|
@ -136,7 +136,9 @@ public class LocalSessionFactoryBuilder extends Configuration {
|
|||
* @param metadataSources the Hibernate MetadataSources service to use (e.g. reusing an existing one)
|
||||
* @since 4.3
|
||||
*/
|
||||
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) {
|
||||
public LocalSessionFactoryBuilder(
|
||||
@Nullable DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) {
|
||||
|
||||
super(metadataSources);
|
||||
|
||||
getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
|
||||
|
|
|
|||
Loading…
Reference in New Issue