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

时间:2022-09-15 19:47:01

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

createSchema.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元素中设置模式名称

2 个解决方案

#1


11  

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

在测试中看不到模式名称(命名查询/实体管理器进行交互),您可以更改hibernate属性以使用此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>

#2


4  

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

#1


11  

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

在测试中看不到模式名称(命名查询/实体管理器进行交互),您可以更改hibernate属性以使用此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>

#2


4  

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