Skip to content

Commit

Permalink
First-class support for MultiTenantConnectionProvider and CurrentTena…
Browse files Browse the repository at this point in the history
…ntIdentifierResolver

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
  • Loading branch information
jhoeller committed Sep 3, 2013
1 parent a9e727c commit 542b5b2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* then be passed to Hibernate-based data access objects via dependency injection.
*
* <p><b>NOTE:</b> This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher.
* As of Spring 4.0, it is compatible with (the quite refactored) Hibernate 4.3 as well.
* It is similar in role to the same-named class in the {@code orm.hibernate3} package.
* However, in practice, it is closer to {@code AnnotationSessionFactoryBean} since
* its core purpose is to bootstrap a {@code SessionFactory} from annotation scanning.
Expand Down Expand Up @@ -81,6 +82,12 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator

private NamingStrategy namingStrategy;

private Object jtaTransactionManager;

private Object multiTenantConnectionProvider;

private Object currentTenantIdentifierResolver;

private Properties hibernateProperties;

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

private String[] packagesToScan;

private Object jtaTransactionManager;

private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

private Configuration configuration;
Expand Down Expand Up @@ -126,7 +131,7 @@ public void setConfigLocation(Resource configLocation) {
* resources are specified locally via this bean.
* @see org.hibernate.cfg.Configuration#configure(java.net.URL)
*/
public void setConfigLocations(Resource[] configLocations) {
public void setConfigLocations(Resource... configLocations) {
this.configLocations = configLocations;
}

Expand All @@ -140,7 +145,7 @@ public void setConfigLocations(Resource[] configLocations) {
* @see #setMappingLocations
* @see org.hibernate.cfg.Configuration#addResource
*/
public void setMappingResources(String[] mappingResources) {
public void setMappingResources(String... mappingResources) {
this.mappingResources = mappingResources;
}

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

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

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

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

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

/**
* Set the Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
* or the JTA {@link javax.transaction.TransactionManager} to be used with Hibernate,
* if any. Implicitly sets up {@code JtaPlatform} and {@code CMTTransactionStrategy}.
* @see LocalSessionFactoryBuilder#setJtaTransactionManager
*/
public void setJtaTransactionManager(Object jtaTransactionManager) {
this.jtaTransactionManager = jtaTransactionManager;
}

/**
* Set a Hibernate 4.1/4.2/4.3 {@code MultiTenantConnectionProvider} to be passed
* on to the SessionFactory: as an instance, a Class, or a String class name.
* <p>Note that the package location of the {@code MultiTenantConnectionProvider}
* interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
* @see LocalSessionFactoryBuilder#setMultiTenantConnectionProvider
*/
public void setMultiTenantConnectionProvider(Object multiTenantConnectionProvider) {
this.multiTenantConnectionProvider = multiTenantConnectionProvider;
}

/**
* Set a Hibernate 4.1/4.2/4.3 {@code CurrentTenantIdentifierResolver} to be passed
* on to the SessionFactory: as an instance, a Class, or a String class name.
* @see LocalSessionFactoryBuilder#setCurrentTenantIdentifierResolver
*/
public void setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver) {
this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
}

/**
* Set Hibernate properties, such as "hibernate.dialect".
* <p>Note: Do not specify a transaction provider here when using
Expand All @@ -237,7 +272,7 @@ public Properties getHibernateProperties() {
* Specify annotated entity classes to register with this Hibernate SessionFactory.
* @see org.hibernate.cfg.Configuration#addAnnotatedClass(Class)
*/
public void setAnnotatedClasses(Class<?>[] annotatedClasses) {
public void setAnnotatedClasses(Class<?>... annotatedClasses) {
this.annotatedClasses = annotatedClasses;
}

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

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

/**
* Set the Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
* or the JTA {@link javax.transaction.TransactionManager} to be used with Hibernate,
* if any.
* @see LocalSessionFactoryBuilder#setJtaTransactionManager
*/
public void setJtaTransactionManager(Object jtaTransactionManager) {
this.jtaTransactionManager = jtaTransactionManager;
}

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
Expand Down Expand Up @@ -335,6 +360,18 @@ public void afterPropertiesSet() throws IOException {
sfb.setNamingStrategy(this.namingStrategy);
}

if (this.jtaTransactionManager != null) {
sfb.setJtaTransactionManager(this.jtaTransactionManager);
}

if (this.multiTenantConnectionProvider != null) {
sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
}

if (this.currentTenantIdentifierResolver != null) {
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
}

if (this.hibernateProperties != null) {
sfb.addProperties(this.hibernateProperties);
}
Expand All @@ -351,10 +388,6 @@ public void afterPropertiesSet() throws IOException {
sfb.scanPackages(this.packagesToScan);
}

if (this.jtaTransactionManager != null) {
sfb.setJtaTransactionManager(this.jtaTransactionManager);
}

// Build SessionFactory instance.
this.configuration = sfb;
this.sessionFactory = buildSessionFactory(sfb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
* Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
*
* <p>Requires Hibernate 4.0 or higher. As of Spring 4.0, it is compatible with
* (the quite refactored) Hibernate 4.3 as well.
*
* <p><b>NOTE:</b> To set up Hibernate 4 for Spring-driven JTA transactions, make
* sure to either use the {@link #setJtaTransactionManager} method or to set the
* "hibernate.transaction.factory_class" property to {@link CMTTransactionFactory}.
Expand Down Expand Up @@ -174,6 +177,28 @@ else if (jtaTransactionManager instanceof TransactionManager) {
return this;
}

/**
* Set a Hibernate 4.1/4.2/4.3 {@code MultiTenantConnectionProvider} to be passed
* on to the SessionFactory: as an instance, a Class, or a String class name.
* <p>Note that the package location of the {@code MultiTenantConnectionProvider}
* interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
* @see AvailableSettings#MULTI_TENANT_CONNECTION_PROVIDER
*/
public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(Object multiTenantConnectionProvider) {
getProperties().put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
return this;
}

/**
* Set a Hibernate 4.1/4.2/4.3 {@code CurrentTenantIdentifierResolver} to be passed
* on to the SessionFactory: as an instance, a Class, or a String class name.
* @see AvailableSettings#MULTI_TENANT_IDENTIFIER_RESOLVER
*/
public LocalSessionFactoryBuilder setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver) {
getProperties().put(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);
return this;
}

/**
* Add the given annotated classes in a batch.
* @see #addAnnotatedClass
Expand Down

0 comments on commit 542b5b2

Please sign in to comment.