【问题标题】:How to reconfigure LocalContainerEntityManagerFactoryBean in test如何在测试中重新配置 LocalContainerEntityManagerFactoryBean
【发布时间】:2019-08-09 16:23:07
【问题描述】:

如果我有一个以编程方式管理的 LocalContainerEntityManagerFactoryBean,例如带有 @Bean 注释的方法:

@Configuration
@EnableJpaRepositories(
  entityManagerFactoryRef = "myEntityManager",
  transactionManagerRef = "myTransactionManager",
  basePackages = {"com.mycompany.my.repository"}
)
public class MyDbConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean mcEntityManager() {
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      //some production config here
      return em;
    }
}

如何添加这个仅用于集成测试的小 JPA 属性,最好是通过下面的监听器(在测试类路径中)

public class AfterAllBeansCreatedListener {

    @Autowired
    private LocalContainerEntityManagerFactoryBean em

    @PostConstruct
    private void reconfigure() {
        HashMap<String, Object> properties = em.getJpaPropertyMap()
        properties.put("hibernate.hbm2ddl.auto", "create");
        em.setJpaPropertyMap(properties);
    }
}

目前,我在嵌套的@TestConfiguration 中使用另一种带有@Bean 注释的方法生成LocalContainerEntityManagerFactoryBean,但这意味着我需要复制该bean 的所有其他创建逻辑,这当然是纯粹的邪恶。

【问题讨论】:

    标签: spring spring-boot spring-boot-test


    【解决方案1】:

    快速简单的解决方案是像这样在 mcEntityManager() 方法中创建属性映射

    @Configuration
    @EnableJpaRepositories(
    entityManagerFactoryRef = "myEntityManager",
    transactionManagerRef = "myTransactionManager",
    basePackages = {"com.mycompany.my.repository"}
    )
    public class MyDbConfig {
    
        @Bean
        public LocalContainerEntityManagerFactoryBean mcEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        
        HashMap<String, Object> properties = em.getJpaPropertyMap()
        properties.put("hibernate.hbm2ddl.auto", "create");
        em.setJpaPropertyMap(properties);
    
        return em;
        }
    }
    

    如果您需要每个 env 不同的 JpaProperty 映射值,您可以将属性映射创建移动到它自己的配置文件特定方法,例如

    @Bean
    @Profile("dev)
    public Map<String,Object> getMyJpaPropertyMap() {
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate.temp.use_jdbc_metadata_defaults",false);
        properties.put("hibernate.jdbc.lob.non_contextual_creation",true);
    }
    
    @Bean
    @Profile("prod)
    public Map<String,Object> getMyJpaPropertyMap() {
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.temp.use_jdbc_metadata_defaults",true);
    }
    

    并且 mcEntityManager() 方法更新为

    @Bean
    public LocalContainerEntityManagerFactoryBean mcEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setJpaPropertyMap(getMyJpaPropertyMap());
        return em;
    }
    

    【讨论】:

    • 您可能需要注入 jpaPropertyMap。否则你会在编译时遇到方法签名冲突
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多