[英]Spring Boot @DataJpaTest without ddl generation
I have MySQL with tables in two databases and want to write a test with SpringBoot (1.5.6.RELEASE) and JPA. 我在两个数据库中都有带MySQL的表,并想用SpringBoot(1.5.6.RELEASE)和JPA编写测试。 For this, I use the
@DataJpaTest
together with @EntityScan
, as the entities are in two different packages. 对于这一点,我用的是
@DataJpaTest
连同@EntityScan
,作为实体在两个不同的包。 As embedded database for the test, I use H2. 作为测试的嵌入式数据库,我使用H2。
schema.sql
with two CREATE SCHEMA IF NOT EXISTS
statements like described in this question . schema.sql
其中包含两个CREATE SCHEMA IF NOT EXISTS
语句,如本问题所述 。 data.sql
file. data.sql
文件。 The problem was now, that Spring boot first executed the schema.sql
, then the data.sql
and following this Hibernate again created the tables, which eventually resulted in empty tables. schema.sql
,那么data.sql
并且在这之后休眠再次创建表,这最终导致空表。 To solve this problem, I tried to set spring.jpa.hibernate.ddl-auto=none
within the application.properties
. application.properties
设置spring.jpa.hibernate.ddl-auto=none
。 However, Hibernate now started to switch naming strategies and converted camelCase to sneak_case, which again resulted in errors. spring.jpa.generate-ddl=false
, but this did not have any effect. spring.jpa.generate-ddl=false
,但这没有任何效果。 How is it possible to deactivate the automatic DDL generation (only use schema.sql
and data.sql
) and at the same time use @DataJpaTest
? 这怎么可能停用自动生成DDL(只使用
schema.sql
和data.sql
),并在同一时间使用@DataJpaTest
?
Thank you very much! 非常感谢你!
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
@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;
}
Note, that application.properies
or application.yaml
should be under test/resources
and your @Configuration
class with @Bean
should be used by tests. 注意,
application.properies
或application.yaml
应该在test/resources
并且带有@Bean
的@Configuration
类应该由测试使用。
For test via sql
-files you could use EmbeddedDataSource
为了通过
sql
-files进行测试,可以使用EmbeddedDataSource
you can create another application.properties and configure file for under test directory. 您可以创建另一个application.properties并为测试目录下的文件配置文件。
for other problem use flyway after db creation flyway scripts which is your insert scripts run automatically. 对于其他问题,请在数据库创建后使用flywayflyway脚本,这是您的插入脚本自动运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.