0

I´m using Spring MyBatis to insert a row in a table. The problem is that in my h2 database I dont have create the table yet, and I would like to do it on the fly

this is my current spring context configuration

<sql id="CREATE_TABLE">
    CREATE TABLE ENTITY(ENTITY_ID INT PRIMARY KEY, TITLE VARCHAR(255),DESCRIPTION VARCHAR(255) )
</sql>

<insert id="CREATE" parameterType="com.dao.entity.dto.EntityDaoDTO">
    INSERT INTO
    ENTITY
    (ENTITY_ID, TITLE, DESCRIPTION)
    VALUES
    (#{entityId},#{title},#{description})

</insert>

How can I do to specify that sql tag must be executed before the insert happens??

I´m running h2 as a java process through maven

                     <execution>
                        <id>start-h2</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>

                        <configuration>
                            <name>start-h2-f2e</name>
                            <waitAfterLaunch>2</waitAfterLaunch>
                            <workingDir>../../../h2/sakila-h2-master/</workingDir>
                            <arguments>
                                <argument>java</argument>
                                <argument>-cp</argument>
                                <argument>h2-1.3.161.jar</argument>
                                <argument>org.h2.tools.Server</argument>
                                <argument>-ifExists</argument>
                                <argument>-tcp</argument>
                                <argument>-web</argument>
                                <argument>-tcpAllowOthers</argument>
                            </arguments>
                        </configuration>
                    </execution>

Regards

2
  • I would say you need init script ddl to be executed on start to create DB structure rather than adding the checks in the insert call. Commented Feb 14, 2017 at 14:09
  • never used that ddl script, any idea where it´s should be? I just run the h2 as a java process with maven, look the edited question Commented Feb 14, 2017 at 14:12

1 Answer 1

1

See about DB init here

In fact you need to add to your spring something like this

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:database/scripts/init-db.ddl" />
</jdbc:initialize-database>

where dataSource is the h2 datasource to be initialized. All the DB structure creation and some initial data insertion could be done in separate DDL/SQL scripts.

You just execute them on start for the in memory H2 DB.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.