Skip to content

Commit 542b5b2

Browse files
committed
First-class support for MultiTenantConnectionProvider and CurrentTenantIdentifierResolver
Uses Object-typed setter methods to bridge between package location changes in Hibernate 4.2 vs 4.3. As a side bonus, those setters accept Class and class names as well. Issue: SPR-10823
1 parent a9e727c commit 542b5b2

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
lines changed

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

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* then be passed to Hibernate-based data access objects via dependency injection.
4545
*
4646
* <p><b>NOTE:</b> This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher.
47+
* As of Spring 4.0, it is compatible with (the quite refactored) Hibernate 4.3 as well.
4748
* It is similar in role to the same-named class in the {@code orm.hibernate3} package.
4849
* However, in practice, it is closer to {@code AnnotationSessionFactoryBean} since
4950
* its core purpose is to bootstrap a {@code SessionFactory} from annotation scanning.
@@ -81,6 +82,12 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
8182

8283
private NamingStrategy namingStrategy;
8384

85+
private Object jtaTransactionManager;
86+
87+
private Object multiTenantConnectionProvider;
88+
89+
private Object currentTenantIdentifierResolver;
90+
8491
private Properties hibernateProperties;
8592

8693
private Class<?>[] annotatedClasses;
@@ -89,8 +96,6 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
8996

9097
private String[] packagesToScan;
9198

92-
private Object jtaTransactionManager;
93-
9499
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
95100

