복붙노트

[SPRING] H2 SQL 문법 예외

SPRING

H2 SQL 문법 예외

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");

어떻게하면이 문제를 해결할 수 있을까요?

해결법

  1. ==============================

    1.한 줄에 각각의 작성 문을 작성하십시오.

    한 줄에 각각의 작성 문을 작성하십시오.

    import.sql의 명령문 분리 문자는 개행 문자입니다. 그것을 변경하고 싶다면, 당신은 Hibernate> 4.1을 사용할 필요가있다. 여기서 MultipleLinesSqlCommandExtractor를 구현하고 hibernate.hbm2ddl.import_files_sql_extractor로 지정할 수 있습니다.

  2. ==============================

    2.드디어 내 질문에 대한 답을 찾았습니다. Ralph의 답변에 따라이 문제를 해결하려면 다음 속성을 최대 절전 모드에 추가하십시오.

    드디어 내 질문에 대한 답을 찾았습니다. Ralph의 답변에 따라이 문제를 해결하려면 다음 속성을 최대 절전 모드에 추가하십시오.

    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이고 알 수없는 이유로 구문 오류를 반환합니다. 한 줄 추출기를 여러 줄 추출기로 바꾸면 해당 문제가 해결됩니다.

  3. from https://stackoverflow.com/questions/17926093/h2-sql-grammar-exception by cc-by-sa and MIT license