Support for "cacheRegionFactory" injection with Hibernate 5

Issue: SPR-17043
This commit is contained in:
Juergen Hoeller 2018-07-16 13:03:46 +02:00
parent 333ec7400e
commit 93d91219fd
2 changed files with 53 additions and 19 deletions

View File

@ -27,6 +27,7 @@ import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
@ -109,6 +110,9 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
@Nullable
private Object jtaTransactionManager;
@Nullable
private RegionFactory cacheRegionFactory;
@Nullable
private MultiTenantConnectionProvider multiTenantConnectionProvider;
@ -116,10 +120,10 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;
@Nullable
private TypeFilter[] entityTypeFilters;
private Properties hibernateProperties;
@Nullable
private Properties hibernateProperties;
private TypeFilter[] entityTypeFilters;
@Nullable
private Class<?>[] annotatedClasses;
@ -259,7 +263,7 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
}
/**
* Set a Hibernate 5.0 ImplicitNamingStrategy for the SessionFactory.
* Set a Hibernate 5 {@link ImplicitNamingStrategy} for the SessionFactory.
* @see Configuration#setImplicitNamingStrategy
*/
public void setImplicitNamingStrategy(ImplicitNamingStrategy implicitNamingStrategy) {
@ -267,7 +271,7 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
}
/**
* Set a Hibernate 5.0 PhysicalNamingStrategy for the SessionFactory.
* Set a Hibernate 5 {@link PhysicalNamingStrategy} for the SessionFactory.
* @see Configuration#setPhysicalNamingStrategy
*/
public void setPhysicalNamingStrategy(PhysicalNamingStrategy physicalNamingStrategy) {
@ -284,6 +288,18 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
this.jtaTransactionManager = jtaTransactionManager;
}
/**
* Set the Hibernate {@link RegionFactory} to use for the SessionFactory.
* Allows for using a Spring-managed {@code RegionFactory} instance.
* <p>Note: If this is set, the Hibernate settings should not define a
* cache provider to avoid meaningless double configuration.
* @since 5.1
* @see LocalSessionFactoryBuilder#setCacheRegionFactory
*/
public void setCacheRegionFactory(RegionFactory cacheRegionFactory) {
this.cacheRegionFactory = cacheRegionFactory;
}
/**
* Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory.
* @since 4.3
@ -301,17 +317,6 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
}
/**
* Specify custom type filters for Spring-based scanning for entity classes.
* <p>Default is to search all specified packages for classes annotated with
* {@code @javax.persistence.Entity}, {@code @javax.persistence.Embeddable}
* or {@code @javax.persistence.MappedSuperclass}.
* @see #setPackagesToScan
*/
public void setEntityTypeFilters(TypeFilter... entityTypeFilters) {
this.entityTypeFilters = entityTypeFilters;
}
/**
* Set Hibernate properties, such as "hibernate.dialect".
* <p>Note: Do not specify a transaction provider here when using
@ -334,6 +339,17 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
return this.hibernateProperties;
}
/**
* Specify custom type filters for Spring-based scanning for entity classes.
* <p>Default is to search all specified packages for classes annotated with
* {@code @javax.persistence.Entity}, {@code @javax.persistence.Embeddable}
* or {@code @javax.persistence.MappedSuperclass}.
* @see #setPackagesToScan
*/
public void setEntityTypeFilters(TypeFilter... entityTypeFilters) {
this.entityTypeFilters = entityTypeFilters;
}
/**
* Specify annotated entity classes to register with this Hibernate SessionFactory.
* @see Configuration#addAnnotatedClass(Class)
@ -546,6 +562,10 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
sfb.setBeanContainer(this.beanFactory);
}
if (this.cacheRegionFactory != null) {
sfb.setCacheRegionFactory(this.cacheRegionFactory);
}
if (this.multiTenantConnectionProvider != null) {
sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
}
@ -554,14 +574,14 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
}
if (this.entityTypeFilters != null) {
sfb.setEntityTypeFilters(this.entityTypeFilters);
}
if (this.hibernateProperties != null) {
sfb.addProperties(this.hibernateProperties);
}
if (this.entityTypeFilters != null) {
sfb.setEntityTypeFilters(this.entityTypeFilters);
}
if (this.annotatedClasses != null) {
sfb.addAnnotatedClasses(this.annotatedClasses);
}

View File

@ -40,6 +40,7 @@ import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
@ -257,6 +258,19 @@ public class LocalSessionFactoryBuilder extends Configuration {
return this;
}
/**
* Set the Hibernate {@link RegionFactory} to use for the SessionFactory.
* Allows for using a Spring-managed {@code RegionFactory} instance.
* <p>Note: If this is set, the Hibernate settings should not define a
* cache provider to avoid meaningless double configuration.
* @since 5.1
* @see AvailableSettings#CACHE_REGION_FACTORY
*/
public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory) {
getProperties().put(AvailableSettings.CACHE_REGION_FACTORY, cacheRegionFactory);
return this;
}
/**
* Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory.
* @since 4.3