我想出了解决我的问题的解决方案。我希望这对外面的人有用。
因此,主要问题归结为Hibernate的限制,即在多租户配置中在运行时为新客户端创建模式。
“ Hibernate在多租户环境中不支持自动模式导出。”
为了解决此限制(使用Spring),我的解决方案是创建一个新的LocalSessionFactoryBean,将其配置为不支持多租户。所以基本上我有两个LocalSessionFactoryBeans。
- 用于多租户会话的多租户LocalSessionFactoryBean
- 非多租户LocalSessionFactoryBean,用于使用spring文件中的配置为租户创建架构。
d簧配置
<!-- Multi-tenant SessionFactory --><bean id="sessionFactory" > <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" > <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>
用于创建架构的代码
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;}
请注意,此代码中的_configuration来自Non-Tenant LocalSessionFactoryBean
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)