H2 SQL语法异常

时间:2013-07-29 14:00:59

标签: spring hibernate h2 spring-batch

我正在尝试为H2导入sql脚本。此脚本由spring-batch提供,用于存储作业元数据。当我直接在H2控制台中执行这个脚本时,我没有语法错误,但我引用了Hibernate / JPA中的相同脚本,在初始化阶段导入,我得到了这个例外:

 org.hibernate.tool.hbm2ddl.ImportScriptException: Error during statement execution (file: 'org/springframework/batch/core/schema-h2.sql'): CREATE TABLE BATCH_JOB_INSTANCE  (
   ....    
    Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE BATCH_JOB_INSTANCE  ("; expected "identifier"; SQL statement:
CREATE TABLE BATCH_JOB_INSTANCE  ( [42001-171]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2-1.3.171.jar:1.3.171]
    at org.h2.message.DbException.get(DbException.java:169) ~[h2-1.3.171.jar:1.3.171]
    at org.h2.message.DbException.getSyntaxError(DbException.java:194) ~[h2-1.3.171.jar:1.3.171]

以下是我要执行的脚本:https://code.google.com/p/joshlong-examples/source/browse/trunk/batch/src/main/resources/sql/schema-h2.sql?r=2

我正在使用hbm2ddl导入sql文件:

jpaProperties.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
    jpaProperties.setProperty("hibernate.dialect", H2Dialect.class.getName());
    jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    jpaProperties.setProperty("hibernate.hbm2ddl.import_files",
        "org/springframework/batch/core/schema-drop-h2.sql,org/springframework/batch/core/schema-h2.sql");

知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:10)

尝试在一行中编写每个create语句。

import.sql中的语句分隔符是换行符。如果你想改变它,那么你需要使用Hibernate> 4.1。在那里,您可以实施MultipleLinesSqlCommandExtractor并按hibernate.hbm2ddl.import_files_sql_extractor

指定

答案 1 :(得分:2)

我终于找到了问题的答案。根据Ralph的回答,要解决此问题,请为hibernate添加以下属性:

jpaProperties.setProperty("hibernate.hbm2ddl.import_files_sql_extractor", "org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor");

或者用XML:

<property key="hibernate.hbm2ddl.import_files_sql_extractor" value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor" />

此类MultipleLinesSqlCommandExtractor是接口ImportSqlCommandExtractor的实现。这是在使用Hibernate执行SchemaExport时调用的接口。默认实现是SingleLineSqlCommandExtractor,并且由于未知原因返回语法错误。用多行提取器替换单行提取器解决了问题。