Java 具有Hibernate二级缓存的Jhipster多租户

Java 具有Hibernate二级缓存的Jhipster多租户,java,hibernate,spring-boot,ehcache,jhipster,Java,Hibernate,Spring Boot,Ehcache,Jhipster,我一直在尝试将我的JHipster生成的应用程序转换成一个多租户应用程序,使用此博客文章作为基础 我遇到了二级缓存的问题。Spring boot似乎能够正确检测和设置: DatabaseConfiguration.java 我在application-dev.yml中定义了所有必需的属性 hibernate.cache.use_second_level_cache: true hibernate.cache.use_query_cache: false hibernate.generate_st

我一直在尝试将我的JHipster生成的应用程序转换成一个多租户应用程序,使用此博客文章作为基础

我遇到了二级缓存的问题。Spring boot似乎能够正确检测和设置:

DatabaseConfiguration.java

我在application-dev.yml中定义了所有必需的属性

hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
spring boot似乎正在为我的DatabaseConfiguration.java正确读取和使用它。我不明白为什么它没有检测到属性文件。如果我尝试通过设置禁用缓存:

hibernate.cache.use_second_level_cache: false
DatabaseConfiguration.java会相应地检测并执行操作,但MultitenacyJPA配置.java仍会引发相同的异常


我是否遗漏了一些明显的内容?

答案是在实体管理器上实际设置jpa属性值。我不知道我怎么会忽视这一点;我以为他们已经准备好了

首先,如果我没有弄错的话,我注入了主数据源和SpringBoot提供的JPAProperties对象

multi-tenacyjpaconfiguration.java

然后,我使用与DatabaseConfiguration.java中使用的相同方法设置值

multi-tenacyjpaconfiguration.java

@Bean(name=“租户管理者”)
公共LocalContainerEntityManagerFactoryBean entityManagerFactory(数据源数据源,
多租户连接提供程序connectionProvider,
CurrentTenantIdentifier解析程序tenantResolver){
LocalContainerEntityManagerFactoryBean emfBean=新的LocalContainerEntityManagerFactoryBean();
emfBean.setDataSource(数据源);
emfBean.setPackagesToScan(“com.quadrimular.nts.hemia.domain.tenant”);
setJpaVendorAdapter(jpaVendorAdapter());
映射属性=新的HashMap();
properties.put(org.hibernate.cfg.Environment.MULTI_-TENANT,MULTI-tenacysttrategy.DATABASE);
properties.put(org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER,connectionProvider);
properties.put(org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER,tenantResolver);
properties.put(“hibernate.ejb.naming_strategy”、“org.hibernate.cfg.ImprovedNamingStrategy”);
emfBean.setJpaPropertyMap(属性);
emfBean.setJPapProperties(AdditionalJPapProperties());
返回emfBean;
}
私有属性附加JPA属性(){
属性=新属性();
for(Map.Entry:jpaProperties.getHibernateProperties(dataSource.entrySet()){
properties.setProperty(entry.getKey(),entry.getValue());
}
归还财产;
}

使用additionalJpaProperties()方法获取我的主数据源的所有hibernate jpa属性。然后在硬编码属性之后设置hibernate属性映射属性。显然,这不是我计划从.yml文件中设置所有jpa值的最干净的解决方案。

听起来可能很愚蠢,但正如我看到的,您尝试过使用dev profile,您尝试过使用prod profile吗?某些类具有基于概要文件的条件批注。@Gaël已经尝试运行“快速概要文件”来禁用缓存,但没有成功。您刚刚尝试使用prod配置文件运行,但仍然得到完全相同的异常。问题中提供的链接已断开(404):(
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.use_second_level_cache: false
@Inject
private JpaProperties jpaProperties;

@Inject
private DataSource dataSource;
    @Bean(name = "tenantEntityManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
                                                           MultiTenantConnectionProvider connectionProvider,
                                                           CurrentTenantIdentifierResolver tenantResolver) {
        LocalContainerEntityManagerFactoryBean emfBean = new LocalContainerEntityManagerFactoryBean();
        emfBean.setDataSource(dataSource);
        emfBean.setPackagesToScan("com.quadrimular.nts.helium.domain.tenant");
        emfBean.setJpaVendorAdapter(jpaVendorAdapter());

        Map<String, Object> properties = new HashMap<>();
        properties.put(org.hibernate.cfg.Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
        properties.put(org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER, connectionProvider);
        properties.put(org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantResolver);
        properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");

        emfBean.setJpaPropertyMap(properties);

        emfBean.setJpaProperties(additionalJpaProperties());

        return emfBean;
    }

    private Properties additionalJpaProperties() {
        Properties properties = new Properties();
        for (Map.Entry<String, String> entry : jpaProperties.getHibernateProperties(dataSource).entrySet()) {
            properties.setProperty(entry.getKey(), entry.getValue());
        }
        return properties;
    }