Best way to create schema in embedded HSQL database
Asked Answered
B

2

8

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

Battlement answered 20/8, 2013 at 9:11 Comment(0)
B
11

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>
Battlement answered 21/8, 2013 at 8:11 Comment(1)
what about if there are two schema in embedded hsqldb ?Dniren
C
4

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();
Conquian answered 20/8, 2013 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.