HSQL-如果Spring Boot应用程序中不存在,则创建模式 [英] Hsql - create schema if not exists in spring boot application

查看:203
本文介绍了HSQL-如果Spring Boot应用程序中不存在,则创建模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using embedded hsql db in my spring boot application and want to create a schema only if does not exist in the db.

假设架构名称为XXX,

Assuming the schema name is XXX,

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

I have the below single statement schema-hsql.sql file:

CREATE SCHEMA XXX IF NOT EXISTS

但是随着下面的堆栈跟踪逐渐消失:

However this is faing with below stack trace:

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

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

What is the correct syntax for achieving this?

谢谢

推荐答案

使用

CREATE SCHEMA IF NOT EXISTS XXX

代替您的SQL

CREATE SCHEMA XXX IF NOT EXISTS

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

and use org.hsqldb::hsqldb v.2.4.0

请参阅: Hsql-如果架构不存在,则创建架构春季启动应用程序

这篇关于HSQL-如果Spring Boot应用程序中不存在,则创建模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