Can Hibernate Schema generation be used to generate DDL without database connection

2.6k views Asked by At

I'd like to use Hibernate' schema generation to generate DDL for a database that I cannot access directly from my PC, just using hibernate config files. I'd like to skip, if possible, the installation of a local oracle database. Can hibernate generate DDL for a "theoretical" database of the appropriate dialect, version, etc., or is this a pipe dream?

Are there other tools that can do this?

3

There are 3 answers

2
Vlad Mihalcea On
  1. You can either use an In-memory database during testing phase;

    hibernate.hbm2ddl.auto="update"

  2. Or you can generate your DDL using the hibernatetool from Maven:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>generate-test-sql-scripts</id>
                <phase>generate-test-resources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <tasks>
                        <property name="maven_test_classpath" refid="maven.test.classpath"/>
                        <path id="hibernate_tools_path">
                            <pathelement path="${maven_test_classpath}"/>
                        </path>
                        <property name="hibernate_tools_classpath" refid="hibernate_tools_path"/>
                        <taskdef name="hibernatetool"
                                 classname="org.hibernate.tool.ant.HibernateToolTask"/>
                        <mkdir dir="${project.build.directory}/test-classes/hsqldb"/>
                        <hibernatetool destdir="${project.build.directory}/test-classes/hsqldb">
                            <classpath refid="hibernate_tools_path"/>
                            <jpaconfiguration persistenceunit="testPersistenceUnit"
                                              propertyfile="src/test/resources/META-INF/spring/jdbc.properties"/>
                            <hbm2ddl drop="false" create="true" export="false"
                                     outputfilename="create_db.sql"
                                     delimiter=";" format="true"/>
                            <hbm2ddl drop="true" create="false" export="false"
                                     outputfilename="drop_db.sql"
                                     delimiter=";" format="true"/>
                        </hibernatetool>
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    This Maven plugin will generate the following DDL files:

    • create_db.sql (containing all DDL statements for creating the DB)
    • drop_db.sql (containing all DDL statements for dropping the DB)
0
jstadler On

As of hibernate-tools 5.3 you can also use the integrated hibernate-tools-maven-plugin. There you should also find documentation on how to use it.

Unfortunatelly the maven-site with detailed description of the parameters/goals for it is not yet published but a little bit older version of it is available here.

There would be a PR (#842) that would allow for the site to be generated... unfortunatelly it hasn't found its way in yet.

0
Archie On

Please try my new plugin which addresses this exact problem which has also plagued me for many years.

https://github.com/archiecobbs/hibernate-jpa-schemagen