本文介绍了Hibernate多租户在运行时创建模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用hibernate 4和spring 4为java web应用程序设置多租户支持。默认架构在应用程序启动时创建并设置。当不试图支持多租户时,这种模式运行良好。
我现在需要做的是为每个创建帐户的新租户创建一个模式。该模式可以简单地作为通用模式的副本,因为它将遵循相同的格式。
如何在运行时创建遵循与默认模式相同格式的新模式?看起来,当LocalSessionFactoryBean被实例化时,默认模式被创建,因为那是我指定映射资源的地方。
用解决方案解决我的问题。我希望它对那里的人有用。
所以主要问题归结为Hibernate限制在多租户运行时为新客户创建模式配置。
My solution to get around this restriction (using Spring) was to create a new LocalSessionFactoryBean that is configured to NOT to support multi-tenancy.So basically I have two LocalSessionFactoryBeans.
- Multi-Tenant LocalSessionFactoryBean that is used for multi-tenant sessions
- Non Multi-Tenant LocalSessionFactoryBean that is used to create schemas for tenants using the configuration set in the spring file.
Spring configuration
<!-- Multi-tenant SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<map>
<entry key="hibernate.dialect" value="${hibernate.dialect}" />
<entry key="hibernate.hbm2ddl.auto" value="NONE" />
<!-- Multi-tenancy support -->
<entry key="hibernate.multiTenancy" value="SCHEMA" />
<entry key="hibernate.tenant_identifier_resolver" value="${hibernate.tenant_identifier_resolver}" />
<entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantConnectionProvider" />
</map>
</property>
<property name="mappingResources">
<list>
<COMMON SCHEMA MAPPING RESOURCES />
</list>
</property>
</bean>
<!-- SessionFactory capable of managing multi-tenant schemas -->
<bean id="sessionFactorySchemaManager"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<map>
<entry key="hibernate.dialect" value="${hibernate.dialect}" />
<entry key="hibernate.hbm2ddl.auto" value="CREATE" />
<!-- Multi-tenancy support -->
<entry key="hibernate.multiTenancy" value="NONE" />
</map>
</property>
<property name="mappingResources">
<list>
<TENANT SPECIFIC SCHEMA MAPPING RESOURCES />
</list>
</property>
</bean>
Code used to create the schema
public boolean createSchema(final String tenantId) throws SQLException {
boolean result = false;
if(_configuration != null && _dataSource != null) {
// Get a local configuration to configure
final Configuration tenantConfig = _configuration;
// Set the properties for this configuration
Properties props = new Properties();
props.put(Environment.DEFAULT_SCHEMA, tenantId);
tenantConfig.addProperties(props);
// Get connection
Connection connection = DriverManager.getConnection(_dataSource.getUrl(),
_dataSource.getUsername(), _dataSource.getPassword());
// Create the schema
connection.createStatement().execute("CREATE SCHEMA " + tenantId + "");
// Run the schema update from configuration
SchemaUpdate schemaUpdate = new SchemaUpdate(tenantConfig);
schemaUpdate.execute(true, true);
// Set the schema
connection.createStatement().execute("SET SCHEMA " + tenantId + "");
// Set the result
result = true;
} else if(_configuration == null) {
if(_LOGGER.isWarnEnabled()) {
_LOGGER.warn("No configuration was specified for " + getClass().getSimpleName());
}
} else if(_dataSource == null) {
if(_LOGGER.isWarnEnabled()) {
_LOGGER.warn("No dataSource was specified for " + getClass().getSimpleName());
}
}
return result;
}
Note that the _configuration in this code came from the Non Multi-Tenant LocalSessionFactoryBean
这篇关于Hibernate多租户在运行时创建模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!