0

I have a sql /ddl script containing CREATE TABLE commands.

I use hibernate and I want hibernate to execute this script to create the database structure.

How to do this?

1 Answer 1

3

If you use Spring you can populate database using its JDBC utilities:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <jdbc:embedded-database id="dataSource" type="H2" />

    <bean class="org.springframework.jdbc.datasource.init.DataSourceInitializer" depends-on="sessionFactory">
        <property name="databasePopulator" ref="resourceDatabasePopulator" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="resourceDatabasePopulator" class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
        <property name="scripts">
            <array>
                <value>classpath*:init-hibernate.sql</value>
            </array>
        </property>
    </bean>
</beans>
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.