Support for "cacheRegionFactory" injection with Hibernate 5
Issue: SPR-17043
This commit is contained in:
@@ -27,6 +27,7 @@ import org.hibernate.boot.MetadataSources;
|
|||||||
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
|
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
|
||||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
||||||
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cache.spi.RegionFactory;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
|
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
|
||||||
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
|
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
|
||||||
@@ -109,6 +110,9 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
@Nullable
|
@Nullable
|
||||||
private Object jtaTransactionManager;
|
private Object jtaTransactionManager;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private RegionFactory cacheRegionFactory;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private MultiTenantConnectionProvider multiTenantConnectionProvider;
|
private MultiTenantConnectionProvider multiTenantConnectionProvider;
|
||||||
|
|
||||||
@@ -116,10 +120,10 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;
|
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private TypeFilter[] entityTypeFilters;
|
private Properties hibernateProperties;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Properties hibernateProperties;
|
private TypeFilter[] entityTypeFilters;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Class<?>[] annotatedClasses;
|
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
|
* @see Configuration#setImplicitNamingStrategy
|
||||||
*/
|
*/
|
||||||
public void setImplicitNamingStrategy(ImplicitNamingStrategy implicitNamingStrategy) {
|
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
|
* @see Configuration#setPhysicalNamingStrategy
|
||||||
*/
|
*/
|
||||||
public void setPhysicalNamingStrategy(PhysicalNamingStrategy physicalNamingStrategy) {
|
public void setPhysicalNamingStrategy(PhysicalNamingStrategy physicalNamingStrategy) {
|
||||||
@@ -284,6 +288,18 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
this.jtaTransactionManager = jtaTransactionManager;
|
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.
|
* Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory.
|
||||||
* @since 4.3
|
* @since 4.3
|
||||||
@@ -301,17 +317,6 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
|
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".
|
* Set Hibernate properties, such as "hibernate.dialect".
|
||||||
* <p>Note: Do not specify a transaction provider here when using
|
* <p>Note: Do not specify a transaction provider here when using
|
||||||
@@ -334,6 +339,17 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
return this.hibernateProperties;
|
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.
|
* Specify annotated entity classes to register with this Hibernate SessionFactory.
|
||||||
* @see Configuration#addAnnotatedClass(Class)
|
* @see Configuration#addAnnotatedClass(Class)
|
||||||
@@ -546,6 +562,10 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
sfb.setBeanContainer(this.beanFactory);
|
sfb.setBeanContainer(this.beanFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.cacheRegionFactory != null) {
|
||||||
|
sfb.setCacheRegionFactory(this.cacheRegionFactory);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.multiTenantConnectionProvider != null) {
|
if (this.multiTenantConnectionProvider != null) {
|
||||||
sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
|
sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
|
||||||
}
|
}
|
||||||
@@ -554,14 +574,14 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
|||||||
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
|
sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.entityTypeFilters != null) {
|
|
||||||
sfb.setEntityTypeFilters(this.entityTypeFilters);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.hibernateProperties != null) {
|
if (this.hibernateProperties != null) {
|
||||||
sfb.addProperties(this.hibernateProperties);
|
sfb.addProperties(this.hibernateProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.entityTypeFilters != null) {
|
||||||
|
sfb.setEntityTypeFilters(this.entityTypeFilters);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.annotatedClasses != null) {
|
if (this.annotatedClasses != null) {
|
||||||
sfb.addAnnotatedClasses(this.annotatedClasses);
|
sfb.addAnnotatedClasses(this.annotatedClasses);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import org.hibernate.MappingException;
|
|||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cache.spi.RegionFactory;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
|
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
|
||||||
@@ -257,6 +258,19 @@ public class LocalSessionFactoryBuilder extends Configuration {
|
|||||||
return this;
|
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.
|
* Set a {@link MultiTenantConnectionProvider} to be passed on to the SessionFactory.
|
||||||
* @since 4.3
|
* @since 4.3
|
||||||
|
|||||||
Reference in New Issue
Block a user