8

単体テスト用のSQLスクリプトファイルをロードしたい。Spring 2.5.2を使用しているので、次のコードを使用して、このメソッドを使用してスクリプトファイルをロードすることにしました。SimpleJdbcTestUtils.executeScript()

DriverManagerDataSource dataSource = ... // getting my DataSource defined in my Spring context
SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);
Resource resource = new ClassPathResource("/create-table.sql");
SimpleJdbcTestUtils.executeSqlScript(template, resource, true);

create-table.sql各SQLステートメントをファイルの1行に記述すれば、すべて問題ありません。しかし、ステートメントを複数行に書き込むと、ステートメントが。で終了していても、エラーが発生します;

作業スクリプト:

CREATE TABLE T_FOO (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP, IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE T_BAR (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP, IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);

スクリプトが機能しない:

CREATE TABLE T_FOO (
    ID NUMERIC PRIMARY KEY,
    DATEID TIMESTAMP,
    IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE T_BAR (
    ID NUMERIC PRIMARY KEY,
    DATEID TIMESTAMP,
    IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);

2番目のファイルのエラー:

org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [CREATE TABLE T_FOO (]; nested exception is java.sql.SQLException: Unexpected token:  in statement [CREATE TABLE T_FOO (]
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:111)
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:404)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:519)
    at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.update(SimpleJdbcTemplate.java:237)
    at org.springframework.test.jdbc.SimpleJdbcTestUtils.executeSqlScript(SimpleJdbcTestUtils.java:150)
    at org.springframework.test.jdbc.SimpleJdbcTestUtils.executeSqlScript(SimpleJdbcTestUtils.java:113)
    at foo.bar.HsqldbUtils.run(HsqldbUtils.java:95)
    at foo.bar.SomeUnitTest.executeTests(SomeUnitTest.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: Unexpected token:  in statement [CREATE TABLE T_FOO (]
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.executeUpdate(Unknown Source)
    at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:509)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:393)

より複雑なテーブルを作成したいので、SQL命令をより読みやすい方法で記述し、数行を使用することを好みます。それを行う方法はありますか?

4

1 に答える 1

6

Spring 2.5.2を使用していますが、2.5.6のドキュメントを参照してください。

2.5.2のjavadoc(ここを参照)を見ると、複数行のスクリプトをサポートしていないことがわかります。

これは明らかに2.5.4で解決されました(ここで問題が発生します)。

2.5.6、さらには3.0.xにアップグレードする必要があります。

于 2010-02-20T16:30:38.947 に答える