44
44
* then be passed to Hibernate-based data access objects via dependency injection.
45
45
*
46
46
* <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.
47
48
* It is similar in role to the same-named class in the {@code orm.hibernate3} package.
48
49
* However, in practice, it is closer to {@code AnnotationSessionFactoryBean} since
49
50
* its core purpose is to bootstrap a {@code SessionFactory} from annotation scanning.
@@ -81,6 +82,12 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
81
82
82
83
private NamingStrategy namingStrategy ;
83
84
85
+ private Object jtaTransactionManager ;
86
+
87
+ private Object multiTenantConnectionProvider ;
88
+
89
+ private Object currentTenantIdentifierResolver ;
90
+
84
91
private Properties hibernateProperties ;
85
92
86
93
private Class <?>[] annotatedClasses ;
@@ -89,8 +96,6 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
89
96
90
97
private String [] packagesToScan ;
91
98
92
- private Object jtaTransactionManager ;
93
-
94
99
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver ();
95
100
96
101
private Configuration configuration ;
@@ -126,7 +131,7 @@ public void setConfigLocation(Resource configLocation) {
126
131
* resources are specified locally via this bean.
127
132
* @see org.hibernate.cfg.Configuration#configure(java.net.URL)
128
133
*/
129
- public void setConfigLocations (Resource [] configLocations ) {
134
+ public void setConfigLocations (Resource ... configLocations ) {
130
135
this .configLocations = configLocations ;
131
136
}
132
137
@@ -140,7 +145,7 @@ public void setConfigLocations(Resource[] configLocations) {
140
145
* @see #setMappingLocations
141
146
* @see org.hibernate.cfg.Configuration#addResource
142
147
*/
143
- public void setMappingResources (String [] mappingResources ) {
148
+ public void setMappingResources (String ... mappingResources ) {
144
149
this .mappingResources = mappingResources ;
145
150
}
146
151
@@ -153,7 +158,7 @@ public void setMappingResources(String[] mappingResources) {
153
158
* or to specify all mappings locally.
154
159
* @see org.hibernate.cfg.Configuration#addInputStream
155
160
*/
156
- public void setMappingLocations (Resource [] mappingLocations ) {
161
+ public void setMappingLocations (Resource ... mappingLocations ) {
157
162
this .mappingLocations = mappingLocations ;
158
163
}
159
164
@@ -166,7 +171,7 @@ public void setMappingLocations(Resource[] mappingLocations) {
166
171
* or to specify all mappings locally.
167
172
* @see org.hibernate.cfg.Configuration#addCacheableFile(java.io.File)
168
173
*/
169
- public void setCacheableMappingLocations (Resource [] cacheableMappingLocations ) {
174
+ public void setCacheableMappingLocations (Resource ... cacheableMappingLocations ) {
170
175
this .cacheableMappingLocations = cacheableMappingLocations ;
171
176
}
172
177
@@ -177,7 +182,7 @@ public void setCacheableMappingLocations(Resource[] cacheableMappingLocations) {
177
182
* or to specify all mappings locally.
178
183
* @see org.hibernate.cfg.Configuration#addJar(java.io.File)
179
184
*/
180
- public void setMappingJarLocations (Resource [] mappingJarLocations ) {
185
+ public void setMappingJarLocations (Resource ... mappingJarLocations ) {
181
186
this .mappingJarLocations = mappingJarLocations ;
182
187
}
183
188
@@ -188,7 +193,7 @@ public void setMappingJarLocations(Resource[] mappingJarLocations) {
188
193
* or to specify all mappings locally.
189
194
* @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
190
195
*/
191
- public void setMappingDirectoryLocations (Resource [] mappingDirectoryLocations ) {
196
+ public void setMappingDirectoryLocations (Resource ... mappingDirectoryLocations ) {
192
197
this .mappingDirectoryLocations = mappingDirectoryLocations ;
193
198
}
194
199
@@ -211,6 +216,36 @@ public void setNamingStrategy(NamingStrategy namingStrategy) {
211
216
this .namingStrategy = namingStrategy ;
212
217
}
213
218
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
+
214
249
/**
215
250
* Set Hibernate properties, such as "hibernate.dialect".
216
251
* <p>Note: Do not specify a transaction provider here when using
@@ -237,7 +272,7 @@ public Properties getHibernateProperties() {
237
272
* Specify annotated entity classes to register with this Hibernate SessionFactory.
238
273
* @see org.hibernate.cfg.Configuration#addAnnotatedClass(Class)
239
274
*/
240
- public void setAnnotatedClasses (Class <?>[] annotatedClasses ) {
275
+ public void setAnnotatedClasses (Class <?>... annotatedClasses ) {
241
276
this .annotatedClasses = annotatedClasses ;
242
277
}
243
278
@@ -246,7 +281,7 @@ public void setAnnotatedClasses(Class<?>[] annotatedClasses) {
246
281
* annotation metadata will be read.
247
282
* @see org.hibernate.cfg.Configuration#addPackage(String)
248
283
*/
249
- public void setAnnotatedPackages (String [] annotatedPackages ) {
284
+ public void setAnnotatedPackages (String ... annotatedPackages ) {
250
285
this .annotatedPackages = annotatedPackages ;
251
286
}
252
287
@@ -259,16 +294,6 @@ public void setPackagesToScan(String... packagesToScan) {
259
294
this .packagesToScan = packagesToScan ;
260
295
}
261
296
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
-
272
297
@ Override
273
298
public void setResourceLoader (ResourceLoader resourceLoader ) {
274
299
this .resourcePatternResolver = ResourcePatternUtils .getResourcePatternResolver (resourceLoader );
@@ -335,6 +360,18 @@ public void afterPropertiesSet() throws IOException {
335
360
sfb .setNamingStrategy (this .namingStrategy );
336
361
}
337
362
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
+
338
375
if (this .hibernateProperties != null ) {
339
376
sfb .addProperties (this .hibernateProperties );
340
377
}
@@ -351,10 +388,6 @@ public void afterPropertiesSet() throws IOException {
351
388
sfb .scanPackages (this .packagesToScan );
352
389
}
353
390
354
- if (this .jtaTransactionManager != null ) {
355
- sfb .setJtaTransactionManager (this .jtaTransactionManager );
356
- }
357
-
358
391
// Build SessionFactory instance.
359
392
this .configuration = sfb ;
360
393
this .sessionFactory = buildSessionFactory (sfb );
0 commit comments