---
title: "How to use Hibernate to generate a DDL script from your Play! Framework project"
date: 2014-10-15T21:41:33+00:00
author: "poornerd"
tags: ["hibernate", "jpa"]
canonical: https://www.poornerd.com/2014/10/15/how-to-use-hibernate-to-generate-a-ddl-script-from-your-play-framework-project/
source: Raw Markdown twin of the HTML article; content is the original source.
---
[<img src="https://www.poornerd.com/wp-content/uploads/2014/10/Screen-Shot-2014-10-15-at-23.34.25-300x212.png" alt="ddl script" width="300" height="212" class="alignleft size-medium wp-image-467" srcset="https://www.poornerd.com/wp-content/uploads/2014/10/Screen-Shot-2014-10-15-at-23.34.25-300x212.png 300w, https://www.poornerd.com/wp-content/uploads/2014/10/Screen-Shot-2014-10-15-at-23.34.25.png 767w" sizes="(max-width: 300px) 100vw, 300px" />](https://www.poornerd.com/wp-content/uploads/2014/10/Screen-Shot-2014-10-15-at-23.34.25.png)Ok, so you have been using the hibernate property name=&#8221;**hibernate.hbm2ddl.auto**&#8221; value=&#8221;**update**&#8221; to continuously update your database schema, **but now you need a complete DDL script?**

Use this method from you Global Class onStart to export the DDL scripts.  Just give it the package name (with path) of your Entities as well as a file name:

&nbsp;

<pre class="brush: plain; title: ; notranslate" title="">public void onStart(Application app) {
        exportDatabaseSchema("models", "create_tables.sql");
    }

    public void exportDatabaseSchema(String packageName, String scriptFilename) {

        final Configuration configuration = new Configuration();
        final Reflections reflections = new Reflections(packageName);
        final Set&lt;Class&lt;?&gt;&gt; classes = reflections.getTypesAnnotatedWith(Entity.class);
        // iterate all Entity classes in the package indicated by the name
        for (final Class&lt;?&gt; clazz : classes) {
            configuration.addAnnotatedClass(clazz);
        }
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");

        SchemaExport schema = new SchemaExport(configuration);
        schema.setOutputFile(scriptFilename);
        schema.setDelimiter(";");
        schema.execute(Target.SCRIPT, SchemaExport.Type.CREATE );  // just export the create statements in the script
    }
</pre>

That is it!

Thanks to @MonCalamari for answering my Question on <a href="http://stackoverflow.com/questions/25871914/how-can-i-get-the-org-hibernate-cfg-configuration-in-play-framework-2-using-the/26380750#26380750" onclick="javascript:pageTracker._trackPageview('/outbound/article/stackoverflow.com');" target="_blank">Stackoverflow here</a>.
