Skip to content

Generate database schema DDL from Hibernate hbm mappings

Hibernate can be used to map Java classes to existing database tables. More often, the Java classes come first and the database is created around the mapping. If that’s the case, you’ll want to define your database schema directly from Hibernate mappings rather than hand crafting DDL scripts. Hibernate offers a couple of ways to do this.

hibernate.hbm2ddl.auto

The quickest way to create a database based on Hibernate mappings is to use the Hibernate hibernate.hbm2ddl.auto property. This can be set in the Hibernate configuration file (hibernate.cfg.xml) or in the Spring Hibernate Session Factory bean (LocalSessionFactoryBean) if you use Spring to configure as I do here.

<bean id="sessionFactory">
    <property name="dataSource" ref="spannersDS"/>
    <property name="hibernateProperties" ref="hibernateProperties"/>
    <property name="mappingLocations">
        <value>classpath:org/dontpanic/spanners/dao/Spanners.hbm.xml</value>
    </property>
</bean>

<bean id="hibernateProperties">
    <property name="properties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

Using the above settings, Hibernate will create the database schema on application start up. This is great to get started quickly but no use for a production environment. On application start up, the database schema is cleared down and recreated – all existing data will be lost.

A quick hack is to use the create setting during development and then use your favourite database tool to export the schema DDL from the database. For a production build, this setting must be changed back to validate or removed. The resulting DDL can be used to initialize any other environment before first run. The application can then be started and stopped without fear of dropping the schema and data.

This is a little bit of a hack and not really advisable for anything other than hobby projects.

hibernate3-maven-plugin

The hibernate3-maven-plugin can also be used to create a schema DDL from Hibernate mapping files. This is a more robust way of creating DDL files as it can be built into the Maven build process and controlled with profiles if necessary. The following Maven profile allows optional generation of a schema DDL file:

<profiles>
    <profile>
        <id>hbm2ddl</id>
        <build>
            <plugins>
                <!-- hibernate ddl generation -->
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                        <artifactId>hibernate3-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2ddl</name>
                                <outputDirectory>target/hbm2ddl</outputDirectory>
                            </component>
                        </components>
                        <componentProperties>
                            <configurationfile>src/main/hbm2ddl/hibernate.cfg.xml</configurationfile>
                            <propertyfile>src/main/hbm2ddl/hibernate-mysql.properties</propertyfile>
                            <outputfilename>hbm2ddl.sql</outputfilename>
                            <drop>false</drop>
                            <create>true</create>
                            <export>false</export>
                            <jdk5>true</jdk5>
                            <format>true</format>
                        </componentProperties>
                    </configuration>
                    <executions>
                        <execution>
                            <id>hbm2ddl</id>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>hbm2ddl</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

The schema can be created by activating the hbm2ddl profile like so

mvn package -Phbm2ddl

The resulting DDL file will be specific to your chosen database vendor as the database dialect is defined in the file specified by the propertyfile property (hibernate-mysql.properties):

hibernate.dialect=org.hibernate.dialect.MySQLDialect

The hibernate.cfg.xml used here simply lists the mapping files to scan:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping resource="org/dontpanic/spanners/dao/Spanners.hbm.xml" />
    </session-factory>
</hibernate-configuration>

The resulting DDL script contains all information that can be inferred from the hibernate mapping including column types and widths, foreign keys and constraints.

Published inHow To

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *