我使用hsqldb进行单元测试。我的作品使用的是Oracle 11G Db。 当我按上面的方式运行我的启动脚本时:
<jdbc:embedded-database id="dataSource" type="HSQL">
</jdbc:embedded-database>
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="classpath:/sql/init-cct-schema.sql" separator=";" />
<jdbc:script location="classpath:/sql/init-cct-insert.sql" separator=";" />
</jdbc:initialize-database>
我真的是HSQL docs中的触发示例。
我看到this post: 但他的解决方案对我不起作用,或者我不理解。
我一直都有这个错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at
...
... 38 more
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 9 of resource class path resource [sql/init-cct-schema.sql]: CREATE TRIGGER TI_TYPE_MVT BEFORE INSERT ON TYPE_MVT REFERENCING NEW AS newrow FOR EACH ROW BEGIN ATOMIC IF newrow.TYPE_MVT_PK is null THEN SET newrow.TYPE_MVT_PK = SQ_TYPE_MVT.nextval
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:199)
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:132)
at org.springframework.jdbc.datasource.init.CompositeDatabasePopulator.populate(CompositeDatabasePopulator.java:55)
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:45)
... 41 more
Caused by: java.sql.SQLSyntaxErrorException: unexpected end of statement: required: ;
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:184)
... 44 more
Caused by: org.hsqldb.HsqlException: unexpected end of statement: required: ;
at org.hsqldb.error.Error.parseError(Unknown Source)
这是我的触发器:
SET DATABASE SQL SYNTAX ORA TRUE;
CREATE TRIGGER TI_TYPE_MVT BEFORE INSERT ON TYPE_MVT
REFERENCING NEW AS newrow FOR EACH ROW
BEGIN ATOMIC
IF newrow.TYPE_MVT_PK is null THEN
SET newrow.TYPE_MVT_PK = SQ_TYPE_MVT.nextval;
END IF;
END;
我试着没有最后的';' ,它会继续失败。
这是我对HSQL的依赖:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
</dependency>
任何想法?
答案 0 :(得分:10)
链接HSQL Create Procedure Syntax doesn't seem to match the documentation中的解决方案位于以下配置行中:
<jdbc:script location="file:Artifacts/Hsql Version Scripts/install/install.sql" separator="/;"/>
默认情况下,Spring脚本使用的分隔符是分号。这意味着当达到触发器定义中的第一个分号时,不完整的定义将被发送到HSQLDB(这会导致错误)。使用上面的配置行时,它会将默认分隔符更改为两个字符“/;”。使用特殊配置,您需要修改脚本以在每个创建触发器定义的末尾具有此分隔符。将分号保留在触发器定义主体内部。
答案 1 :(得分:0)
在Spring中进行配置时-添加以下内容:
返回新的EmbeddedDatabaseBuilder() .setType(“ HSQL”) .setName(“ DBNAME”) .addScript(“ some_script.sql”) .setSeparator(“ /;”) .build();