我们有类似的东西:1..N数据库作为不同的客户。每个客户都有相同的架构,因此我们只有 1 个实体经理。因此,您需要通过 spi 2 实现类提供休眠,并通过属性进行 handen:
hibernate.multi_tenant_connection_provider
org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider
和hibernate.tenant_identifier_resolver org.hibernate.context.spi.CurrentTenantIdentifierResolver
“hibernate.multiTenancy”, “SCHEMA”
这是如何使用弹簧框架的示例
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setJpaVendorAdapter(jpaVendorAdapter());
emf.setPersistenceUnitName("security");
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.cache.use_second_level_cache", "true");
hibernateProperties.put("hibernate.multiTenancy", "SCHEMA");
// do not load all metadata from standard db speadup startup
hibernateProperties.put("hibernate.temp.use_jdbc_metadata_defaults", "false");
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
hibernateProperties.put("hibernate.multi_tenant_connection_provider", multiTenantConnectionProvider());
hibernateProperties.put("hibernate.tenant_identifier_resolver", currentTenantIdentifierResolver(tenant));
hibernateProperties.put("hibernate.show_sql", true);
emf.setJpaProperties(hibernateProperties);
emf.setJpaDialect(new HibernateJpaDialect());
LOG.info("LocalContainerEntityManagerFactoryBean bean created");
return emf;