Hibernate MultiTenancy与spring jpa

时间:2017-06-05 09:51:58

标签: spring hibernate spring-boot spring-data-jpa

我想使用单独的模式将多个租户添加到我的应用程序中。我的应用程序基于spring jpa和hibernate。我实施了MultiTenantConnectionProviderCurrentTenantIdentifierResolver。 我的配置类是:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){

    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactory.setPackagesToScan("com.**.api.entity");
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(hibernateJpaVendorAdapter);
    Properties jpaProperties = new Properties();
    jpaProperties.put("hibernate.globally_quoted_identifiers",true);
    jpaProperties.put("hibernate.dialect",org.hibernate.dialect.MySQL5Dialect.class);
    jpaProperties.put("hibernate.multi_tenant_connection_provider",multiTenantConnectionProvider);
    jpaProperties.put("hibernate.tenant_identifier_resolver",currentTenantIdentifierResolver);
    jpaProperties.put("hibernate.multiTenancy","SCHEMA");
    entityManagerFactory.setJpaProperties(jpaProperties);

    return entityManagerFactory;
}

我使用MapDataSourceLookup来保存dataSources.It工作。但这是一个小问题。我必须分配packagesToScan。我希望它是一个基本服务。一些应用程序依赖它。似乎分配packagesToScan不是一个好习惯。

有没有更好的方法呢?

0 个答案:

没有答案