Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何在测试中重新配置LocalContainerEntityManagerFactoryBean_Spring_Spring Boot_Spring Boot Test - Fatal编程技术网

Spring 如何在测试中重新配置LocalContainerEntityManagerFactoryBean

Spring 如何在测试中重新配置LocalContainerEntityManagerFactoryBean,spring,spring-boot,spring-boot-test,Spring,Spring Boot,Spring Boot Test,如果我有一个编程管理的LocalContainerEntityManagerFactoryBean,比如@Bean注释方法: @Configuration @EnableJpaRepositories( entityManagerFactoryRef = "myEntityManager", transactionManagerRef = "myTransactionManager", basePackages = {"com.mycompany.my.repository"} )

如果我有一个编程管理的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类之后beanscreatedListener{
@自动连线
私有LocalContainerEntityManagerFactoryBean em
@施工后
私有void重新配置(){
HashMap属性=em.getJpaPropertyMap()
properties.put(“hibernate.hbm2ddl.auto”、“create”);
em.setJpaPropertyMap(属性);
}
}

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

快速而简单的解决方案是在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;
    }
}

快速而简单的解决方案是在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;
    }
}

您可能需要注入jpaPropertyMap。否则,您将在编译时得到方法签名冲突,您可能需要注入jpaPropertyMap。否则,您将在编译时得到方法签名冲突
@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);
}
@Bean
public LocalContainerEntityManagerFactoryBean mcEntityManager() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setJpaPropertyMap(getMyJpaPropertyMap());
    return em;
}