jorrebor
jorrebor

Reputation: 2232

How to get Hibernate to create tables based on DDL / SQL script?

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?

Upvotes: 0

Views: 833

Answers (1)

hoaz
hoaz

Reputation: 10143

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>

Upvotes: 3

Related Questions