96101
private Configuration configuration;
@@ -126,7 +131,7 @@ public void setConfigLocation(Resource configLocation) {
126131
* resources are specified locally via this bean.
127132
* @see org.hibernate.cfg.Configuration#configure(java.net.URL)
128133
*/
129-
public void setConfigLocations(Resource[] configLocations) {
134+
public void setConfigLocations(Resource... configLocations) {
130135
this.configLocations = configLocations;
131136
}
132137

@@ -140,7 +145,7 @@ public void setConfigLocations(Resource[] configLocations) {
140145
* @see #setMappingLocations
141146
* @see org.hibernate.cfg.Configuration#addResource
142147
*/
143-
public void setMappingResources(String[] mappingResources) {
148+
public void setMappingResources(String... mappingResources) {
144149
this.mappingResources = mappingResources;
145150
}
146151

@@ -153,7 +158,7 @@ public void setMappingResources(String[] mappingResources) {
153158
* or to specify all mappings locally.
154159
* @see org.hibernate.cfg.Configuration#addInputStream
155160
*/
156-
public void setMappingLocations(Resource[] mappingLocations) {
161+
public void setMappingLocations(Resource... mappingLocations) {
157162
this.mappingLocations = mappingLocations;
158163
}
159164

@@ -166,7 +171,7 @@ public void setMappingLocations(Resource[] mappingLocations) {
166171
* or to specify all mappings locally.
167172
* @see org.hibernate.cfg.Configuration#addCacheableFile(java.io.File)
168173
*/
169-
public void setCacheableMappingLocations(Resource[] cacheableMappingLocations) {
174+
public void setCacheableMappingLocations(Resource... cacheableMappingLocations) {
170175
this.cacheableMappingLocations = cacheableMappingLocations;
171176
}
172177

@@ -177,7 +182,7 @@ public void setCacheableMappingLocations(Resource[] cacheableMappingLocations) {
177182
* or to specify all mappings locally.
178183
* @see org.hibernate.cfg.Configuration#addJar(java.io.File)
179184
*/
180-
public void setMappingJarLocations(Resource[] mappingJarLocations) {
185+
public void setMappingJarLocations(Resource... mappingJarLocations) {
181186
this.mappingJarLocations = mappingJarLocations;
182187
}
183188

@@ -188,7 +193,7 @@ public void setMappingJarLocations(Resource[] mappingJarLocations) {
188193
* or to specify all mappings locally.
189194
* @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
190195
*/
191-
public void setMappingDirectoryLocations(Resource[] mappingDirectoryLocations) {
196+
public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {
192197
this.mappingDirectoryLocations = mappingDirectoryLocations;
193198
}
194199

@@ -211,6 +216,36 @@ public void setNamingStrategy(NamingStrategy namingStrategy) {
211216
this.namingStrategy = namingStrategy;
212217
}
213218

219+
/**
220+
* Set the Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
221+
* or the JTA {@link javax.transaction.TransactionManager} to be used with Hibernate,
222+
* if any. Implicitly sets up {@code JtaPlatform} and {@code CMTTransactionStrategy}.
223+
* @see LocalSessionFactoryBuilder#setJtaTransactionManager
224+
*/
225+
public void setJtaTransactionManager(Object jtaTransactionManager) {
226+
this.jtaTransactionManager = jtaTransactionManager;
227+
}
228+
229+
/**
230+
* Set a Hibernate 4.1/4.2/4.3 {@code MultiTenantConnectionProvider} to be passed
231+
* on to the SessionFactory: as an instance, a Class, or a String class name.
232+
* <p>Note that the package location of the {@code MultiTenantConnectionProvider}
233+
* interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
234+
* @see LocalSessionFactoryBuilder#setMultiTenantConnectionProvider
235+
*/
236+
public void setMultiTenantConnectionProvider(Object multiTenantConnectionProvider) {
237+
this.multiTenantConnectionProvider = multiTenantConnectionProvider;
238+
}
239+
240+
/**
241+
* Set a Hibernate 4.1/4.2/4.3 {@code CurrentTenantIdentifierResolver} to be passed
242+
* on to the SessionFactory: as an instance, a Class, or a String class name.
243+
* @see LocalSessionFactoryBuilder#setCurrentTenantIdentifierResolver
244+
*/
245+
public void setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver) {
246+
this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
247+
}
248+
214249
/**
215250
* Set Hibernate properties, such as "hibernate.dialect".
216251
* <p>Note: Do not specify a transaction provider here when using
@@ -237,7 +272,7 @@ public Properties getHibernateProperties() {
237272
* Specify annotated entity classes to register with this Hibernate SessionFactory.
238273
* @see org.hibernate.cfg.Configuration#addAnnotatedClass(Class)
239274
*/
240-
public void setAnnotatedClasses(Class<?>[] annotatedClasses) {
275+
public void setAnnotatedClasses(Class<?>... annotatedClasses) {
241276
this.annotatedClasses = annotatedClasses;
242277
}
243278

@@ -246,7 +281,7 @@ public void setAnnotatedClasses(Class<?>[] annotatedClasses) {
246281
* annotation metadata will be read.
247282
* @see org.hibernate.cfg.Configuration#addPackage(String)
248283
*/
249-
public void setAnnotatedPackages(String[] annotatedPackages) {
284+
public void setAnnotatedPackages(String... annotatedPackages) {
250285
this.annotatedPackages = annotatedPackages;
251286
}
252287

@@ -259,16 +294,6 @@ public void setPackagesToScan(String... packagesToScan) {
259294
this.packagesToScan = packagesToScan;
260295
}
261296

262-
/**
263-
* Set the Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
264-
* or the JTA {@link javax.transaction.TransactionManager} to be used with Hibernate,
265-
* if any.
266-
* @see LocalSessionFactoryBuilder#setJtaTransactionManager
267-
*/
268-
public void setJtaTransactionManager(Object jtaTransactionManager) {
269-
this.jtaTransactionManager = jtaTransactionManager;
270-
}
271-
272297
@Override
273298
public void setResourceLoader(ResourceLoader resourceLoader) {
274299
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
@@ -335,6 +360,18 @@ public void afterPropertiesSet() throws IOException {
335360
sfb.setNamingStrategy(this.namingStrategy);
336361
}
337362

363+
if (this.jtaTransactionManager != null) {
364+
sfb.setJtaTransactionManager(this.jtaTransactionManager);
365+
}
366+
367+
if (this.multiTenantConnectionProvider != null) {
368+
sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
369+
}
370+
371+
if (this.currentTenantIdentifierResolver != null) {
372+
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
373+
}
374+
338375
if (this.hibernateProperties != null) {
339376
sfb.addProperties(this.hibernateProperties);
340377
}
@@ -351,10 +388,6 @@ public void afterPropertiesSet() throws IOException {
351388
sfb.scanPackages(this.packagesToScan);
352389
}
353390

354-
if (this.jtaTransactionManager != null) {
355-
sfb.setJtaTransactionManager(this.jtaTransactionManager);
356-
}
357-
358391
// Build SessionFactory instance.
359392
this.configuration = sfb;
360393
this.sessionFactory = buildSessionFactory(sfb);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
5757
* Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
5858
*
59+
* <p>Requires Hibernate 4.0 or higher. As of Spring 4.0, it is compatible with
60+
* (the quite refactored) Hibernate 4.3 as well.
61+
*
5962
* <p><b>NOTE:</b> To set up Hibernate 4 for Spring-driven JTA transactions, make
6063
* sure to either use the {@link #setJtaTransactionManager} method or to set the
6164
* "hibernate.transaction.factory_class" property to {@link CMTTransactionFactory}.
@@ -174,6 +177,28 @@ else if (jtaTransactionManager instanceof TransactionManager) {
174177
return this;
175178
}
176179

180+
/**
181+
* Set a Hibernate 4.1/4.2/4.3 {@code MultiTenantConnectionProvider} to be passed
182+
* on to the SessionFactory: as an instance, a Class, or a String class name.
183+
* <p>Note that the package location of the {@code MultiTenantConnectionProvider}
184+
* interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
185+
* @see AvailableSettings#MULTI_TENANT_CONNECTION_PROVIDER
186+
*/
187+
public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(Object multiTenantConnectionProvider) {
188+
getProperties().put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
189+
return this;
190+
}
191+
192+
/**
193+
* Set a Hibernate 4.1/4.2/4.3 {@code CurrentTenantIdentifierResolver} to be passed
194+
* on to the SessionFactory: as an instance, a Class, or a String class name.
195+
* @see AvailableSettings#MULTI_TENANT_IDENTIFIER_RESOLVER
196+
*/
197+
public LocalSessionFactoryBuilder setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver) {
198+
getProperties().put(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);
199+
return this;
200+
}
201+
177202
/**
178203
* Add the given annotated classes in a batch.
179204
* @see #addAnnotatedClass

0 commit comments

Comments
 (0)