【问题标题】:Hsql - create schema if not exists in spring boot applicationHsql - 如果 Spring Boot 应用程序中不存在,则创建模式
【发布时间】:2016-10-24 02:53:15
【问题描述】:

我在我的 Spring Boot 应用程序中使用嵌入式 hsql db 并且想要 仅当数据库中不存在架构时才创建架构。

假设架构名称是XXX,

我有以下单语句 schema-hsql.sql 文件:

CREATE SCHEMA XXX IF NOT EXISTS

然而,这与下面的堆栈跟踪有关:

org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/xxx/schema-hsql.sql]: CREATE SCHEMA XXX IF NOT EXISTS; nested exception is java.sql.SQLSyntaxErrorException: unexpected token: IF

at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:494)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:231)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:157)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:81)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:305)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 65 more
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: IF
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
    at com.mchange.v2.c3p0.impl.NewProxyStatement.execute(NewProxyStatement.java:909)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:473)
    ... 77 more
Caused by: org.hsqldb.HsqlException: unexpected token: IF
    at org.hsqldb.error.Error.parseError(Unknown Source)
    at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
    at org.hsqldb.ParserDDL.getCompiledStatementBody(Unknown Source)
    at org.hsqldb.ParserDDL.compileCreateSchema(Unknown Source)
    at org.hsqldb.ParserDDL.compileCreate(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 81 more

实现此目的的正确语法是什么?

谢谢

【问题讨论】:

    标签: java hibernate jdbc spring-boot hsqldb


    【解决方案1】:

    使用

    CREATE SCHEMA IF NOT EXISTS XXX
    

    而不是你的 sql

    CREATE SCHEMA XXX IF NOT EXISTS
    

    并使用 org.hsqldb::hsqldb v.2.4.0

    见: Hsql - create schema if not exists in spring boot application

    【讨论】:

      【解决方案2】:

      你也可以在你的“hibernate.cfg.xml”中指定这个:

      <!-- Drop and re-create the database schema on startup -->
      <property name="hbm2ddl.auto">create</property>
      

      这是完整的例子:

      <session-factory>
      
          <!-- Database connection settings -->
          <property name="connection.driver_class">org.h2.Driver</property>
          <property name="connection.url">jdbc:h2:file:db/personh2db;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
          <property name="connection.username">sa</property>
          <property name="connection.password"/>
      
          <!-- JDBC connection pool (use the built-in) -->
          <property name="connection.pool_size">1</property>
      
          <!-- SQL dialect -->
          <property name="dialect">org.hibernate.dialect.H2Dialect</property>
      
          <!-- Disable the second-level cache  -->
          <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
      
          <!-- Echo all executed SQL to stdout -->
          <property name="show_sql">true</property>
      
          <!-- Drop and re-create the database schema on startup -->
          <property name="hbm2ddl.auto">create</property>
      
          <mapping resource="com/example/model/Person.hbm.xml"/>
      
      </session-factory>
      

      hbm2ddl.autoare 的可能选项列表,

      • validate:验证架构,不更改数据库。
      • 更新:更新架构。
      • create:创建架构,销毁以前的数据。
      • create-drop:在会话结束时删除架构。

      编辑

      使用 Spring boot 时,您可以在 application.properties 中配置休眠属性。

      这是application.properties 的文档:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

      您可以在此处找到hbm2ddl.auto 配置的示例:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-configure-jpa-properties

      【讨论】:

      • 我没有 hibernate.cfg.xml 因为我在 Spring Boot 上运行。我想我的问题是 CREATE SCHEMA XXX IF NOT EXISTS 语句有些错误,我正在为 hsql db 搜索正确的版本。
      猜你喜欢
      • 2013-09-08
      • 2018-12-12
      • 2019-11-22
      • 2018-11-28
      • 2018-09-01
      • 2019-02-17
      • 2016-04-27
      • 2020-07-23
      • 2020-12-21
      相关资源
      最近更新 更多