是否可以通过.sql脚本禁用嵌入式H2 jdbc数据源init并使用带有实体标记的对象代替没有init数据的对象

问题描述

我正在从事Spring Boot项目,并且已经针对嵌入式H2成功编写了一些测试。在我的本地环境中,带有@Entitiy注释的模型类用于创建测试表。当我使用mvn构建项目时,出现错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; 

nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [URL [file:/home/.../.../.../.../.../target/test-classes/data.sql]]; 

nested exception is java.lang.IllegalArgumentException: 'script' must not be null or empty

我发现here是默认的默认行为。问题是我只需要空表来进行实体模型创建的测试。就像提到的here一样,我认为Spring会自动检测嵌入式数据库,并在表生命周期中使用“ create-drop”。

是否可以通过脚本禁用此datasource.init进程?是否必须在src / test / resources下的application.properties文件中添加其他属性?我想默认值会按照我想要的方式运行,因为当我在intellij中运行默认值时,一切正常。

编辑: 我通过创建其中包含无用的sql语句的data.sql文件进行了变通。现在它可以工作了,但是看起来很hacky。对于是否有办法删除此自动配置行为仍然很感兴趣。

解决方法

参考:https://docs.spring.io/autorepo/docs/spring-boot/1.1.0.M1/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-hibernate

spring.datasource.continueOnError = true

Spring Boot默认情况下启用Spring JDBC初始化程序的failfast功能,因此,如果脚本导致异常,则应用程序将无法启动。

要禁用快速故障转移,可以设置spring.datasource.continueOnError = true。一旦应用程序成熟并部署了几次,这将很有用,因为脚本可以充当“穷人的迁移” -插入失败意味着数据已经存在,因此无需阻止应用程序例如运行。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...