Skip to content

Commit 00474ce

Browse files
committed
Introduced setCacheRegionFactory method on LocalSessionFactoryBuilder/Bean
Issue: SPR-11056
1 parent 13ed423 commit 00474ce

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.hibernate.Interceptor;
2525
import org.hibernate.SessionFactory;
26+
import org.hibernate.cache.spi.RegionFactory;
2627
import org.hibernate.cfg.Configuration;
2728
import org.hibernate.cfg.NamingStrategy;
2829

@@ -88,6 +89,8 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
8889

8990
private Object currentTenantIdentifierResolver;
9091

92+
private RegionFactory cacheRegionFactory;
93+
9194
private Properties hibernateProperties;
9295

9396
private Class<?>[] annotatedClasses;
@@ -246,6 +249,18 @@ public void setCurrentTenantIdentifierResolver(Object currentTenantIdentifierRes
246249
this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
247250
}
248251

252+
/**
253+
* Set the Hibernate RegionFactory to use for the SessionFactory.
254+
* Allows for using a Spring-managed RegionFactory instance.
255+
* <p>Note: If this is set, the Hibernate settings should not define a
256+
* cache provider to avoid meaningless double configuration.
257+
* @see org.hibernate.cache.spi.RegionFactory
258+
* @see LocalSessionFactoryBuilder#setCacheRegionFactory
259+
*/
260+
public void setCacheRegionFactory(RegionFactory cacheRegionFactory) {
261+
this.cacheRegionFactory = cacheRegionFactory;
262+
}
263+
249264
/**
250265
* Set Hibernate properties, such as "hibernate.dialect".
251266
* <p>Note: Do not specify a transaction provider here when using
@@ -372,6 +387,10 @@ public void afterPropertiesSet() throws IOException {
372387
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
373388
}
374389

390+
if (this.cacheRegionFactory != null) {
391+
sfb.setCacheRegionFactory(this.cacheRegionFactory);
392+
}
393+
375394
if (this.hibernateProperties != null) {
376395
sfb.addProperties(this.hibernateProperties);
377396
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.io.IOException;
2020
import java.lang.annotation.Annotation;
21+
import java.lang.reflect.Method;
2122
import java.util.LinkedHashSet;
23+
import java.util.Properties;
2224
import java.util.Set;
2325
import javax.persistence.Embeddable;
2426
import javax.persistence.Entity;
@@ -29,10 +31,13 @@
2931
import org.hibernate.HibernateException;
3032
import org.hibernate.MappingException;
3133
import org.hibernate.SessionFactory;
34+
import org.hibernate.cache.spi.RegionFactory;
3235
import org.hibernate.cfg.AvailableSettings;
3336
import org.hibernate.cfg.Configuration;
3437
import org.hibernate.cfg.Environment;
38+
import org.hibernate.cfg.Settings;
3539
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
40+
import org.hibernate.service.ServiceRegistry;
3641

3742
import org.springframework.core.io.Resource;
3843
import org.springframework.core.io.ResourceLoader;
@@ -97,6 +102,8 @@ public class LocalSessionFactoryBuilder extends Configuration {
97102

98103
private final ResourcePatternResolver resourcePatternResolver;
99104

105+
private RegionFactory cacheRegionFactory;
106+
100107

101108
/**
102109
* Create a new LocalSessionFactoryBuilder for the given DataSource.
@@ -199,6 +206,18 @@ public LocalSessionFactoryBuilder setCurrentTenantIdentifierResolver(Object curr
199206
return this;
200207
}
201208

209+
/**
210+
* Set the Hibernate RegionFactory to use for the SessionFactory.
211+
* Allows for using a Spring-managed RegionFactory instance.
212+
* <p>Note: If this is set, the Hibernate settings should not define a
213+
* cache provider to avoid meaningless double configuration.
214+
* @see org.hibernate.cache.spi.RegionFactory
215+
*/
216+
public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory) {
217+
this.cacheRegionFactory = cacheRegionFactory;
218+
return this;
219+
}
220+
202221
/**
203222
* Add the given annotated classes in a batch.
204223
* @see #addAnnotatedClass
@@ -273,6 +292,24 @@ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFac
273292
}
274293

275294

295+
// Overridden methods from Hibernate's Configuration class
296+
297+
@Override
298+
public Settings buildSettings(Properties props, ServiceRegistry serviceRegistry) throws HibernateException {
299+
Settings settings = super.buildSettings(props, serviceRegistry);
300+
if (this.cacheRegionFactory != null) {
301+
try {
302+
Method setRegionFactory = Settings.class.getDeclaredMethod("setRegionFactory", RegionFactory.class);
303+
setRegionFactory.setAccessible(true);
304+
setRegionFactory.invoke(settings, this.cacheRegionFactory);
305+
}
306+
catch (Exception ex) {
307+
throw new IllegalStateException("Failed to invoke Hibernate's setRegionFactory method", ex);
308+
}
309+
}
310+
return settings;
311+
}
312+
276313
/**
277314
* Build the {@code SessionFactory}.
278315
*/

0 commit comments

Comments
 (0)