Best way to create schema in embedded HSQL database

ⅰ亾dé卋堺 提交于 2019-12-10 03:28:58

问题


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

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


回答1:


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

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

<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

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



回答2:


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

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


来源:https://stackoverflow.com/questions/18331203/best-way-to-create-schema-in-embedded-hsql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!