Skip to content

Commit b6d9556

Browse files
committed
Explicit support for Hibernate Integrators on LocalSessionFactoryBean
Issue: SPR-16828
1 parent b0ece0e commit b6d9556

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBean.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.hibernate.cfg.Configuration;
3131
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
3232
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
33+
import org.hibernate.integrator.spi.Integrator;
34+
import org.hibernate.service.ServiceRegistry;
3335

3436
import org.springframework.beans.factory.DisposableBean;
3537
import org.springframework.beans.factory.FactoryBean;
@@ -44,15 +46,13 @@
4446
import org.springframework.core.task.AsyncTaskExecutor;
4547
import org.springframework.core.type.filter.TypeFilter;
4648
import org.springframework.lang.Nullable;
47-
import org.springframework.util.Assert;
4849

4950
/**
50-
* {@link FactoryBean} that creates a Hibernate
51-
* {@link SessionFactory}. This is the usual way to set up a shared
52-
* Hibernate SessionFactory in a Spring application context; the SessionFactory can
53-
* then be passed to Hibernate-based data access objects via dependency injection.
51+
* {@link FactoryBean} that creates a Hibernate {@link SessionFactory}. This is the usual
52+
* way to set up a shared Hibernate SessionFactory in a Spring application context; the
53+
* SessionFactory can then be passed to data access objects via dependency injection.
5454
*
55-
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2, as of Spring 4.3.
55+
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
5656
*
5757
* @author Juergen Hoeller
5858
* @since 4.2
@@ -120,6 +120,9 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
120120
@Nullable
121121
private AsyncTaskExecutor bootstrapExecutor;
122122

123+
@Nullable
124+
private Integrator[] hibernateIntegrators;
125+
123126
private boolean metadataSourcesAccessed = false;
124127

125128
@Nullable
@@ -358,14 +361,28 @@ public void setBootstrapExecutor(AsyncTaskExecutor bootstrapExecutor) {
358361
this.bootstrapExecutor = bootstrapExecutor;
359362
}
360363

364+
/**
365+
* Specify one or more Hibernate {@link Integrator} implementations to apply.
366+
* <p>This will only be applied for an internally built {@link MetadataSources}
367+
* instance. {@link #setMetadataSources} effectively overrides such settings,
368+
* with integrators to be applied to the externally built {@link MetadataSources}.
369+
* @since 5.1
370+
* @see #setMetadataSources
371+
* @see BootstrapServiceRegistryBuilder#applyIntegrator
372+
*/
373+
public void setHibernateIntegrators(Integrator... hibernateIntegrators) {
374+
this.hibernateIntegrators = hibernateIntegrators;
375+
}
376+
361377
/**
362378
* Specify a Hibernate {@link MetadataSources} service to use (e.g. reusing an
363379
* existing one), potentially populated with a custom Hibernate bootstrap
364380
* {@link org.hibernate.service.ServiceRegistry} as well.
365381
* @since 4.3
382+
* @see MetadataSources#MetadataSources(ServiceRegistry)
383+
* @see BootstrapServiceRegistryBuilder#build()
366384
*/
367385
public void setMetadataSources(MetadataSources metadataSources) {
368-
Assert.notNull(metadataSources, "MetadataSources must not be null");
369386
this.metadataSourcesAccessed = true;
370387
this.metadataSources = metadataSources;
371388
}
@@ -385,6 +402,11 @@ public MetadataSources getMetadataSources() {
385402
if (this.resourcePatternResolver != null) {
386403
builder = builder.applyClassLoader(this.resourcePatternResolver.getClassLoader());
387404
}
405+
if (this.hibernateIntegrators != null) {
406+
for (Integrator integrator : this.hibernateIntegrators) {
407+
builder = builder.applyIntegrator(integrator);
408+
}
409+
}
388410
this.metadataSources = new MetadataSources(builder.build());
389411
}
390412
return this.metadataSources;

spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
7272
* Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
7373
*
74-
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2, as of Spring 4.3.
74+
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
7575
*
7676
* @author Juergen Hoeller
7777
* @since 4.2
@@ -136,7 +136,9 @@ public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoade
136136
* @param metadataSources the Hibernate MetadataSources service to use (e.g. reusing an existing one)
137137
* @since 4.3
138138
*/
139-
public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) {
139+
public LocalSessionFactoryBuilder(
140+
@Nullable DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) {
141+
140142
super(metadataSources);
141143

142144
getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());

0 commit comments

Comments
 (0)