java - Spring Boot @DataJpaTest 不生成 ddl

标签 java hibernate unit-testing spring-boot spring-data-jpa

我有 MySQL,表位于两个数据库中,并且想使用 SpringBoot (1.5.6.RELEASE) 和 JPA 编写测试。为此,我将 @DataJpaTest@EntityScan 一起使用,因为实体位于两个不同的包中。作为测试的嵌入式数据库,我使用H2。

  1. 我的第一个问题是,Spring Boot 引发了未找到架构的异常,因此我创建了一个包含两个 CREATE SCHEMA IF NOT EXISTS 语句的 schema.sql就像描述的那样in this question .
  2. 但是,我还想插入一些测试数据并添加了 data.sql 文件。现在的问题是,Spring boot 首先执行 schema.sql,然后执行 data.sql,然后 Hibernate 再次创建表,最终导致空表。为了解决这个问题,我尝试在application.properties中设置spring.jpa.hibernate.ddl-auto=none。然而,Hibernate 现在开始切换命名策略,将camelCase 转换为sneak_case,这又导致了错误。所以我想,这不是应该做的事情。我也尝试过spring.jpa.generate-ddl=false,但这没有任何效果。

如何停用自动 DDL 生成(仅使用 schema.sqldata.sql)并同时使用 @DataJpaTest

非常感谢!

最佳答案

解决方案 1:properties/yaml 文件

spring:
 autoconfigure:
  exclude: org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
 jackson:
  serialization:
   indent_output: true
 datasource:
  driver-class-name: org.hsqldb.jdbcDriver
  generate-unique-name: true
 jpa:
  hibernate:
    ddl-auto: none
  show-sql: true
 h2:
  console:
   enabled: false

liquibase:
 change-log: classpath:/liquibase/db.changelog-master.xml
 drop-first: true
 contexts: QA

解决方案2:覆盖@Bean

@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
    final LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(dataSource);
    factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    factory.setPackagesToScan("com.spring.web.demo.persistent.entity");
    factory.setJpaProperties(jpaProperties(env));

    return factory;
}

private Properties jpaProperties(Environment env) {
    final Properties properties = new Properties();
    properties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
    //!!!: see here
    properties.put("hibernate.hbm2ddl.auto", "none");

    properties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", false);
    properties.put("hibernate.physical_naming_strategy", PhysicalNamingStrategyStandardImpl.class.getName());
    properties.put("hibernate.generate_statistics", true);
    properties.put("hibernate.cache.use_second_level_cache", true);
    properties.put("hibernate.cache.use_query_cache", true);
    properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");

    return properties;
}

请注意,application.properiesapplication.yaml 应位于 test/resources 下,并且您的 @Configuration > 测试应使用带有 @Bean 的类。

要通过 sql 文件进行测试,您可以使用 EmbeddedDataSource

我的 github 有一些例子

关于java - Spring Boot @DataJpaTest 不生成 ddl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471625/

相关文章:

c# - 英孚。模拟 ICollection

hibernate - BeginTransaction Hibernate有必要吗?

swift - 使用 Swift : body of closure not executed 进行单元测试

java - 如何在java中验证文本字段

Java自定义注释和动态数据库内容

java - "java.text.ParseException: Unparseable date: "2017 年 6 月 18 日,5 :39AM"

java - 为了绘制轻量级组件,需要递归更新或绘制什么?

java - Spring Boot 中使用 3 个表进行 Hibernate 映射的问题

mysql - Hibernate 5.0.12 错误的列定义与 Mysql 5.7 转义

c# - System.Net.Http 无法加载