SpringBoot 2.6升级至2.7.9后集成测试出现H2表不存在错误
SpringBoot 2.6.14升级至2.7.9后集成测试报错:表"DOCUMENT"不存在
将SpringBoot版本从2.6.14升级到2.7.9后,未做任何其他改动,但集成测试抛出Table "DOCUMENT" not found;错误,实际该表是存在的。错误日志如下:
[INFO] Running com.trex.integration.tests.RepositoriesIT [ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.071 s <<< FAILURE! - in com.trex.integration.tests.RepositoriesIT [ERROR] testPageComponent Time elapsed: 0.064 s <<< ERROR! com.trex.application.services.exception.AsyncPreProcessingServiceException: org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select documentda0_.id as id1_2_0_, documentda0_.company_country as company_2_2_0_, documentda0_.company_currency as company_3_2_0_, documentda0_.company_date_format as company_4_2_0_, documentda0_.created_utc as created_5_2_0_, documentda0_.labeled as labeled6_2_0_, documentda0_.labeled_at as labeled_7_2_0_, documentda0_.line_items as line_ite8_2_0_, documentda0_.batch_only as tech_onl9_2_0_, documentda0_.vat_registered_company as vat_reg10_2_0_, documentda0_.vde_document_id as vde_doc11_2_0_, documentda0_.workflow as workflo12_2_0_ from document documentda0_ where documentda0_.id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement at com.trex.integration.tests.RepositoriesIT.testPageComponent(RepositoriesIT.java:153) Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select documentda0_.id as id1_2_0_, documentda0_.company_country as company_2_2_0_, documentda0_.company_currency as company_3_2_0_, documentda0_.company_date_format as company_4_2_0_, documentda0_.created_utc as created_5_2_0_, documentda0_.labeled as labeled6_2_0_, documentda0_.labeled_at as labeled_7_2_0_, documentda0_.line_items as line_ite8_2_0_, documentda0_.batch_only as tech_onl9_2_0_, documentda0_.vat_registered_company as vat_reg10_2_0_, documentda0_.vde_document_id as vde_doc11_2_0_, documentda0_.workflow as workflo12_2_0_ from document documentda0_ where documentda0_.id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement at com.trex.integration.tests.RepositoriesIT.testPageComponent(RepositoriesIT.java:153) Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement at com.trex.integration.tests.RepositoriesIT.testPageComponent(RepositoriesIT.java:153) Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "DOCUMENT" not found; SQL statement: select documentda0_.id as id1_2_0_, documentda0_.company_country as company_2_2_0_, documentda0_.company_currency as company_3_2_0_, documentda0_.company_date_format as company_4_2_0_, documentda0_.created_utc as created_5_2_0_, documentda0_.labeled as labeled6_2_0_, documentda0_.labeled_at as labeled_7_2_0_, documentda0_.line_items as line_ite8_2_0_, documentda0_.batch_only as tech_onl9_2_0_, documentda0_.vat_registered_company as vat_reg10_2_0_, documentda0_.vde_document_id as vde_doc11_2_0_, documentda0_.workflow as workflo12_2_0_ from document documentda0_ where documentda0_.id=? [42102-214] at com.trex.integration.tests.RepositoriesIT.testPageComponent(RepositoriesIT.java:153)
解决方案
- 调整H2数据库大小写配置:SpringBoot 2.7升级后,H2默认的大小写敏感规则可能变化。在测试数据源URL中添加
;DATABASE_TO_LOWER=TRUE或;CASE_INSENSITIVE_IDENTIFIERS=TRUE,强制表名大小写不敏感:spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE - 确认Hibernate DDL生成策略:检查
spring.jpa.hibernate.ddl-auto配置,确保测试环境设置为create、create-drop或update,保证表结构能正常生成。SpringBoot 2.7搭配的Hibernate 5.6.x相比2.6版本的5.5.x,DDL生成逻辑有细微调整。 - 锁定Hibernate命名策略:如果实体类表名映射出现大小写问题,添加以下配置保持原命名策略,避免Hibernate自动转换表名:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - 重置测试上下文:在测试类上添加
@DirtiesContext注解,确保每次测试重新初始化Spring上下文和H2内存数据库,避免缓存残留的旧表结构影响测试。
内容的提问来源于stack exchange,提问作者gravitypulling
相关产品推荐
相关产品推荐

