我一直在尝试实现模式多租户(在同一dataSource上),但是模式不同。
我基于的指南是this article和this repo on github
问题是我正在使用较新的spring boot 2.0.3,并且也将其转换为其他模式,因此我需要使用属性hibernate.default_schema
,该属性将生成包含内联(当前)的已编译SQL。模式,public.tablename
:
..tipologia0_.status as status7_39_ from public.tipologia tipologia0..
我似乎在创建了配置类,该配置类创建了Bean LocalContainerEntityManagerFactoryBean
必需的工厂之后,又创建了一个,但是我没有运气尝试使用jpaProperties.put("hibernate.default_schema", jpaProperties.get("hibernate.cm_schema"));
设置每个租户LocalEntityManagerFactory(如果该属性为null,架构便消失了,但是由于我程序的复杂性,另一个功能坏了
关于如何更改(从HQL)内联编译模式的任何想法?
PS:我正在提供HibernateConfig类供参考:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, MultiTenantConnectionProvider multiTenantConnectionProvider,
CurrentTenantIdentifierResolver tenantIdentifierResolver) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource);
Map<String, Object> jpaProperties = remapHibernateProperties(customJpaProperties().getProperties());
/*for (Object lol : customJpaProperties().getProperties()) {
}*/
//jpaProperties.addAll())
jpaProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
jpaProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
jpaProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifierResolver);
/*if ( tenantIdentifierResolver.resolveCurrentTenantIdentifier() != jpaProperties.get("hibernate.cm_schema") ) {
TenantContext.setCurrentTenant("cm");
jpaProperties.put("hibernate.default_schema", jpaProperties.get("hibernate.cm_schema));
} else {
TenantContext.setCurrentTenant("public");
System.err.println("back to public");
}*/
System.err.println(jpaProperties.toString());
jpaProperties.put("hibernate.default_schema", null);// jpaProperties.get("hibernate.cm_schema"));
/*if (TenantContext.getCurrentTenant() == null) {
TenantContext.setCurrentTenant("cm");
} else {
jpaProperties.put("hibernate.default_schema", jpaProperties.get("hibernate.cm_schema"));
TenantContext.setCurrentTenant("public");
}*/
jpaProperties.put(Environment.FORMAT_SQL, true);
System.err.println("NOW I AM: "+TenantContext.getCurrentTenant() + " -> " +jpaProperties.get("hibernate.default_schema"));
//emf.setJpaVendorAdapter(this.jpaVendorAdapter());
emf.setJpaVendorAdapter(this.createJpaVendorAdapter(customJpaProperties()));
emf.setPackagesToScan("br.duayres.multitenancy.domain.entity");
emf.setPersistenceUnitName("defaultCM");//+tenantIdentifierResolver.resolveCurrentTenantIdentifier() );
emf.setJpaPropertyMap(changeProperties(customJpaProperties().getProperties()));
emf.afterPropertiesSet();
emf.setJpaPropertyMap(jpaProperties);
return emf;
}