Skip to content

Commit 93d9121

Browse files
committed
Support for "cacheRegionFactory" injection with Hibernate 5
Issue: SPR-17043
1 parent 333ec74 commit 93d9121

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

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

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
2828
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
2929
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
30+
import org.hibernate.cache.spi.RegionFactory;
3031
import org.hibernate.cfg.Configuration;
3132
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
3233
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
@@ -109,17 +110,20 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
109110
@Nullable
110111
private Object jtaTransactionManager;
111112

113+
@Nullable
114+
private RegionFactory cacheRegionFactory;
115+
112116
@Nullable
113117
private MultiTenantConnectionProvider multiTenantConnectionProvider;
114118

115119
@Nullable
116120
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;
117121

118122
@Nullable
119-
private TypeFilter[] entityTypeFilters;
123+
private Properties hibernateProperties;
120124

121125
@Nullable
122-
private Properties hibernateProperties;
126+
private TypeFilter[] entityTypeFilters;
123127

124128
@Nullable
125129
private Class<?>[] annotatedClasses;
@@ -259,15 +263,15 @@ public void setEntityInterceptor(Interceptor entityInterceptor) {
259263
}
260264

261265
/**
262-
* Set a Hibernate 5.0 ImplicitNamingStrategy for the SessionFactory.
266+
* Set a Hibernate 5 {@link ImplicitNamingStrategy} for the SessionFactory.
263267
* @see Configuration#setImplicitNamingStrategy
264268
*/
265269
public void setImplicitNamingStrategy(ImplicitNamingStrategy implicitNamingStrategy) {
266270
this.implicitNamingStrategy = implicitNamingStrategy;
267271
}
268272

269273
/**
270-
* Set a Hibernate 5.0 PhysicalNamingStrategy for the SessionFactory.
274+
* Set a Hibernate 5 {@link PhysicalNamingStrategy} for the SessionFactory.
271275
* @see Configuration#setPhysicalNamingStrategy
272276
*/
273277
public void setPhysicalNamingStrategy(PhysicalNamingStrategy physicalNamingStrategy) {
@@ -284,6 +288,18 @@ public void setJtaTransactionManager(Object jtaTransactionManager) {
284288
this.jtaTransactionManager = jtaTransactionManager;
285289
}
286290

291+
/**
292+
* Set the Hibernate {@link RegionFactory} to use for the SessionFactory.
293+
* Allows for using a Spring-managed {@code RegionFactory} instance.
294+
* <p>Note: If this is set, the Hibernate settings should not define a
295+
* cache provider to avoid meaningless double configuration.
296+
* @since 5.1
297+
* @see LocalSessionFactoryBuilder#setCacheRegionFactory
298+
*/
299+
public void setCacheRegionFactory(RegionFactory cacheRegionFactory) {
300+
this.cacheRegionFactory = cacheRegionFactory;
301+
}
302+
287303
/**
288304
* Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory.
289305
* @since 4.3
@@ -301,17 +317,6 @@ public void setCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver c
301317
this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
302318
}
303319

304-
/**
305-
* Specify custom type filters for Spring-based scanning for entity classes.
306-
* <p>Default is to search all specified packages for classes annotated with
307-
* {@code @javax.persistence.Entity}, {@code @javax.persistence.Embeddable}
308-
* or {@code @javax.persistence.MappedSuperclass}.
309-
* @see #setPackagesToScan
310-
*/
311-
public void setEntityTypeFilters(TypeFilter... entityTypeFilters) {
312-
this.entityTypeFilters = entityTypeFilters;
313-
}
314-
315320
/**
316321
* Set Hibernate properties, such as "hibernate.dialect".
317322
* <p>Note: Do not specify a transaction provider here when using
@@ -334,6 +339,17 @@ public Properties getHibernateProperties() {
334339
return this.hibernateProperties;
335340
}
336341

342+
/**
343+
* Specify custom type filters for Spring-based scanning for entity classes.
344+
* <p>Default is to search all specified packages for classes annotated with
345+
* {@code @javax.persistence.Entity}, {@code @javax.persistence.Embeddable}
346+
* or {@code @javax.persistence.MappedSuperclass}.
347+
* @see #setPackagesToScan
348+
*/
349+
public void setEntityTypeFilters(TypeFilter... entityTypeFilters) {
350+
this.entityTypeFilters = entityTypeFilters;
351+
}
352+
337353
/**
338354
* Specify annotated entity classes to register with this Hibernate SessionFactory.
339355
* @see Configuration#addAnnotatedClass(Class)
@@ -546,6 +562,10 @@ public void afterPropertiesSet() throws IOException {
546562
sfb.setBeanContainer(this.beanFactory);
547563
}
548564

565+
if (this.cacheRegionFactory != null) {
566+
sfb.setCacheRegionFactory(this.cacheRegionFactory);
567+
}
568+
549569
if (this.multiTenantConnectionProvider != null) {
550570
sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
551571
}
@@ -554,14 +574,14 @@ public void afterPropertiesSet() throws IOException {
554574
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
555575
}
556576

557-
if (this.entityTypeFilters != null) {
558-
sfb.setEntityTypeFilters(this.entityTypeFilters);
559-
}
560-
561577
if (this.hibernateProperties != null) {
562578
sfb.addProperties(this.hibernateProperties);
563579
}
564580

581+
if (this.entityTypeFilters != null) {
582+
sfb.setEntityTypeFilters(this.entityTypeFilters);
583+
}
584+
565585
if (this.annotatedClasses != null) {
566586
sfb.addAnnotatedClasses(this.annotatedClasses);
567587
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.hibernate.SessionFactory;
4141
import org.hibernate.boot.MetadataSources;
4242
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
43+
import org.hibernate.cache.spi.RegionFactory;
4344
import org.hibernate.cfg.AvailableSettings;
4445
import org.hibernate.cfg.Configuration;
4546
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
@@ -257,6 +258,19 @@ public LocalSessionFactoryBuilder setBeanContainer(ConfigurableListableBeanFacto
257258
return this;
258259
}
259260

261+
/**
262+
* Set the Hibernate {@link RegionFactory} to use for the SessionFactory.
263+
* Allows for using a Spring-managed {@code RegionFactory} instance.
264+
* <p>Note: If this is set, the Hibernate settings should not define a
265+
* cache provider to avoid meaningless double configuration.
266+
* @since 5.1
267+
* @see AvailableSettings#CACHE_REGION_FACTORY
268+
*/
269+
public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory) {
270+
getProperties().put(AvailableSettings.CACHE_REGION_FACTORY, cacheRegionFactory);
271+
return this;
272+
}
273+
260274
/**
261275
* Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory.
262276
* @since 4.3

0 commit comments

Comments
 (0)