Java 在嵌入式 HSQL 数据库中创建模式的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18331203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:34:25  来源:igfitidea点击:

Best way to create schema in embedded HSQL database

javaspringhibernatehsqldbembedded-database

提问by t0mmyw

I'm currently using the following setup to create a schema in an embedded database before running my tests against it

我目前正在使用以下设置在嵌入式数据库中创建一个架构,然后再针对它运行我的测试

In my application context

在我的应用程序上下文中

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:createSchema.sql" />
</jdbc:embedded-database>

createSchema.sql

创建Schema.sql

create schema ST_TEST AUTHORIZATION DBA;

hibernate properties

休眠属性

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
    <property name="hibernate.default_schema" value="ST_TEST"/>
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.use_sql_comments" value="true" />
    <property name="hibernate.cache.use_second_level_cache" value="false" />
</properties>

My question is is this the best way to do this. Or can i use a different schema name in my properties? or set the schema name in the jdbc:embedded-database element

我的问题是这是做到这一点的最佳方式。或者我可以在我的属性中使用不同的模式名称吗?或在 jdbc:embedded-database 元素中设置模式名称

采纳答案by t0mmyw

By default HSQL creates a schema called PUBLIC. source: HSQL documentation

默认情况下,HSQL 创建一个名为 PUBLIC 的模式。来源:HSQL 文档

Seeing as the schema name is never seen in the tests (named queries/entity manager to do the interactions) you can change the hibernate properties to use this PUBLIC schema

由于在测试中从未见过架构名称(命名查询/实体管理器进行交互),您可以更改休眠属性以使用此 PUBLIC 架构

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
    <property name="hibernate.default_schema" value="PUBLIC"/>
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>

OR
just leave out the default_schema from the properties list and it uses PUBLIC anyway

或者
只是从属性列表中省略 default_schema 并且它无论如何都使用 PUBLIC

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>

回答by Dhanush Gopinath

You can use this code in your Base Testing class, and call it using @BeforeClass annotation (for Junit). I do it like this.

您可以在 Base Testing 类中使用此代码,并使用 @BeforeClass 注释(对于 Junit)调用它。我这样做。

    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    builder = builder.setType(EmbeddedDatabaseType.HSQL).addScript(
            "createSchema.sql");
    builder.setName("MyDatabase");
    EmbeddedDatabase db = builder.build();